簡體   English   中英

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

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

我試圖在我的配置文件中使用命名路由,但我不斷收到 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'
    ]
];

我想知道 Laravel 中是否有什么東西可以阻止這種情況發生。 如果沒有,我是否做錯了什么?

另外:旁注。

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

是我在控制台中遇到的唯一錯誤,只有在我使用php artisan serve時才會出現。

更新

我不再工作,所以我沒有我的確切源代碼; 但是,它類似於下面的內容。

(在刀片文件中)其中 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

我相信我已經找到了我的解決方案,並認為我會為了開源而分享。

所以,而不是僅僅使用 Auth 門面或路由方法。 相反,我選擇為門面使用閉包,為路線使用字符串。

例子:

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

我用來生成和構建鏈接的實際功能。

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);
}

現在要使用此函數,您最終將使用鍵“鏈接”傳遞到此函數的數組。 因此,最終返回的是正確的值。

這意味着生成的路由函數實際上看起來像

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

雖然它可能有點亂,但對我的任務來說效果非常好。 如果有任何其他方式,任何人都可以想到在我的配置中使用外觀或命名路由表單,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM