Get config in Routes?

喜欢而已 提交于 2021-01-28 19:44:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!