Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RequestResponseArgs strategy does not pass $args as an array in nested group routes. #3372

Open
qdmcoding opened this issue Feb 22, 2025 · 0 comments

Comments

@qdmcoding
Copy link

qdmcoding commented Feb 22, 2025

Expected Behavior:

When using RequestResponseArgs as the default invocation strategy, the $args parameter should always be an array, regardless of whether the route is inside a group.

Current Behavior:

When using RequestResponseArgs, $args is passed as a string instead of an array when the route is nested inside a group.

Steps to Reproduce:

Install Slim 4.12.0.
Set RequestResponseArgs as the default invocation strategy.
Define a nested route inside a group.
Access the route and observe that $args is passed as a string.

Minimal Reproducible Example:

<?php

require __DIR__ . '/vendor/autoload.php';

use Slim\Factory\AppFactory;
use Slim\Interfaces\RouteCollectorProxyInterface as RouteCollectorProxy;
use Slim\Handlers\Strategies\RequestResponseArgs;

$app = AppFactory::create();

// Set RequestResponseArgs strategy
$collector = $app->getRouteCollector();
$collector->setDefaultInvocationStrategy(new RequestResponseArgs());

$app->group('/goods', function (RouteCollectorProxy $group) {
    $group->get('/{name}', function ($request, $response, array $args) {
        var_dump($args); // Expect array, but it's a string
        return $response;
    });
});

$app->run();

Error Message:

Fatal error: Uncaught TypeError: Closure::{closure}(): Argument #3 ($args) must be of type array, string given

Possible Cause:

It seems that when {name} is inside a nested group, Slim fails to properly process the route parameters and passes them as a string instead of an array.

Workarounds:

  1. Using RequestResponse instead of RequestResponseArgs

$collector->setDefaultInvocationStrategy(new \Slim\Handlers\Strategies\RequestResponse());

  1. Using $request->getAttribute('name') instead of $args['name']

$name = $request->getAttribute('name');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant