Symfony Startup Process Detailed learning

Source: Internet
Author: User

To understand the Symfony startup process, you must start with the startup file (in developer mode).

<?php
/*
* web/app_dev.php
*/
$loader = require_once __dir__. ' /.. /app/bootstrap.php.cache ';
Debug::enable ();
Require_once __dir__. ' /.. /app/appkernel.php ';
Initialize Appkernel
$kernel = new Appkernel (' Dev ', true);
Kernel boot, load cache
$kernel->loadclasscache ();
Use some information to construct the request object (such as $_get $_post, etc.)
$request = Request::createfromglobals ();
Convert the Request object to a response object through the Symfony kernel
$response = $kernel->handle ($request);
Output Response Object
$response->send ();
Perform some time-consuming operations such as sending mail
$kernel->terminate ($request, $response);
?>

From the above view, the basic idea is the client request, symfony the kernel through the response request, return the response of the response object, then Symfony is how to execute the response request? Below is the official document to see


Incoming requests is interpreted by the routing and passed to controller functions that return Response objects. Each "page" of your site was defined in a routing configuration file, maps different URLs to different PHP functions. The job of each PHP function, called a controller, are to use information from the Request–along with many other tools Sy Mfony makes available–to Create and return a Response object. In other words, the controller is where your code Goes:it's where you interpret the request and create a response.

To get a general idea of the workflow, let's look at how to obtain the Request object, and call the Createrequestfromfactory method within the Createfromglobals method.

These parameters are all passed through the HTTP request, using the Hyper global variable self::createrequestfromfactory ($_get, $_post, Array (), $_cookie, $_files, $server);

A Request object is then instantiated by a constructor to return.

<?php
private static function createrequestfromfactory (array $query = Array (), array $request = Array (), array $attributes = arr Ay (), array $cookies = Array (), array $files = Array (), array $server = Array (), $content = null)
{
if (self:: $requestFactory) {
$request = Call_user_func (self:: $requestFactory, $query, $request, $attributes, $cookies, $files, $server, $content);
if (! $request instanceof Self) {throw new \logicexception (' The "Request factory must return an instance of Symfony\componen T\httpfoundation\request. ');
}
return $request;
}
return new Static ($query, $request, $attributes, $cookies, $files, $server, $content);
}
?>

Createrequestfromfactory, as the name implies, creates a request object through a factory with the $requestfactory attribute in the request class, if you instantiate a Request object class by yourself, and then through the Setfactory () Functions are set under the factory, which can be instantiated by customizing, or Static. This returns a Request object.

The difference between the new static () above and the new self (). Here is a foreigner's explanation of the self refers to the same class whose method, the new operation takes place in.static in PHP 5.3 is late static B Indings refers to whatever class in the hierarchy which the method on. In the following example, B inherits both methods from A. Self is bound to a because it's defined in a ' s implementation of The first method, whereas static is bound to the called Class (also see Get_called_class ()).

In fact, through an example, it's obvious

<?php
Class A {
public static function Get_self () {
return new self ();
}

public static function Get_static () {
return new static ();
}
}

Class B extends A {}

Echo Get_class (B::get_self ()); A
Echo Get_class (B::get_static ()); B
Echo Get_class (A::get_static ()); A
?>

It's easy to understand by example.

In the controller we can get the parameters of the phase through the request object, and after processing the data, we return a response object. So how do you return a response object?

Let us enter the $kernel->handle ($request);

<?php
/**
* {@inheritdoc}
*/
Public function handle (Request $request, $type = httpkernelinterface::master_request, $catch = True)
{
Symfony kernel starts only once
if (false = = = $this->booted) {
Register all the Bundles
Initialize container, load, cache configuration data and route data, etc.
$this->boot ();
}
Kernel processing Request
return $this->gethttpkernel ()->handle ($request, $type, $catch);
}
?> <?php
/**
* Boots the current kernel.
*/
Public Function boot ()
{
if (true = = = $this->booted) {
Return
}
if ($this->loadclasscache) {
$this->doloadclasscache ($this->loadclasscache[0], $this->loadclasscache[1]);
}
Inside Call Kernel->registerbundles ()
$this->initializebundles ();
Initialize container, including loading configuration information, compiling information, etc.
Loading of the core components of the Symfony2
$this->initializecontainer ();
Inject each bundle into container so that its contents can be used
and start the bundle
foreach ($this->getbundles () as $bundle) {
$bundle->setcontainer ($this->container);
$bundle->boot ();
}
$this->booted = true;
}
?> <?php
The kernel processes the request, and here is the main message, which is to execute the corresponding controller by request, render the view
Private Function Handleraw (Request $request, $type = self::master_request)
{
$this->requeststack->push ($request);
Request Object
$event = new Getresponseevent ($this, $request, $type);
$this->dispatcher->dispatch (Kernelevents::request, $event);
if ($event->hasresponse ()) {return $this->filterresponse ($event->getresponse (), $request, $type);
}
Controller loaded in response
if (false = = = $controller = $this->resolver->getcontroller ($request)) {throw new Notfoundhttpexception (sprintf (' Unable to find the controller for path '%s '. The route is wrongly configured. ', $request->getpathinfo ()));
}
$event = new Filtercontrollerevent ($this, $controller, $request, $type);
$this->dispatcher->dispatch (Kernelevents::controller, $event);
$controller = $event->getcontroller ();
Parameters of the Controller
$arguments = $this->resolver->getarguments ($request, $controller);
Call Controller
$response = Call_user_func_array ($controller, $arguments);
View
if (! $response instanceof Response) {$event = new getresponseforcontrollerresultevent ($this, $request, $type, $response) ; $this->dispatcher->dispatch (Kernelevents::view, $event), if ($event->hasresponse ()) {$response = $event- >getresponse ();} if (! $response instanceof Response) {$msg = sprintf (' The controller must return a response (%s given). ', $this->vartos Tring ($response)); if (null = = = $response) {$msg. = ' Did you forget to add a return statement somewhere in your controller? ';} throw new \ Logicexception ($msg);}
}
return $this->filterresponse ($response, $request, $type);
}
?>

A response object is returned and sent to the client, so we can see its contents.

Symfony Startup Process Detailed learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.