Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 组功能中的Slim(v3)访问请求_Php_Slim - Fatal编程技术网

Php 组功能中的Slim(v3)访问请求

Php 组功能中的Slim(v3)访问请求,php,slim,Php,Slim,设一个(v3)组,如: $app->group('/user/{user\u id}',函数(app$app){ //这里 $app->get('/stuff',函数(请求$Request,响应$Response){ $userId=$request->getAttribute('user_id'); //…获取用户的资料 }); $app->get('/otherstuff',函数(请求$Request,响应$Response){ $userId=$request->getAttribute(

设一个(v3)组,如:

$app->group('/user/{user\u id}',函数(app$app){
//这里
$app->get('/stuff',函数(请求$Request,响应$Response){
$userId=$request->getAttribute('user_id');
//…获取用户的资料
});
$app->get('/otherstuff',函数(请求$Request,响应$Response){
$userId=$request->getAttribute('user_id');//重复!
//…获取用户的其他信息
});
});

是否有方法访问组函数中的
$request
(以及URL参数)以避免每个函数中的重复?

您可以通过容器访问
组()
方法中的
$request
对象

$req = $app->getContainer()->get('request');
但是当您使用
getAttribute()
方法时,将得到
NULL
。在您的情况下,不应使用从容器检索的请求,因为请求的状态可以在必要时更改

和对象是不可变的。属性可以根据获取请求对象的时间而变化。完成的请求对象将在最后的操作中移交。所以,在行动中使用它

然而,您仍然可以获取路径并获取
user\u id
的URL参数

$path = $req->getUri()->getPath();
$user_id = explode('/', $path)[2];
话虽如此,
group()
方法旨在帮助您将路由组织到逻辑组中。如果你想干涸你的代码,创建一个包含你需要的动作的类。例如:

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Container\ContainerInterface;

class UserController
{
    protected $container;
    protected $userId;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
        $this->userId = $this->user();
    }

    public function stuff(Request $request, Response $response)
    {
        // Use $this->userId here
    }

    public function otherStuff(Request $request, Response $response)
    {
        // Use $this->userId here
    }

    protected function user()
    {
        $path = $this->container->get('request')->getUri()->getPath();
        $parts = explode('/', $path);
        return $parts[2] ?? '';
    }
}
然后,更新您的路线:

$app->group('/user/{user_id}', function(App $app) {
    $app->get('/stuff', UserController::class . ':stuff');
    $app->get('/otherstuff', UserController::class . ':otherStuff');
});
或者,您可以从路线参数中获取
$user\u id

class UserController
{
    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function stuff(Request $request, Response $response, array $args)
    {
        // Use $args['user_id'] here
    }

    public function otherStuff(Request $request, Response $response, array $args)
    {
        // Use $args['user_id'] here
    }
}
或者,如果您更喜欢闭包:

$app->group('/user/{user_id}', function(App $app) {
    $app->get('/stuff', function(Request $request, Response $response, array $args) {
         // Use $args['user_id'] here
    });

    $app->get('/otherstuff', function(Request $request, Response $response, array $args) {
         // Use $args['user_id'] here
    });
});

您可以通过容器访问
group()
方法中的
$request
对象

$req = $app->getContainer()->get('request');
但是当您使用
getAttribute()
方法时,将得到
NULL
。在您的情况下,不应使用从容器检索的请求,因为请求的状态可以在必要时更改

和对象是不可变的。属性可以根据获取请求对象的时间而变化。完成的请求对象将在最后的操作中移交。所以,在行动中使用它

然而,您仍然可以获取路径并获取
user\u id
的URL参数

$path = $req->getUri()->getPath();
$user_id = explode('/', $path)[2];
话虽如此,
group()
方法旨在帮助您将路由组织到逻辑组中。如果你想干涸你的代码,创建一个包含你需要的动作的类。例如:

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Container\ContainerInterface;

class UserController
{
    protected $container;
    protected $userId;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
        $this->userId = $this->user();
    }

    public function stuff(Request $request, Response $response)
    {
        // Use $this->userId here
    }

    public function otherStuff(Request $request, Response $response)
    {
        // Use $this->userId here
    }

    protected function user()
    {
        $path = $this->container->get('request')->getUri()->getPath();
        $parts = explode('/', $path);
        return $parts[2] ?? '';
    }
}
然后,更新您的路线:

$app->group('/user/{user_id}', function(App $app) {
    $app->get('/stuff', UserController::class . ':stuff');
    $app->get('/otherstuff', UserController::class . ':otherStuff');
});
或者,您可以从路线参数中获取
$user\u id

class UserController
{
    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function stuff(Request $request, Response $response, array $args)
    {
        // Use $args['user_id'] here
    }

    public function otherStuff(Request $request, Response $response, array $args)
    {
        // Use $args['user_id'] here
    }
}
或者,如果您更喜欢闭包:

$app->group('/user/{user_id}', function(App $app) {
    $app->get('/stuff', function(Request $request, Response $response, array $args) {
         // Use $args['user_id'] here
    });

    $app->get('/otherstuff', function(Request $request, Response $response, array $args) {
         // Use $args['user_id'] here
    });
});

我认为这将帮助你实现你想要的。我认为这将帮助你实现你想要的。非常感谢。我应该使用类而不是闭包。非常感谢。我应该使用类而不是闭包。