I have a controller with several methods and I need to add a specific authorization check on start of each method. So I thought to put this check in constructor as,
class AdminController extends BaseController {
public function __construct() {
$this->isAuthorized();
}
protected $layout = "layouts.main";
private function isAuthorized() {
if (!Session::get('userId')) {
echo "inside check"; // checking for debug purpose
return Redirect::to('login');
}
}
/**
* Admin dashboard view after authentication.
*/
public function getDashboard() {
$this->layout->content = View::make('admin.dashboard');
}
}
It does not work, it just prints the message inside Session check and load the dashboard page rather than redirecting back to login page.
I have also tried something like this,
public function getDashboard() {
$this->isAuthorized();
$this->layout->content = View::make('admin.dashboard');
}