简体   繁体   中英

Laravel base controller method to call a filter

A have a problem using Laravel 3, I need to use this laravel version because this is a homologated version of the laravel in my company.

So, I want to implement a protected method inside a base_controller to use this method inside all of my controllers.

Then I implemented the code below:

protected function verifyLoggedUser( $module, $action ) {
        $result = false;
        //logical to validate user permissions
        if( !$result )
            return $this->filter('before', 'profileValidate');
        else
            return $result;
}

Inside my controller I call this method to verify the logged user permissions, then I need that when the permission is false, the base controller call the filter profileValidate, that filter will redirect the user to some route.

This way that I implemented the result of log is:

    Laravel\Routing\Filter_Collection Object ( 
[filters] => Array ( [0] => profileValidate ) [parameters] => [only] => Array ( ) 
[except] => Array ( ) [methods] => Array ( ) )

Inside of my filter, I have a Redirect to some route, but didn't work and didn't show any error!

In beginning of my implementation, I try to redirect the user to some page inside of the base controller, but isn't works, because to works I need to make a return in my parent controller too, then it's a problem, because this verification has to be made in the beginning of my method implementation and the return has to be in the end of the method.

Can you help me with? Thank you!

You could use filters directly on routes passing parameters to filter, setting the role or roles has access to the section, something like this:

Route::group(array('before' => 'verifyLoggedUser:admin,action'), function()
{
    Route::get('panel', 'someController@action');    
    Route::get('dashboard','someController@otherAction');
});

this parameters could be taken by the filter this way:

Route::filter('filter', function($route, $request, $module, $action)
{
    $result = false;
    //logical to validate user permissions
    if( !$result )
        return $this->filter('before', 'profileValidate');
    else
        return $result;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM