Taylor
Taylor

Reputation: 3141

Laravel - Naming routes (GET, POST, DELETE) the same name?

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?

Upvotes: 4

Views: 1894

Answers (1)

Angad Dubey
Angad Dubey

Reputation: 5452

Yes, in fact you can use the following to keep your route file clean: https://laravel.com/docs/5.2/controllers#restful-naming-resource-route-parameters

Route::resource('foobar', 'FooBarController');

This will auto generate RESTful routes for you:

if you run php artisan route:list you can see all the HTTP routes.

Upvotes: 4

Related Questions