Laravel-如何从配置文件中获取命名路由? [英] Laravel - How do I get a named route from a config file?
问题描述
我在routes.php
文件中有一些命名路由,可以通过全局route('routename',[args])
来构建url.但是,如何在配置文件中执行此操作?
I have a few named routes in the routes.php
file which I can build the url to via the global route('routename',[args])
. However how can I do this in a config file?
我尝试了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
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
推荐答案
在laravel启动应用程序以处理请求时,配置文件无法调用在自动加载的类中声明的函数,因为它们尚未加载.我建议您设置路线名称:
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. I suggest to you setting the route's name:
//config/custom.php
'checkout' => 'shop.checkout'
然后,例如,在您的控制器或视图中,使用config值构建路由:
Then, in your controllers or views, for example, you build your routes using the config value:
$url = route(config('custom.checkout'));
这篇关于Laravel-如何从配置文件中获取命名路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!