I created a small site by laravel 6, with the four blade index, create, edit, show and an authentication system, I want everyone to see the blades index and show, and the blades create and edit prohibit that if user authenticate. TinghirsController.php
JavaScript
x
public function __construct() {
$this->middleware('auth');
}
public function index()
{
$tinghirs=Tinghir::orderBy('created_at','desc')->paginate(30);
return view('tinghirs.index', ['tinghirs' => $tinghirs]);
}
public function create()
{
return view('tinghirs.create');
}
public function show($id){
$tinghirs = Tinghir::where('id',$id)->firstOrfail();
return view('tinghirs.show', ['tinghirs' => $tinghirs]);
}
public function edit($id) {
$tinghir = Tinghir::find($id);
return view('tinghirs.edit', ['tinghir' => $tinghir]);
}
Route/web.php
JavaScript
Route::resource('tinghirs','TinghirsController');
Advertisement
Answer
As per the documentation, you can specify which controller methods you want to apply a piece of middleware to. In you’re case you want to apply the auth middleware to all methods except index
and show
.
To achieve change the middleware call in your __constructor
method to be:
JavaScript
$this->middleware('auth')->except('index', 'show');