简体   繁体   中英

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.

<?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. 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 .

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.

@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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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