How to stop execution and redirect?

Posted 1 year ago by brysonreece

I currently have a controller method that calls

Auth::user()->checkPermissions('admin', Auth::user()->currentTeam);

as a single statement.

The contents of this method are

// User Model
public function checkCurrentPermissions(string $role, Team $team) {
    // The $role string is the role value, we want the key
    $role = array_search($role, Team::ROLES);

    // If the User instance does not have permissions, redirect back
    if (!$this->hasTeamRolePermissions($role, $team) || !$this->currentTeam->is($team)) {
        return Redirect::back();
    }
}

My question is that since the Redirect response gets returned and nothing is done with it, code execution continues. Is there any way to stop execution and redirect back, regardless of where I am in the controller method?

Note, I could always do

// User Model
public function checkCurrentPermissions(string $role, Team $team) {
    // The $role string is the role value, we want the key
    $role = array_search($role, Team::ROLES);

    // If the User instance does not have permissions, redirect back
    if (!$this->hasTeamRolePermissions($role, $team) || !$this->currentTeam->is($team)) {
        return Redirect::back();
    }
    else {
        return null;
    }
}

and check if the result of the method call is null, however I'm trying to keep the

Auth::user()->checkPermissions('admin', Auth::user()->currentTeam);

to one line.

Please sign in or create an account to participate in this conversation.