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 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题