Laravel/PHP - returning/redirecting from child class

寵の児 提交于 2019-12-06 07:44:34

The redirect will happen only if VolunteersController::index() will return a "Redirect". It does not do so in your code.

It would, if you had

class VolunteersController extends \BaseController
{
    public function index()
    {
        if ($res = $this->checkForRoles(['admin'])) return $res;
        //list some secret stuff for admin
    }
}

I would move the logic to a filter, which will allow the Redirect to function properly. This is the kind of thing filters were designed for.

If you need to pass multiple roles to the filter, instead of passing an array to the filter (which Laravel won't allow), use a delimiter like "+" and then explode the parameter in the filter to simulate passing an array.

For example, your route would be:

Route::get('volunteer', array(
    'before' => 'roles:admin+author', 
    'uses' => 'VolunteersController@index'
));

...and then your filter can easily convert the multiple roles into an array:

Route::filter('roles', function($route, $request, $roles)
{
    $roles = explode('+', $roles);
    // 'admin+author' becomes ['admin', 'author'];
    // continue with your checkForRoles function from above:
    foreach ($roles as $role) {
        if (!(Auth::user()->hasRole($role))) {
            return Redirect::to('/');
        }
    }
}

Then you can remove the logic from the BaseController.

Alternatively, you can pass multiple parameters to a filter as a comma-delimited list. So if you called your route with 'before' => 'roles:admin,author', you could access them in your filter using func_get_args():

Route::filter('roles', function($route, $request, $roles)
{
    $roles = array_slice(func_get_args(), 2); // remove $route and $request
    //...continue as above.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!