rest - What is best practice in a Restful laravel app for Controllers and methods -
i developing restful laravel application , need know best practice implement routes, controllers , methods in laravel both support restful requests
, http web requests
can create resource controller , add following line routes file in laravel:
route::resource('photo', 'photocontroller');
and in photocontroller
need add following lines of codes returns json
response photos:
class photocontroller { public function index() { $photos = photo::all(); return response()-> json(['result' => $photos]); } }
we need controller
, method
responds web http
request , returns web page instead of json
response displays photos web users
question: best place put method , controller practice put inside same controller , returns view? following example
class photocontroller{ public function getall(){ $photos = photo::getall(); return view('views',['photos'=>$photos]); } }
or creating controller
, handling web requests there , adding new rout
in routes
file example : mysite.com\photos\all
routes file?
or have keep in controller
or have decide whether or not request web inside same method below example:
public function index() { $photos = photo::all(); if ( web ){ return view('views',['photos'=>$photos]); } else { return response()-> json(['result' => $photos]); } }
i have mention asked below question: best design practice implement routes , controllers restful laravel app didn't answer.
there's reason why didn't answer last time asked question. depends on project. share own preference:
i have 2 sets of routes, middleware, controllers , common services.
routes:
//for web route::resource('photo', 'photocontroller'); /for api, versioning route::resource('api/v1/photo', 'api\photocontroller');
middlewares
//for web public function handle($request, closure $next, $role="view") { if(gate::denies($role.'-photo', $photo)){ if($role == "view"){ abort(404); } return $this->redirect($photo)->witherrors(['you not authorized see photo']);; } } return $next($request); /for api, versioning public function handle($request, closure $next, $role="view") { if(gate::foruser(auth::guard('api')->user())->denies($role.'-photo', $photo)){ if($role == "view"){ return response::json([], 404); } return response::json([], 403); } } return $next($request);
controllers
//photocontroller class photocontroller{ //restful name here public function index(){ $photoservice = new photoservice(); $photos = $photoservice->getall(); return view('views',['photos'=>$photos]); } } //api\photocontroller class photocontroller{ //restful name here public function index(){ $photoservice = new photoservice(); $photos = $photoservice->getall(); return response::json($photos, 200); } }
service
class photoservice(){ //you add _construct() accept request $request or user $user public function getall(){ return photo::all(); } }
bonus: exception handler
you should take @ restexceptionhandlertrait.
it may seems lot of duplication me more readable , clear adding $request->wantsjson()
everywhere. might think overkill and, in cases, overkill.
however, because request , response logic api , web separated. therefore can deal different requests (ex: accessing user information) , different responses (ex: json or view). creates dedicated space each type of request allow execute logic if needed each part of app.
again, it's question of preference , project hope answers question.
Comments
Post a Comment