3

Say I have some routes:

 Route::get('items', 'ItemController@index')->name('item.index');
 Route::get('items/{hash}/{slug}', 'ItemController@show')->name('item.show');

While using route model binding I want to handle the following cases:

  • /items/<correct_hash>/<incorrect_slug> permanent redirect to correct hash and slug
  • /items/<correct_hash> permanent redirect to correct hash and slug
  • Anything else redirect to /items/

I currently understand the shortcut benefit of route model binding which reduces controller code but is typically used for a simple /items/{id} cases. Is it possible to extend what is shown in the documentation for my case? Or should I scrap the entire model binding approach and go back to caveman controller logic?

Currently it seems the documentation can only bind one parameter at a time not a combination:

    Route::bind('user', function ($value) {
        return App\User::where('name', $value)->first() ?? abort(404);
    });
Helen Che
  • 1,951
  • 5
  • 29
  • 41
  • AFAIK is not possible to define a route mode binding, implicit or explicit, with more than one parameter. IMHO that is a very difficult route to follow, you'll have to fight against ton of already written code, RESTful resources usually do follow the one parameter way, being it id or slug. – dparoli Mar 01 '19 at 23:43
  • @dparoli I have done some searching in the meantime, apparently it might be possible to have multiple parameters https://stackoverflow.com/questions/39289608/laravel-5-3-route-model-binding-with-multiple-parameters. I'll try this out to see if it's true for 5.8 release. – Helen Che Mar 01 '19 at 23:46
  • Good luck, in case you solve you can answer your own question. – dparoli Mar 01 '19 at 23:51

0 Answers0