Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Workflow with Symfony2?

Tags:

symfony

I am new to symfony2. I started it with some tutorials and then started building SYMBLOG. I have understood it and i am able to add or change the functionality. I have a bit confusion in the workflow, I mean How the files work together to render a page or to produce an output. Can anyone explain me in detail from the beginning how this flow goes on in symfony2. starting from the user request say user enters a url till the symfony2 displays results. please include the routing.yml in the flow. ?

like image 453
ScoRpion Avatar asked Jan 31 '12 05:01

ScoRpion


1 Answers

You should check out this link. Symfony - the big picture

It explains in detail all the steps involved from the time you enter the URL in the browser to the page getting rendered.

Basically all requests go to a Front Controller. Its job is to route the requests to the appropriate controller code. It does this with the help of the routes defined in the app/config/routing.yml file. The controllers which are defined in src/<BundleName>/Controller/<name> perform some business logic, like getting data from the Model (Repository) and send that information to the View (Templates). The views are simply HTML code. Symfony uses a templating engine called Twig. Instead of including <?php ... ?> blocks in the HTML code, Symfony passes the data from the controller and it can be easily used inside the view within Twig {% %} or {{ }} blocks.

Simply put, here is the workflow:

  1. Browser sends Request
  2. Request received in front controller web/app_dev.php or web/app.php
  3. Front controller checks the routes defined in app/config/routing.yml and sends the request to the appropriate controller defined in src/<BundleName>/Controller/<controller_name>
  4. Controller prepares the content that is needed in the HTML (Example - query the database from src/<BundleName>/Repository) and sends the information to the View - src/Resources/views/<twig file name>
  5. The view creates the HTML and sends it back to the controller
  6. The controller creates an HTTP response and sends it back to the browser

There are things like the app/AppKernel that come in between but I have skipped it.

Here are the useful excerpts from the link provided above:

URL:

http://localhost/Symfony/web/app_dev.php/demo/hello/Fabien

What's going on here? Let's dissect the URL: app_dev.php: This is a front controller. It is the unique entry point of the application and it responds to all user requests; /demo/hello/Fabien: This is the virtual path to the resource the user wants to access. Your responsibility as a developer is to write the code that maps the user's request (/demo/hello/Fabien) to the resource associated with it (the Hello Fabien! HTML page).

Routing:

Symfony2 routes the request to the code that handles it by trying to match the requested URL against some configured patterns. By default, these patterns (called routes) are defined in the app/config/routing.yml configuration file. When you're in the dev environment - indicated by the app_dev.php front controller - the app/config/routing_dev.yml configuration file is also loaded. In the Standard Edition, the routes to these "demo" pages are placed in that file:

_welcome:
pattern:  /
defaults: { _controller: AcmeDemoBundle:Welcome:index }

Controller:

Symfony2 chooses the controller based on the _controller value from the routing configuration: AcmeDemoBundle:Welcome:index. This string is the controller logical name, and it references the indexAction method from the Acme\DemoBundle\Controller\WelcomeController class:

class WelcomeController extends Controller
{
    public function indexAction()
    {
        return $this->render('AcmeDemoBundle:Welcome:index.html.twig');
    }
}

View:

The controller renders the src/Acme/DemoBundle/Resources/views/Demo/hello.html.twig template

{% extends "AcmeDemoBundle::layout.html.twig" %}

{% block title "Hello " ~ name %}

{% block content %}
    <h1>Hello {{ name }}!</h1>
{% endblock %}

You may also want to check out the Symfony2 architecture

like image 146
Amit Avatar answered Sep 30 '22 12:09

Amit