Giving routes of different kinds with same URL the same name?

Published 1 year ago by APPLE199

To make my life simpler in a huge website, can I do this?

    Route::get('view/{id}', 'PostController@show')->name('post');
    Route::delete('view/{id}', 'PostController@delete')->name('post');
    Route::post('view/{id}', 'PostController@save')->name('post');

And then in my form, I can do this.

    <!-- Delete Form -->
    <form method="post" action="{{ route('post', $post->id) }}">
        <input type="hidden" name="_method" value="DELETE">
        <button type="submit">Delete</button>
    </form>

    <!-- Edit Form -->
    <form method="post" action="{{ route('post', $post->id) }}">
        <input type="hidden" name="_method" value="PATCH">
        <button type="submit">Edit</button>
    </form>

    <!-- Etc -->

Can I do this? Is this recommended?

Best Answer (As Selected By APPLE199)
Refringe

If the routes have the same URL there's no point in naming all of them. Just name the GET request and use that route name to generate URLs for the other request types. This removes a little bit of unnecessary repetition.

I'm not even sure the DELETE or POST route names would even be picked up in your example. You're not passing the request type to the route function (I don't think you can) so it wouldn't know which one to return. You can test this by simply changing the routes so they're different, but still sharing the same name:

Route::get('view/{id}', 'PostController@show')->name('post');
Route::delete('view/delete/{id}', 'PostController@delete')->name('post');
Route::post('view/save/{id}', 'PostController@save')->name('post');

I would expect route('post', $post->id) to return either /view/{id} or an error.

However, if your routes have different URLs then I would recommend giving them different (descriptive) names based on their type:

Route::get('view/{id}', 'PostController@show')->name('post.show');
Route::delete('view/delete/{id}', 'PostController@delete')->name('post.delete');
Route::post('view/save/{id}', 'PostController@save')->name('post.save');

That being said, I'm pretty sure there's no "rules" when it comes to this. Do what works for you. :)

kreierson

Yeah that's fine. In fact if you do

Route::resource('view', 'PostController');

Then all those routes will be set up for you. Then in your terminal run php artisan route:list to view the routes that will set up for you

APPLE199

@kreierson Yes, I know. But I am asking if it's okay to name routes the same name if they are different request types...

Refringe

If the routes have the same URL there's no point in naming all of them. Just name the GET request and use that route name to generate URLs for the other request types. This removes a little bit of unnecessary repetition.

I'm not even sure the DELETE or POST route names would even be picked up in your example. You're not passing the request type to the route function (I don't think you can) so it wouldn't know which one to return. You can test this by simply changing the routes so they're different, but still sharing the same name:

Route::get('view/{id}', 'PostController@show')->name('post');
Route::delete('view/delete/{id}', 'PostController@delete')->name('post');
Route::post('view/save/{id}', 'PostController@save')->name('post');

I would expect route('post', $post->id) to return either /view/{id} or an error.

However, if your routes have different URLs then I would recommend giving them different (descriptive) names based on their type:

Route::get('view/{id}', 'PostController@show')->name('post.show');
Route::delete('view/delete/{id}', 'PostController@delete')->name('post.delete');
Route::post('view/save/{id}', 'PostController@save')->name('post.save');

That being said, I'm pretty sure there's no "rules" when it comes to this. Do what works for you. :)

Please sign in or create an account to participate in this conversation.