doulao3905 2019-06-10 11:40
浏览 406

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.


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


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

    //Init RequestContext object
    $context = new RequestContext();
    //generate the context from user passed $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);

        $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 kubeadm部署k8s出错
  • ¥15 Abaqus打不开cae文件怎么办?
  • ¥20 双系统开机引导中windows系统消失问题?
  • ¥15 小程序准备上线,软件开发公司需要提供哪些资料给甲方
  • ¥15 关于生产日期批次退货退款,库存回退的问题
  • ¥15 手机应用的时间可以修改吗
  • ¥15 docker 运行OPEN-webui异常
  • ¥15 麒麟系统如何删除光盘刻录痕迹
  • ¥15 recipe通过gem协议传的是什么
  • ¥15 TS2307: Cannot find module 'cc'.