Slim: Get Params in Middleware and Return Object

Created on 13 Dec 2016  路  7Comments  路  Source: slimphp/Slim

Hi guys, I'm trying to get a String argument from route, proccess it in high level middleware and then return to the component itself

This is the MiddleWare

$dateMw = function ($request, $response, $next) {
    $lastSyncDate = $request->getAttribute('routeInfo')[2]['lastSyncDate'];
    $date = new \DateTime();
    $date->setTimeStamp(strtotime($lastSyncDate));
    return $next($request, $response,$date);
};

But in my element the third param remains expecting $args (an array)

I like

$app->get('/{lastSyncDate}', function(Request $request, Response $response, DateTime $lastSyncDate){
        $referenceList = new ReferenceList($this->db);
        return $response->withJson(['REFERENCE' => $referenceList->getUpdates($lastSyncDate)->values()]);
    })->add($dateMw);

How i can do this?

Most helpful comment

@liko28s: This can be done in middleware if the determineRouteBeforeAppMiddleware setting is set to true:

$app->add(function ($request, $response, $next) {
    // get the route from the request
    $route = $request->getAttribute('route');

    if (!$route) {
        // no route matched
        return $next($request, $response);
    }

    // get the arguments and alter them
    $args = $route->getArguments();
    $args['foo'] = 'bar';

    // update the request with the new arguments to route
    $route->setArguments($args);
    $request = $request->withAttribute('route', $route);

    // also update the routeInfo attribute so that we are consistent
    $routeInfo = $request->getAttribute('routeInfo');
    $routeInfo[2] = $args;
    $request = $request->withAttribute('route', $route);

    // continue
    return $next($request, $response);
});

All 7 comments

I think what you are looking for is here...

https://github.com/PHP-DI/Slim-Bridge

You have no (easy) control over the parameters passed to middleware or the route callable. You can change the data in the $args though by setting data into the request's attributes.

I think that if you have active the determineRouteBeforeAppMiddleware config set to true You should be able to overwrite the args In Middleware an then pass to the next as you want

$middleware = function ($request, $response, $next) {
    $args = $request->getArgs();
    //Do what you like with args
    $customOrProcessedArgs = new Custom();
    return $next($request, $response, $customOrProcessedArgs);
};
$app->get('/', function(Request $request, Response $response, Custom $args){
    //Do what you like with customized args
        return $response;
    })->add($middleware);

At least receive or pass additional parameters in $next

@liko28s: This can be done in middleware if the determineRouteBeforeAppMiddleware setting is set to true:

$app->add(function ($request, $response, $next) {
    // get the route from the request
    $route = $request->getAttribute('route');

    if (!$route) {
        // no route matched
        return $next($request, $response);
    }

    // get the arguments and alter them
    $args = $route->getArguments();
    $args['foo'] = 'bar';

    // update the request with the new arguments to route
    $route->setArguments($args);
    $request = $request->withAttribute('route', $route);

    // also update the routeInfo attribute so that we are consistent
    $routeInfo = $request->getAttribute('routeInfo');
    $routeInfo[2] = $args;
    $request = $request->withAttribute('route', $route);

    // continue
    return $next($request, $response);
});

Out of interest, why the setting determineRouteBeforeAppMiddleware = true isn't set by default?

Out of interest, why the setting determineRouteBeforeAppMiddleware = true isn't set by default?

The setting was added after 3.0 was released, so the default is false to maintain BC.

Thank you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JamesTheHacker picture JamesTheHacker  路  3Comments

lwiwala picture lwiwala  路  5Comments

jwyuen picture jwyuen  路  5Comments

jaklimoff picture jaklimoff  路  4Comments

codeguy picture codeguy  路  3Comments