seavers
seavers

Reputation: 571

Redirect from parent controller

I'm implementing some Access Control Logic for a Laravel application. I'd like to redirect to a 'not authorised' page if the user is not authorised to view that resource.

I'm using controller/method logic for my urls.

I can redirect from the controller/method that is called, but to make my code more scalable, I'd like to check the ACL logic in a parent controller, maybe the Base_controller, and redirect from there, before the URI controller/method is accessed.

I have tried to add a redirect to a construct, or call another method in the construct that redirects, to no avail.

So, I'm wondering if this is possible, because 'Redirect::to' seems to only work in the controller/method specified in the url, and nowhere else - is that correct?

Upvotes: 0

Views: 2088

Answers (1)

Laurence
Laurence

Reputation: 60048

You need to 'return' the redirect. You can do this from anywhere:

return Redirect::to('login')

But you should run logic in filter:

public function __construct()
{
    $this->beforeFilter('acl');
}

Then in filters.php

Route::filter('acl', function()
{
    if ($something)
    {
          return Redirect::to('somewhere');
    }
});

Upvotes: 5

Related Questions