Pergunta

i've got some routes problems with Laravel. I think it's because i don't take the good method but...

Here is my code:

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

So it's working for the first group... mysite.fr/products => ok mysite.fr/MyCategorie => ok mysite.fr/mashopname => ok

but when i add the second paramater like :

mysite.fr/products/myshopname/myfirstproduct

i got an error witouht specific message...

Thanks a lot for your help !

Foi útil?

Solução

The problem here is these are all the same routes. Laravel doesn't know what would count as a categorie, shopname, or any. For example if I go to /products/test, Laravel won't know if test is a categorie, a shopname, or a name of a product.

Try this instead...

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

This way, if I go to products/categorie/test, Laravel will know that I'm looking for a categorie and be able to route me appropriately.

Update:

If Hightech is a category and product_1 is a product, you could use a route like this...

    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   

And then the URL would be .com/products/category/Hightech/product/product_1. Or you can take the /product out and /category out and you could just go to .com/products/Hightech/product_1

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top