Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use URL:: or asset() in config files in laravel 5

Tags:

laravel-5

In any config file If I use URL class I get the error 'Class URL not found'; if I use the function "asset", when I update composer.json I get this error: Catchable fatal error: Argument 2 passed to Illuminate\Routing\UrlGenerator::__construct() must be an instance of Illuminate\Http\Request, null given,

Outside of config files both work fine

return [
    'photos_url' => URL::asset('xxx'),
];

or

return [
    'photos_url' => asset('xxx'),
];

Test

echo config('site.photos_url'); // or echo Config::get('site.photos_url');
like image 548
ebelendez Avatar asked Mar 11 '15 18:03

ebelendez


2 Answers

Configs are loaded really early and probably not meant to use anything from the framework except Dotenv

return [
    'photos_url' => URL::asset('xxx'),
];

Instead you can use:

return [
    'photos_url' => env('APP_URL').'/rest_of_path.ext',
];

Source: Laravel Issue #7671

like image 180
bmatovu Avatar answered Nov 12 '22 14:11

bmatovu


You shouldn't use dynamic code in your config. As a solution you can use ConfigServiceProvider to add any specific cases:

public function register()
{
    config([
        'photos_url' => assets('xxx'),
        'services.facebook.redirect' => url('auth/callback/facebook'),
    ]);
}

Source: https://github.com/laravel/framework/issues/7671

like image 34
zub0r Avatar answered Nov 12 '22 12:11

zub0r