问题
I want to prefix all my routes with a value from config but I am having trouble getting the value from the config file:
Config::get('custom.routes.prefix');
The above gets null even though the value is set in the config file:
//config/custom.php
'routes' => => [
'prefix' => 'whatever',
],
How can I get access to config in routes.php?
Edit
Please note this is not how a question about how to prefix routes, it's how to prefix them with a value from config.
回答1:
Maybe just store the route name in the config file, like;
return [
'route' => 'admin.index',
]
Then use it with the route helper. For example...
<a href="{!! route(config('yourConfig.route')) !!}">Admin</a>
Source: https://laracasts.com/discuss/channels/laravel/how-to-access-route-method-in-config-file/replies/126295
回答2:
Your config/custom.php
file:
'routes' => [
'prefix' => 'home',
],
Your app/Http/routes.php
file:
Route::group(['prefix' => config('custom.routes.prefix')], function () {
// This route will be prefix with your configured prefix.
Route::get('/', 'WelcomeController@index');
});
来源:https://stackoverflow.com/questions/39083846/get-config-in-routes