Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hey@bryson.cc's avatar

How to stop execution and redirect?

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.

0 likes
1 reply
hey@bryson.cc's avatar
Level 3

I ended up solving this by creating a custom exception, throwing that exception in the checkCurrentPermissionsmethod, and then placing my Alert and Redirect in a

if ($exception instanceof MyCustomException) {
    // Alert
    // return Redirect
}

statement within my app/Exceptions/Handler.php render method.

Please or to participate in this conversation.