简体   繁体   English

在 laravel 5.1 配置文件中使用命名路由

[英]Using named routes in a laravel 5.1 config file

I'm trying to use a named route in my config file and I keep getting a 500 error.我试图在我的配置文件中使用命名路由,但我不断收到 500 错误。

<?php
return [
    'Warden' => [
         route('warden::models', ['user']), 
         'fa fa-btn fa-fw fa-user-secret text-success'
    ],
    'Dispatch' => [
         route('dispatch::index'), 
         'fa fa-btn fa-fw fa-fa-microphone text-success'
    ],
    'Identicon' => [
         route('identicon::main', [md5(Auth::user()->email)]), 
         'fa fa-btn fa-fw fa-get-pocket text-success'
    ]
];

I was wondering if there might be something in Laravel that is preventing this from happening.我想知道 Laravel 中是否有什么东西可以阻止这种情况发生。 If not, am I doing something improperly?如果没有,我是否做错了什么?

Also: Side note.另外:旁注。

PHP Catchable fatal error:  
   Argument 2 passed to Illuminate\Routing\UrlGenerator::__construct()  must be an instance of Illuminate\Http\Request, null given, called in 
   /home/austin/html/hidden/vendor/laravel/framework/src/Illuminate/Routing/RoutingServiceProvider.php on line 62 
   and defined in /home/austin/html/hidden/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php on line 99

Is the only error I get in my console and that's only when I use php artisan serve .是我在控制台中遇到的唯一错误,只有在我使用php artisan serve时才会出现。

Update更新

I'm no longer at work so I don't have my exact source code with me;我不再工作,所以我没有我的确切源代码; however, it was similar to what's below.但是,它类似于下面的内容。

(in a blade file) where kregel is a directory and menu is the file name. (在刀片文件中)其中 kregel 是一个目录,而 menu 是文件名。

@foreach(config('kregel.menu') as $menu_item => list($link, $icon))
  <li>
    <a href="{{$link}}>
      $menu_item <i class="{{$icon}}"></i>
    </a>
  </li>
@endforeach

I believe I have found my solution and figured I would share just for the sake of open source-ness.我相信我已经找到了我的解决方案,并认为我会为了开源而分享。

So instead of just using the Auth facade or the route method.所以,而不是仅仅使用 Auth 门面或路由方法。 I instead chose to use a closure for the facade and a string for the route.相反,我选择为门面使用闭包,为路线使用字符串。

Example:例子:

'Identicon' => [
   'link' =>[
      'identicon::main', 
      function() {
         return md5(Auth::user()->email);
      }
   ], 
   'icon' => 'fa fa-btn fa-fw fa-get-pocket text-success'
]

The actual function I have used to produce and build the link.我用来生成和构建链接的实际功能。

protected function linkBuilder($link){
    // This makes sure that there is indeed parameters.
    if(!is_array($link)){
        return route($link);
    }

    // This grabs the two expected parameters.
    list($route, $params) = $link;

    // Now we see if the parameter(s) is actually an anon function
    if($params instanceof Closure) {
        // call this function
        return route($route, $params());
    }
    // This must have no function and must just be 
    // either an array of parameters or just a string
    return route($route, $params);
}

Now to use this function you would end up passing through the array with the key 'link' to this function.现在要使用此函数,您最终将使用键“链接”传递到此函数的数组。 So therefore what ends up getting returned is the proper value.因此,最终返回的是正确的值。

This means that the resulting route function would actually look like这意味着生成的路由函数实际上看起来像

route('identicon::main', md5(Auth::user()->email));

Which, while it might be a little messy, works INCREDIBLY well for my task.虽然它可能有点乱,但对我的任务来说效果非常好。 If there is any other way, anyone might be able to think of to use a facade or a named route form within my config please let me know.如果有任何其他方式,任何人都可以想到在我的配置中使用外观或命名路由表单,请告诉我。

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

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