Skip to content

Handling exceptions within controller methods #77

@mazuhl

Description

@mazuhl

Is it possible to throw an exception in a controller method and catch that in the dispatcher/router? Controller methods are called in the Dispatcher using call_user_func_array, which doesn't allow for catching exceptions.

If not, how else would you return early from a method?

class MyController {

    public function anyIndex() {
        $this->checkAccess('edit');
    }

    private function checkAccess($permission) {
        if ($userPermission != $permission) throw new Exception("You don't have permission", 401);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions