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(){
$photos = Photo::getAll();
return view('views',['photos'=>$photos]);
}
}
or creating another Controller and handling the web requests there and adding a new rout in the routes file for example : mysite.com\photos\all to the routes file?
or do I have to keep this in another Controller or do I have to decide whether or not the request is from the web inside the same method like the below example:
public function index()
{
$photos = Photo::all();
if ( from web ){
return view('views',['photos'=>$photos]);
} else {
return response()->
json(['result' => $photos]);
}
}
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.