Skip to content

Routing: accept [Controller::class, 'method'] as the route action

Latest

Choose a tag to compare

@macropay-solutions macropay-solutions released this 19 Nov 21:30
353b501
// Array Callable Action
$router->get('users', [UserController::class, 'index']);

// Array Callable As "uses"
$router->get('users', [
    'uses' => [UserController::class, 'index'],
    'as' => 'users.index',
]);

The array callable must be [Controller::class, 'method']: a two-item list or a uses array of another shape (empty class, invalid method name, string keys) throws an InvalidArgumentException when the route is added. It is stored as the Controller@method string, so route caching works the same for both forms.

NOTE: The group namespace is prepended to controller mapping strings only. An array callable such as [UserController::class, 'index'] already holds the fully qualified class name, so the namespace is not prepended to it.