Reputation: 10105
Base Controller
class MyBaseController extends Controller {
public function __construct()
{
if (Auth::check()) {
// The user is logged in...
}
else {
//Redirect to login page
}
}
}
Controller that will have database calling functions and will be available for logged in users only
class ChildController extends MyBaseController {
public function __construct()
{
parent::__construct();
}
}
What do I need ?
I want to check each request if that is made by an authenticated user ?
Question:
Is my approach correct or is this best practice to check if the call to this controller is made by authenticated user ?
Upvotes: 0
Views: 184
Reputation: 562
You were almost there, you need to use this concept of middleware. There is a built in middleware for Session based authentification simply called "auth".
//in your controller :
public function __construct() {
$this->middleware('auth');
}
You can learn more about middlewares and controllers in the docs
Upvotes: 0
Reputation: 3866
The best would be to use Middlewares.
You can simply define a route group like this and add a middleware.
Route::group([
'middleware' => 'auth',
], function() {
// Your routes here...
});
Upvotes: 1