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

时间:2016-12-25 16:10:35

标签: php laravel laravel-socialite callbackurl

我需要为社交名流使用动态回调网址,所以我将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'),
    ],


    ];

2 个答案:

答案 0 :(得分:2)

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

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

您可以做的最好的事情是不在配置文件中使用Laravel或手动定义的功能,并为您的问题找到另一种解决方案。

答案 1 :(得分:2)

在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']));
    }
}

现在应该可以正常工作