doulao3905 2019-06-10 11:40
浏览 404
已采纳

Symfony / Http-foundation组件如何处理Http请求和响应

I've been assigned to a project which needs include the Symfony components in order to re-organize its business logic. However, I got confused by looking at the Symfony HTTP-foundation document. Hope someone here could help me explain a bit how this component deals with users Http requests and responses.

Basically, what I did in the project was:

  1. having a PHP page creates the Request object with requests' URL and method

  2. Using an ApiRouter to direct the code to the desired controller

  3. Within the controller, it will send the HTTP request to the server and convert responses as a Symfony Response object base on the Request URL.

location.php

class GetLocation
{
public function __construct($q)
   {
    $request = Request::create('location?v=full&q=' . 
    urlencode($q), 'GET'); //simulates a request using the url
    $rest_api = new RestApi();  //passing the request to api router
    $rest_api->apiRouter($request);
    }
}

ApiRouter.php

    //location router
       $location_route = new Route(
            '/location',
            ['controller' => 'LocationController']
        );
       $api_routes->add('location_route', $location_route);

    //Init RequestContext object
    $context = new RequestContext();
    //generate the context from user passed $request
    $context->fromRequest($request);

    // Init UrlMatcher object matches the url path with router
    // Find the current route and returns an array of attributes
    $matcher = new UrlMatcher($api_routes, $context);
    try {
        $parameters = $matcher->match($request->getPathInfo());
        extract($parameters, EXTR_SKIP);
        ob_start();

        $response = new Response(ob_get_clean());
    } catch (ResourceNotFoundException $exception) {
        $response = new Response('Not Found', 404);
    } catch (Exception $exception) {
        $response = new Response('An error occurred', 500);
    }

What I hope to know is whether my understanding is correct regards to the logic or not? And what does the method Request:createFromGlobal means, what are the differences between this and Request:create(URL)

Please do let me know if my question needs to be more specific.

  • 写回答

1条回答 默认 最新

  • doulin1867 2019-06-10 18:30
    关注

    First the easier of your questions:

    Request::createFromGlobals() will create a request basedon some PHP global variables, e.g. $_SERVER, $_GET and $_POST, meaning it will create a request from the current request we are "in", i.e. the user request that triggered our application. Request::create() on the other hand will build a "new" request without this context being applied, meaning you have to pass certain info, like the path and HTTP-method yourself.

    Now regarding your code and whether it will work. Short answer is, probably not. In GetLocation you create both a new request and a new router and inside the controller you create a route, that will then be added to the router. Meaning unless the controller code is executed before GetLocation, the route will not be available in the router, meaning the controller is never called.

    You might want to look into the series: Create your own PHP Framework inside the symfony docs, especially the parts from The HttpFoundation Component onwards. Hopefully this will clear things up for you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 matlab求解平差
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办