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 in Laravel by kartik
• 26,540 points
798 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 by Niroj
• 57,260 points

Related Questions In Laravel

0 votes
1 answer

What is reverse routing in Laravel?

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

answered Mar 17 in Laravel by Niraj
1,663 views
0 votes
1 answer

What is the significant difference between insert() and insertGetId() function in Laravel?

Hello, Insert(): This function is simply used to ...READ MORE

answered Mar 17 in Laravel by Niroj
• 57,260 points
172 views
0 votes
1 answer

What is the concept of controller in Laravel?

Hii, In the MVC framework, the letter ‘C’ ...READ MORE

answered Mar 18 in Laravel by Niroj
• 57,260 points
95 views
0 votes
1 answer

What is middleware? How can we register middeleware in Laravel?

Middleware acts as a bridge between a ...READ MORE

answered Mar 18 in Laravel by Niroj
• 57,260 points
268 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 in Laravel by Niroj
• 57,260 points
2,090 views
0 votes
1 answer

What is redirection in Laravel?

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

answered Mar 18 in Laravel by Niroj
• 57,260 points
218 views
0 votes
1 answer

How to install Laravel via composer?

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

answered Mar 23 in Laravel by Niroj
• 57,260 points
181 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 in Laravel by Niroj
• 57,260 points
2,490 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 in Laravel by Niroj
• 57,260 points
183 views
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 in Laravel by Niroj
• 57,260 points
112 views