如何使用 services.php laravel 配置文件中的函数

How to use functions in services.php laravel config file

我需要为社交名流使用 动态回调 url 所以我将 url() 函数添加到我的 services.php 文件中它工作正常(并且它仍在我的实时服务器上运行)但是当尝试在本地启动项目时,我收到以下错误。当我删除 url() 方法时,一切正常,请帮忙。

PHP Fatal error:  Uncaught ReflectionException: Class log does not exist in /home/fenn/projects/jokwit/vendor/laravel/framework/src/Illuminate/Container/Container.php:734
Stack trace:
#0 /home/fenn/projects/jokwit/vendor/laravel/framework/src/Illuminate/Container/Container.php(734): ReflectionClass->__construct('log')
#1 /home/fenn/projects/jokwit/vendor/laravel/framework/src/Illuminate/Container/Container.php(629): Illuminate\Container\Container->build('log', Array)
#2 /home/fenn/projects/jokwit/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(697): Illuminate\Container\Container->make('log', Array)
#3 /home/fenn/projects/jokwit/vendor/laravel/framework/src/Illuminate/Container/Container.php(849): Illuminate\Foundation\Application->make('log')
#4 /home/fenn/projects/jokwit/vendor/laravel/framework/src/Illuminate/Container/Container.php(804): Illuminate\Container\Container->resolveClass(Object(ReflectionParameter))
#5 /home/fenn/projects/jokwit/vendor/laravel/framework/src/Illuminate/Container/Container.php(7 in /home/fenn/projects/jokwit/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 734

这是我的 services.php 文件

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Third Party Services
    |--------------------------------------------------------------------------
    |
    | This file is for storing the credentials for third party services such
    | as Stripe, Mailgun, Mandrill, and others. This file provides a sane
    | default location for this type of information, allowing packages
    | to have a conventional place to find your various credentials.
    |
    */

    'mailgun' => [
    'domain' => env('MAILGUN_DOMAIN'),
    'secret' => env('MAILGUN_SECRET'),
    ],

    'mandrill' => [
    'secret' => env('MANDRILL_SECRET'),
    ],

    'ses' => [
    'key'    => env('SES_KEY'),
    'secret' => env('SES_SECRET'),
    'region' => 'us-east-1',
    ],

    'stripe' => [
    'model'  => App\User::class,
    'key'    => env('STRIPE_KEY'),
    'secret' => env('STRIPE_SECRET'),
    ],

    'facebook' => [
    'client_id' => '1700935300171729',
    'client_secret' => 'XXXXXXXXXXXXXXXXXXX',
    'redirect' => url('/facebook/callback'),
    ],


    'google' => [
    'client_id'     => 'XXXXXXXXXXXXXXXXXXXXXXXX',
    'client_secret' => 'XXXXXXXXXXXXXXXXXXXXXXXX',
    'redirect'      => url('google/callback'),
    ],


    ];

它不起作用,因为在生产中 Laravel 缓存所有配置文件并使用此缓存。在开发环境中Laravel 没有创建缓存。

你可以通过在配置中注释url()然后运行php artisan config:cache命令来检查它。取消注释 url() 部分,您会看到错误消失了。

您在这里可以做的最好的事情是不使用 Laravel 或在配置文件中手动定义的函数,并为您的问题寻找其他解决方案。

在services.php文件中

...
'redirect'      => 'google/callback',
...

接下来创建服务提供者,例如 ConfigServiceProvider

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class ConfigServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        \Config::set("services.google.redirect", url(\Config::get('services')['google']['redirect']));
    }
}

现在应该可以正常工作了