Constructor redirect is not working in Laravel

0 votes

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');
}
Apr 6, 2020 in Laravel by kartik
• 37,530 points
2,403 views

1 answer to this question.

0 votes

Hello,

vent with Illuminate\Routing\Redirector, laravel set redirect in http header, but also continue to execute request like there are no redirect. So the solution is use die() after the redirection.

public function __construct(Request $request, \Illuminate\Routing\Redirector $redirecor)
{
    //$bool = ...
    if (false == $bool) {
        $redirecor->action('MyController@myAction')->send() ;
        die();
    }
}
answered Apr 6, 2020 by Niroj
• 82,820 points

Related Questions In Laravel

0 votes
1 answer

Error:Laravel [InvalidArgumentException] Script “post-install-cmd” is not defined in this package

Hello @kartik, Do composer global update before laravel new <project-name> and you'll ...READ MORE

answered Apr 6, 2020 in Laravel by Niroj
• 82,820 points
532 views
0 votes
1 answer

How to validate an input field if value is not null in Laravel?

Hello @kartik, try using nullable as a rule 'password' ...READ MORE

answered Sep 25, 2020 in Laravel by Niroj
• 82,820 points
5,906 views
0 votes
1 answer

How to redirect to Login if user not logged in Laravel?

Hello @kartik, You can use it in middleware. ...READ MORE

answered Dec 4, 2020 in Laravel by Niroj
• 82,820 points
4,544 views
0 votes
1 answer

What is reverse routing in Laravel?

 Laravel reverse routing is generating URL's based ...READ MORE

answered Mar 17, 2020 in Laravel by Niraj
5,082 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,820 points
9,835 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,820 points
1,096 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,820 points
883 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,820 points
10,977 views
0 votes
1 answer

How to redirect to login if user is not authenticated in laravel?

Hello @kartik, Use middleware for this purpose and ...READ MORE

answered Oct 28, 2020 in Laravel by Niroj
• 82,820 points
2,724 views
0 votes
1 answer

How to check request is ajax or not in Laravel?

Hello, Laravel allow use of their library method that ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,820 points
1,151 views