简体   繁体   English

Laravel-如何从配置文件中获取命名路由?

[英]Laravel - How do I get a named route from a config file?

I have a few named routes in the routes.php file which I can build the url to via the global route('routename',[args]) . 我在routes.php文件中有一些命名的路由,可以通过全局route('routename',[args])构建URL。 However how can I do this in a config file? 但是,如何在配置文件中执行此操作?

I've tried URL::route('name') , and also route('name') and receive the error: Fatal error: Uncaught ReflectionException: Class log does not exist in /var/www/site/vendor/laravel/framework/src/Illuminate/Container/Container.php:734 我尝试了URL::route('name')route('name')并收到错误: Fatal error: Uncaught ReflectionException: Class log does not exist in /var/www/site/vendor/laravel/framework/src/Illuminate/Container/Container.php:734

When laravel is bootstraping your application to process the request, the config files are not able to call functions declared in autoloaded classes, because they are not loaded already. 当laravel启动您的应用程序以处理请求时,配置文件无法调用在自动加载的类中声明的函数,因为它们尚未加载。 I suggest to you setting the route's name: 我建议您设置路线的名称:

//config/custom.php
'checkout' => 'shop.checkout'

Then, in your controllers or views, for example, you build your routes using the config value: 然后,例如,在控制器或视图中,使用config值构建路由:

$url = route(config('custom.checkout'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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