Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the coder-elementor domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u262393194/domains/codestap.com/public_html/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the rank-math domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u262393194/domains/codestap.com/public_html/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the rocket domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u262393194/domains/codestap.com/public_html/wp-includes/functions.php on line 6114
Explain how Slim Framework’s Callable routes work. - Code Stap
Explain how Slim Framework’s Callable routes work.

Explain how Slim Framework’s Callable routes work.

In Slim Framework, callable routes allow you to define route handlers using PHP callables—functions or methods that are invoked when a specific route is matched. This makes the routing system in Slim highly flexible, letting you handle requests using anonymous functions, standalone functions, or methods within classes.

Here’s how callable routes work in Slim:

1. Using Anonymous Functions (Lambdas)

The most common way to define a route in Slim is by using an anonymous function as a callable. This is useful for defining inline, self-contained logic.

Example

<?php
$app->get('/hello/{name}', function ($request, $response, $args) {
    $name = $args['name'];
    $response->getBody()->write("Hello, $name");
    return $response;
});
?>
  • Here, /hello/{name} matches the route, and the anonymous function is called when the route is accessed.
  • $args contains route parameters, and you can write to the $response object.

2. Using Standalone Functions

You can define standalone functions as route handlers, and Slim will treat them as callables:

Example

<?php
function greet($request, $response, $args) {
    $name = $args['name'];
    $response->getBody()->write("Hello, $name from function!");
    return $response;
}

$app->get('/greet/{name}', 'greet');
?>
  • The function greet is treated as a callable for the route /greet/{name}.
  • Slim will automatically pass $request, $response, and $args to the function.

3. Using Class Methods (Invokable Classes)

You can use class methods as callables in Slim, including invokable classes (those with __invoke() method). This is useful for organizing logic into classes.

Invokable Class Example

Example

<?php
class GreetController {
    public function __invoke($request, $response, $args) {
        $name = $args['name'];
        $response->getBody()->write("Hello, $name from GreetController!");
        return $response;
    }
}

$app->get('/greet/{name}', GreetController::class);
?>
  • Here, GreetController is an invokable class because it defines the __invoke() method.
  • The controller class is automatically called when the /greet/{name} route is accessed.

Specific Class Method Example

You can also specify a specific method of a class by passing an array with the class and method name:

Example

<?php
class GreetController {
    public function greet($request, $response, $args) {
        $name = $args['name'];
        $response->getBody()->write("Hello, $name from GreetController method!");
        return $response;
    }
}

$app->get('/greet/{name}', [GreetController::class, 'greet']);
?>
  • This time, the greet() method of GreetController is called when the route matches.

4. Passing Dependencies with Callable Resolvers

Slim uses a callable resolver that can inject dependencies into callable routes. This is particularly useful for controllers that require external services. You can register controllers or services in the dependency container, and Slim will automatically resolve and inject them.

Example

<?php
$container->set(GreetController::class, function () {
    return new GreetController();
});

$app->get('/greet/{name}', GreetController::class);
?>
  • Slim will resolve the GreetController from the container when the route is matched.

5. Middleware Callable Example

Callables can also be used for middleware. Here’s an example of a middleware defined as a callable:

Example

<?php
$middleware = function ($request, $handler) {
    $response = $handler->handle($request);
    $response->getBody()->write(' - Middleware Processed');
    return $response;
};

$app->get('/hello/{name}', function ($request, $response, $args) {
    $response->getBody()->write("Hello, {$args['name']}");
    return $response;
})->add($middleware);
?>
  • The middleware callable modifies the response after the main route callable is executed.

Related Questions & Topics