简体   繁体   English

Laravel基本控制器方法来调用过滤器

[英]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. 使用Laravel 3时遇到问题,我需要使用此laravel版本,因为这是我公司中laravel的认证版本。

So, I want to implement a protected method inside a base_controller to use this method inside all of my controllers. 因此,我想在base_controller内实现一个受保护的方法,以在所有控制器中使用此方法。

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. 在我的控制器内部,我调用此方法来验证记录的用户权限,然后,当权限为false时,我需要基本控制器调用过滤器profileValidate,该过滤器会将用户重定向到某些路由。

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;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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