samedi 12 novembre 2016

What is best practice in a Restful laravel app for Controllers and methods

I am developing a restful laravel application and I need to know what is the best practice to implement the routes, controllers and methods in the Laravel to both support the restful requests and HTTP web requests we can easily create a resource controller and then add the following line to the routes file in Laravel:

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

and then in the PhotoController we just need to add the following lines of codes which returns a json response from all the photos:

class PhotoController {

  public function index()
  {
     $photos = Photo::all();        

     return response()->
                        json(['result' => $photos]);       
     }
}

we also need a Controller and a method which responds to web HTTP request and returns a web page instead of a json response which displays all the photos to the web users

Question: where is the best place to put this method and Controller is it a good practice to put it inside the same Controller and returns a view? like following example

class PhotoController{

 public function getAll(){
            $roles = Role::getAll();

            return view('admin.acl.roles_list',['roles'=>$roles]);
  }
}

or creating another Controller and handling the web requests there or maybe someone has another solution?

I also have to mention that I previously asked the below question: Best design practice to implement routes and controllers for a RESTFul Laravel app but didn't get any answer.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire