我在Laravel遇到了一些路线问题。我认为这是因为我没有采取好的方法,但是......
这是我的代码:
Route::group(array('prefix' => 'products'), function()
{
Route::get('', array('uses'=>'products@index'));
//show all the products
Route::get('{Categorie}',array('uses'=>'products@categorie'))->where('Categorie','^[A-Z][a-z0-9_-]{3,19}$');
//show the products of this categorie
Route::get('{shopname}',array('uses'=>'products@shopname'))->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'=>'product@getProduct'));
//the Product controller is now SINGULAR
//show this product in particular
});
所以它适用于第一组...... mysite.fr/products =>好 mysite.fr/MyCategorie =>好 mysite.fr/mashopname =>确定
但是当我添加第二个参数时:
mysite.fr/products/myshopname/myfirstproduct
我收到了一条错误消息......
非常感谢你的帮助!
答案 0 :(得分:1)
这里的问题是这些都是相同的路线。 Laravel不知道什么会被视为类别,商店或任何。例如,如果我转到/products/test
,Laravel将不知道测试是否是类别,商店名称或产品名称。
试试这个......
Route::group(array('prefix' => 'products'), function()
{
Route::get('/', array('uses'=>'products@index'));
//show all the products
Route::get('categorie/{Categorie}',array('uses'=>'products@categorie'))->where('Categorie','^[A-Z][a-z0-9_-]{3,19}$');
//show the products of this categorie
Route::get('shopname/{shopname}',array('uses'=>'products@shopname'))->where('shopname','^[a- z][a-z0-9_-]{3,19}$');
//show the product of this shopname
Route::get('product/{productName}', array('uses'=>'product@getProduct'));
//the Product controller is now SINGULAR
});
这样,如果我去products/categorie/test
,Laravel会知道我正在寻找一个categorie
并且能够恰当地路由我。
更新
如果Hightech
是一个类别且product_1
是产品,您可以使用这样的路线......
Route::get('category/{categorie}/product/{product}',array('uses'=>'products@categorie'))->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