Controller or resource controller, how to determine which one to use?

0

I'm creating a small application using Laravel 5.6 , however, I'm having trouble 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');

Would it be relevant to use resource Controller to improve the aesthetics of my route files?

Note: I will still have other routes like change-email

I thought about:

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

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

Project mentioned github

    
asked by anonymous 03.04.2018 / 04:50

1 answer

2

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

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

You have the following routes.

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

  
  • GET / photos index photos.index
  •   
  • GET / photos / 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} destroy photos.destroy
  •   

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

php artisan routes

See that you've just simplified by "grouping" all types of HTTP requests into a route definition.

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

  

On which one to use, it depends a lot on your design structure and   as all your routes will be organized. You can define the resources that will be used and also add specific routes like change-email .

    
03.04.2018 / 13:40