2016-04-08 57 views
0

路線我在laravel5相當新的,我想下生成route.phpLaravel 5命名爲參數

動態路由別名這是它:

Route::get('/menu/{category}/{product}/{item}', '[email protected]')->name('/{category}/{item}'); 

我已經嘗試過用「作爲」和「使用」,我仍然得到:

/menu/{category}/{product}/{item} 

與正確的價值觀,而不是更換所有參數:

/{category}/{item} 
+0

對不起,我試圖把嗨,但它看起來並不想更新,因爲有人射我:D – JroS

+0

你的目的是什麼? – trinvh

+0

目的只是將「http:// localhost:8080/menu/homme/bijoux/pendentif」中的網址替換爲「http:// localhost:8080/homme/pendentif」,其中homme爲{category}, pendentif是{item] – JroS

回答

2

試試:

Route::get('/menu/{category}/{product}/{item}', ['as' => 'a.name.to.your.route' , 'uses' => '[email protected]']); 
+0

嘗試擴大你的答案,至於你做了什麼來解決問題。 – Wowsk

+0

嗨路易斯感謝相同的結果看起來像沒有使用 – JroS

0

簡釋什麼維尼修斯·路易斯說。

Route::get('/menu/{category}/{product}/{item}', ['as' => 'named.route' , 'uses' => '[email protected]']); 

// to get the actual linke 
route('named.route', ['category' => $category->id, 'product' => $product->id, 'item' => $item->id]); 

依賴,你可能做不到 - > ID或任何東西,你可能只是通過整個$類,$產品等依賴於你的控制器路由的設置方式。

編輯: 從您的評論,它喜歡像你想是這樣的:

class MenuController { 
    public function lisItem($category_name, $product_name) { 
     $category = Category::where('name', $category_name)->first(['id']); 
     $product = Product::where('category_id', $category->id)->where('name', $product_name')->first(); 
    } 
} 

Route::get('/{category}/{item}', ['as' => 'named.route' , 'uses' => '[email protected]']); 

// to get the actual linke 
route('named.route', ['category' => $category->id, 'item' => $item->id]); 

有可能是一個更好的方式做了查詢,但應該爲你工作。

+0

您好Kenyon thx您的答覆,但你失去了我我雖然我的問題很簡單,我只是想用另一個使用給定的參數替換顯示的URL,如示例中所示。有沒有可能? – JroS

+0

我不確定你想要完成什麼?按照我給你的方式,你最終會得到一個像'/ menu/10/5/6'這樣的URL,當查看時它將解析爲'MenuController @ listItem'。你想嘗試別名路線嗎?或者你是否想讓URL顯示不同?你究竟想要什麼網址? – Kenyon

+0

exaclty我只是想顯示的url是不同的什麼是用於路由採取你的例子我只想看/ 10/6 – JroS