简体   繁体   中英

How to use URL:: or asset() in config files in 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');

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM