douhui8163 2017-01-04 06:34
浏览 99

在路由器类Slim Framework 3中的构造函数中使用Request,Response

I am using Slim Framework for my application. I am using routes. All is working fine. But now I want to do some pre-process working under my constructor on Request and Response.

So that I should not rework on every function of the class. Like getting host and token in every function. I am using middle-ware for many pre-process. But I also want to do some work in class constructor. When I am trying to access request and response interface in constructor, It is showing the error, Please show me the right way of using Request and Response in a class constructor. Will I have to append $app, or will need to work with container.

If it can be done without help of middleware, It will be great for me.

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;    
$app->group('/products', function() {
    new \Products($this);
});

And I have a class called Products.

class Products
{
    public function __construct($app)
    {
        $app->map(['GET','POST'], '/createupdate', array($this, 'createupdate'));    
        //I want to use Request and Response here in constructor. But it is showing error. 
        $this->req_data['request_token'] = $request->getAttribute('request_token');
    }
    public function createupdate($request, $response, $args) {      
    //This is working fine.
    $this->req_data['request_token'] = $request->getAttribute('request_token');
    }
}
  • 写回答

2条回答 默认 最新

  • dongpang9573 2017-01-04 07:18
    关注

    When you really want to do this, then you could get the request/response object from the container.

    class Products
    {
        public function __construct($app)
        {
            $app->map(['GET','POST'], '/createupdate', array($this, 'createupdate'));
    
            $request = $app->getContainer()->get('request');
            $this->req_data['request_token'] = $request->getAttribute('request_token');     
        }
    // [..]
    }
    

    But, also this will not make much difference $this->req_data['request_token'] is nearly as long as $request->getAttribute('request_token'); so you should use this inside the code.

    Note: I expect you to set this attribute already inside middleware, so it may not be available here, because first the container will create a new request object and second cause the middleware is not run when php executes your constructor code.

    When you now still want to use $this->req_data['request_token'] inside your class then you should do this:

    $products = new \Products();
    
    $app->group('/products', function() use ($products) {
        $products->addRoutes($this);
    })->add($products); // add the class as middleware as well to set there the class attributes (__invoke function)
    
    class Products
    {
        public function addRoutes($app)
        {
            $app->map(['GET','POST'], '/createupdate', array($this, 'createupdate'));
        }
    
        public function __invoke($request, $response, $next) // this is middleware function
        {
            $this->req_data['request_token'] = $request->getAttribute('request_token');
    
            return $next($request, $response); // next in this example would be your route function like createupdate()
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题