Laravel 在路由中获取配置?
我想在我的所有路线前加上一个来自配置的值,但从配置文件中获取该值时遇到问题:Laravel 在路由中获取配置?,laravel,laravel-5,laravel-5.2,Laravel,Laravel 5,Laravel 5.2,我想在我的所有路线前加上一个来自配置的值,但从配置文件中获取该值时遇到问题: Config::get('custom.routes.prefix'); //config/custom.php 'routes' => => [ 'prefix' => 'whatever', ], 即使在配置文件中设置了该值,上述值也会变为null: Config::get('custom.routes.prefix'); //config/custom.php 'routes'
Config::get('custom.routes.prefix');
//config/custom.php
'routes' => => [
'prefix' => 'whatever',
],
即使在配置文件中设置了该值,上述值也会变为null:
Config::get('custom.routes.prefix');
//config/custom.php
'routes' => => [
'prefix' => 'whatever',
],
如何访问routes.php中的配置
编辑
请注意,这不是关于如何为路由添加前缀的问题,而是如何使用config中的值为路由添加前缀。您的
config/custom.php
文件:
'routes' => [
'prefix' => 'home',
],
Route::group(['prefix' => config('custom.routes.prefix')], function () {
// This route will be prefix with your configured prefix.
Route::get('/', 'WelcomeController@index');
});
您的app/Http/routes.php
文件:
'routes' => [
'prefix' => 'home',
],
Route::group(['prefix' => config('custom.routes.prefix')], function () {
// This route will be prefix with your configured prefix.
Route::get('/', 'WelcomeController@index');
});
也许只需在配置文件中存储路由名称,如
返回[
“路由”=>“管理索引”,
]
然后将其与route helper一起使用。例如
来源:不起作用,当我列出路线时,该值为空,这意味着它们没有前缀。不,它应该起作用。正如您在评论中所说的,我的配置文件有问题。您可以访问
routes.php
文件中的所有配置值。如果你不能,那么其他的事情就发生了。我不认为你可以在routes中访问config()
,但是你可以访问env()
,那么为什么不在文件中添加ROUTE\u PREFIX=whatever
,并访问ROUTE::group([“PREFIX”=>env(“ROUTE\u PREFIX”)],…
EDIT:在测试之后,您似乎可以在路由中使用config()
,因此这一定是另一个问题。您能给我看一个路由文件工作的示例吗?谢谢!route::get(“/”.env(“APP_env”)、function(){dd(config(“APP.env”);
,然后导航到/local
返回“local”
。如果您切换env()
和config()
的位置,效果也一样。谢谢-我的配置文件有问题。没问题,很高兴您解决了!