2013-10-28 38 views
1

我在Laravel遇到了一些路线问题。我想这是因为我不走的好方法,但...Laravel 4 - >具有多个变量的同一控制器上的路线

这里是我的代码:

Route::group(array('prefix' => 'products'), function() 
{ 
    Route::get('', array('uses'=>'[email protected]')); 
    //show all the products 

    Route::get('{Categorie}',array('uses'=>'[email protected]'))->where('Categorie','^[A-Z][a-z0-9_-]{3,19}$'); 
    //show the products of this categorie 

    Route::get('{shopname}',array('uses'=>'[email protected]'))->where('shopname','^[a- z][a-z0-9_-]{3,19}$'); 
    //show the product of this shopname 
}); 

Route::group(array('prefix' => '/products/{:any}'), function() 
{ 
    //no index because productName is not optionnal 

    Route::get('{productName}', array('uses'=>'[email protected]')); 
    //the Product controller is now SINGULAR 
    //show this product in particular 
}); 

所以它的工作为第一组... mysite.fr/products =>确定 mysite.fr/MyCategorie => OK mysite.fr/mashopname => OK

但是当我添加第二paramater等:

mysite.fr/products/myshopname/myfirstproduct

我得到一个错误与特定的消息...

非常感谢您的帮助!

回答

0

这里的问题是这些都是相同的路线。 Laravel不知道什么会被视为一个分类,商店名称或其他。例如,如果我去/products/test,Laravel不会知道测试是一个分类,一个商店名称还是一个产品的名称。

试试这个...

Route::group(array('prefix' => 'products'), function() 
{ 
    Route::get('/', array('uses'=>'[email protected]')); 
    //show all the products 

    Route::get('categorie/{Categorie}',array('uses'=>'[email protected]'))->where('Categorie','^[A-Z][a-z0-9_-]{3,19}$'); 
    //show the products of this categorie 

    Route::get('shopname/{shopname}',array('uses'=>'[email protected]'))->where('shopname','^[a- z][a-z0-9_-]{3,19}$'); 
    //show the product of this shopname 

    Route::get('product/{productName}', array('uses'=>'[email protected]')); 
    //the Product controller is now SINGULAR 
}); 

这样一来,如果我去products/categorie/test,Laravel就知道我正在寻找一个categorie和能够路由我恰如其分。

更新:

如果Hightech是一个类别,product_1是一个产品,你可以使用这样的路线......

Route::get('category/{categorie}/product/{product}',array('uses'=>'[email protected]'))->where('categorie','^[A-Z][a-z0-9_-]{3,19}$')->where('product','^[A-Z][a-z0-9_-]{3,19}$'); 
    //show the products of this categorie 

然后是网址是.com/products/category/Hightech/product/product_1。或者你可以把/product/category拿出来,你可以去.com/products/Hightech/product_1

+0

我想到了这一点,但我的想法是保持网址中的商店名称/种类。你试试这个 - >“Route :: get('/ {:any}/{productName}',array(''),然后你去.com/products/Hightech/product_1。使用 '=>' 产品@ getProduct'));”但仍然是一个错误... – pierreaurelemartin

+0

我刚刚更新了答案,所以你可以使用这样的URL。 – user1669496

相关问题