0
votes

I have a book list which are in categories and subcategories.

Every category hasMany Subcategories. And every subcategory belongsTo a category in my Models.

Now I want to have routes like this:

books/{category?}

and

books/{category?}/{subcategory?}

my Routes look like this:

Route::resource('categories', 'ReligionsController');

Route::resource('category/{category?}', 'ReligionBranchController');

Route::get('category/{category?}/{subcategory?}', function()
{
    return 'Quitter never win';
});

Now the Problem is, that my 2nd route works perfectly find, with my ReligionBranchController, but my last route (3rd) isn't working at all and I get this message:

 Route pattern "/categories/{category}/{{category}}" cannot reference variable name "category" more than once.

How am I going to use dynamic routes like this:

 example.org/category/fear/thriller
 example.org/category/love/drama
 example.org/category/love/science-fiction

Hope someone understands my trouble.

1
The error you are showing us is for the first route Route pattern "/categories/ and not the third, which starts with category and not categories.Antonio Carlos Ribeiro

1 Answers

2
votes

You must undestand that Laravel tries to resolve routes the faster it can, so your most generic routes must be the last ones, that being said, I recommend you to change the order of your routes to:

Route::get('category/{category}/{subcategory}', function()
{
    return 'Quitter never win';
});

Route::resource('category/{category?}', 'ReligionBranchController');

Route::resource('categories', 'ReligionsController');

Also, this route

Route::get('category/{category?}/{subcategory?}', function() ...

Is exactly the same as

Route::get('category/{category?}', function()

If your user pass only one parameter, or no parameters at all, so I also recommend to change it to:

Route::get('category/{category}/{subcategory}', function() ...

This way, if someone gives category AND subcategory it will enter the first one, if just category OR no category, the second one.