4

Let's assume we have 2 such routes:

Route::get('{categoryitem}', ['as' => 'category_index', 'uses' => 'CategoryDisplayController@index']);

Route::get('{entryitem}', ['as' => 'entry', 'uses' => 'EntryController@show']);

There are no parameter constraints for parameters and 2 following route model bindings are defined:

Route::bind('categoryitem', function($slug)
{
    return Category::whereSlug($slug)->root()->firstOrFail();
});
Route::bind('entryitem', function($slug)
{
    return Entry::whereSlug($slug)->firstOrFail();
});

Now let's assume that URL we run is http://project/something. Is it possible to make Laravel look first in route categoryitem for something slug and in case no model is found it will look in the second route for entry with slug something? I haven't found solution for this other than adding some prefix/suffix to route

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291

1 Answers1

3

"Skipping" routes on a condition is not possible at the moment. Although there is a request on github that would allow skipping on a condition if implemented by the framework...

In the meantime you will have to write one route that catches them all. And determine inside of that route (or controller if you wish) if the item exists.

Route::get('{slug}', function($slug){
    // check if categoryitem exists
    // else check if entryitem exists, etc...
});

Obviously if this becomes a bit two much code move it into a controller

lukasgeiter
  • 147,337
  • 26
  • 332
  • 270