How to use functions in services.php laravel config file

7.2k views Asked by At

I needed to use a dynamic callback url for socialite so I added the url() function to my services.php file it worked fine(and its still working on my live server) But when tried to start the project locally I get the following error. When I remove the url() method everything works fine please help.

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

Here is my services.php file

<?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

There are 2 answers

0
Alexey Mezenin On BEST ANSWER

It doesn't work because in production Laravel cached all configuration files and using this cache. In development environment Laravel didn't create cache.

You can check it by commenting url() in config and then running php artisan config:cache command. Uncomment url() part and you'll see error is disappeared.

The best you can do here is to not use Laravel or manually defined functions in config files and find another solution for your problem.

1
RainDev On

In services.php file

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

Next create service provider for example 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']));
    }
}

now should work fine