Controller or Resource controller, how do I determine which one to use?

Asked

Viewed 718 times

0

I am creating a small application using the Laravel 5.6, however, I’m having difficulty determining the route nomenclature, or rather, to determine whether or not I use resource Controller.

I currently have these routes:

Route::get('/alterar-senha','Painel\AlterarSenhaController@index')
     ->name('alterar-senha');
Route::post('/alterar-senha','Painel\AlterarSenhaController@alterarSenha')
     ->name('alterar-senha');

It would be relevant to use resource Controller to improve the aesthetics of my route files?

Observing: I’ll still have other routes like change-email

I thought about:

Route::resource('alterar-senha', 'Painel\AlterarSenhaController')
     ->only(['edit', 'update']);

The route to change email followed the same approach. When using resource with you instead of having 4 lines, having only 2 and visually a more pleasing structure to the eye, but this approach would really be valid?

Project mentioned github

1 answer

2

The Resource Controller is a normal controller that implements CRUD functions by default, but by defining a Resource in the route file, you create multiple "routes" without having to write code for these routes. See the example placed on documentation . Creating a Resource path for the Photocontroller :

Route::resource('photos', 'PhotoController');

You have the following routes.

First the method HTTP used, followed by URL which will be used, after we have the method in the Controller that will be executed and, finally, the route name.

  • GET /photos index photos.index
  • GET /photos/create create create photos create.
  • POST /photos store photos.store GET /photos/{photo} show photos show.
  • GET /photos/{photo}/Edit Edit photos.Edit
  • PUT/PATCH /photos/{photo} update photos.update
  • DELETE /photos/{photo} Stroy photos.Destroy

You can see better using the following command at the prompt.

php artisan routes

Note that you just simplified, "grouping" all types of HTTP requests into a route definition.

If the answer is useful, do not forget to mark as answered.

On which to use, depends a lot on your design structure and how all your routes will be organized. You can define the Resources that will be used and also add specific routes as alter-email.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.