简体   繁体   English

访问路由组中的请求对象 - Slim Framework

[英]access Request object in route group - Slim Framework

I´m using Slim Framework and I need to access the Request object in a group so I can create objects and use them in the routes 我正在使用Slim Framework,我需要访问组中的Request对象,这样我就可以创建对象并在路由中使用它们

$app->group('/my-group', function (App $app) {
    $id = $app->request->getAttribute('id')); // this doesn´t work
    $user = some_method_to_find_user($id)

    $app->get('/route-1', function () use ($user) {
        var_dump($user);
    }
}

how to access the Request object? 如何访问Request对象?

I also tried with 我也尝试过

$app->group('/api', function (App $app, Request $request) {
    $id = $request->getAttribute('id')); // this doesn´t work

but is giving me this error: 但是给了我这个错误:

 Uncaught ArgumentCountError: Too few arguments to function Closure::{closure}(),

groups are designed only to create Router (and by name - route groups) 组仅设计用于创建路由器(以及名称 - 路由组)

you should use and access request only in Middlewares and Controllers (ie. clousures used as route) 你应该只在中间件和控制器中使用和访问请求(即用作路由的clousures)

during Group call the request might not be determined yet 在群组通话期间,可能尚未确定请求

application is building entire router (groups, each route,..) and then by requested URI router will fill and pass Request to your middlewares and to route stack 应用程序正在构建整个路由器(组,每个路由,...),然后通过请求的URI路由器将填充并传递请求到您的中间件和路由堆栈

use Slim Documentation, it is full of valid examples: 使用Slim Documentation,它充满了有效的例子:

http://www.slimframework.com/docs/v3/objects/router.html#how-to-create-routes http://www.slimframework.com/docs/v3/objects/router.html#how-to-create-routes

$app->get('/books/{id}', function ($request, $response, $args) {
    // Show book identified by $args['id']
});

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

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