Reputation: 10230
I have the following code in my adminController
:
public function index() {
if (!(Auth::check())) {
return Redirect::to('login');
}
$tags = DB::table('Tags')->get();
/* convert Object to array */
$tagsArray = array();
foreach($tags as $tag) {
$tagsArray[$tag->tag] = $tag->tag;
}
$tagsArray = json_decode(json_encode($tagsArray) , TRUE);
return view('admin.index')->with('tags' , $tagsArray);
}
I have the following two steps to check if the user has logged in , if hes not then he will be asked to login:
if (!(Auth::check())) {
return Redirect::to('login');
}
Actually i would like to make these two steps into a method and then apply that method to all the other methods in the adminController
, How do i do this ?
Upvotes: 2
Views: 34
Reputation: 3704
Wrap what you want in a method
public function authCheck() {
if (!(Auth::check())) {
return Redirect::to('login');
}
}
then from other methods call it like
public function someOtherMethod() {
$this->authCheck();
// your code....
}
Upvotes: 1
Reputation: 163788
You should really use auth
or custom middleware for that:
Route::resource('admin', 'adminController)->middleware('auth');
Or use middleware group:
Route::group(['middleware' => ['auth']], function () {
Route::resource('admin', 'adminController);
});
Or define middleware in a constructor:
public function __construct()
{
$this->middleware('auth');
}
Upvotes: 2