phpsymfonysymfony-http-foundation

How does the Symfony/Http-foundation component handles Http request and response


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.


Solution

  • 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.