Search code examples
routeslaravel-9

Laravel 9 - Redirect to edit route when show route is browsed


This is in my routes/web.php

    Route::resources([
        'currencies' => 'CurrencyController',
        'items' => 'ItemController',
    ], [
        'except' => ['show']
    ]);

There are chances that someone might access the show route...

items/{id}
currencies/{id}

I want to redirect them to

items/{id}/edit
currencies/{id}/edit

I don't want to show the 404 page. I tried :

Route::redirect('items/{id}', 'items/{id}/edit');

But this does not work... I tried to use Route::fallback(), but could not find a way to redirect to the same resource's edit action, as the fallback route does not have a controller.


Solution

  • After a lot of searching and hassle, I used a simple solution - don't redirect - but call the edit action on the show route. i.e. I added these lines to routes/web.php at the bottom.

    Route::get('currencies/{currency}', 'CurrencyController@edit')->name('currencies.view');
    Route::get('items/{item}', 'ItemController@edit')->name('items.view');