简体   繁体   English

在Slim v3中间件中访问参数

[英]Accessing parameters in Slim v3 middleware

According to http://www.slimframework.com/docs/concepts/middleware.html , route middleware is added as follows. 根据http://www.slimframework.com/docs/concepts/middleware.html ,路由中间件添加如下。

<?php
$app = new \Slim\App();

$mw = function ($request, $response, $next) {
    // How is $arg accessed?
    $response->getBody()->write('BEFORE');
    $response = $next($request, $response);
    $response->getBody()->write('AFTER');

    return $response;
};

$app->get('/ticket/{id}', function ($request, $response, $args) {
    $response->getBody()->write(' Hello ');
    // $arg will be ['id'=>123]
    return $response;
})->add($mw);

$app->run();

$arg will be the parameters array. $arg将是参数数组。 How can this be accessed in the middleware? 如何在中间件中进行访问?

http://help.slimframework.com/discussions/questions/31-how-pass-route-pram-to-middleware shows an approach to do so, however, appears to be an earlier release of Slim, and errors Fatal error: Call to undefined method Slim\\\\Route::getParams() . http://help.slimframework.com/discussions/questions/31-how-pass-route-pram-to-middleware显示了一种这样做的方法,但是,它似乎是Slim的早期版本,并且出现Fatal error: Call to undefined method Slim\\\\Route::getParams()

Route Params can be accessed through the routeInfo Attribute on Request, as such 可以通过Request上的routeInfo属性来访问Route Params,例如

$app->get('/hello/{name}', \HelloWorld::class . ':execute')
    ->add(function ($request, $response, $next) {
        var_dump($request->getAttribute('routeInfo')[2]['name']);exit();
        return $next($request, $response);
    });

This is noted in this Github issue 这在Github问题中有所记录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM