laravel - How to condition route model bindings by actions in RouteServiceProvider -
i have application needs accomplish in routeserviceprovider.php:
//in \app\providers\routeserviceprovider.php public function boot(router $router) { // parent::boot($router); //somehow can current action $action = $router->getcurrentaction(); if($action == 'edit'){ $router->model('articles','app\article'); } else{ $router->bind('articles', function($id){ return article::published()->findorfail($id); }); } } here route:
route::resource('articles', 'articlescontroller'); here controller:
public function show(article $article){ return view('articles.show',compact('article')); } public function edit(article $article){ return view('articles.edit',compact(['article','tags'])); } the reason want because want show action shows published articles while edit action can change both published , unpublished articles.
if there better solution, please teach me. thank !
why don't make middleware, work 'published' routes, check if article published?
Comments
Post a Comment