Symfony 1 - MVC - How does it work?

In this tutorial I will show you how the symfony framework works with the MVC pattern.

Each application, for example the frontend, has one or several modules.

Each module has one or several Controllers and Views.

The first Controller of a module is in the actions directory and the actions.class.php file that can be found there:

symfony > apps > frontend > modules > moduleName > actions > actions.class.php

The Views in the templates directory and they are suffixed by the Success word, except for the _form file.

symfony > apps > frontend > modules > moduleName > templates > *Success.php

The general template, named layout.php is in the frontend directory:

symfony > apps > frontend > templates > layout.php

This file is the general layout of the application
Each link with a module and an action - http://project/module/action - will be redirected in the general layout in the $sf_content variable:

echo $sf_content;

But each module can have its own display.

The global setting is in the view.yml file:

symfony > apps > frontend > config > view.yml

In this file we can manage files to include in all views of the frontend application.

But each module can have their own style sheets for example.

It can be defined in the config directory of each module.

symfony > apps > frontend > modules > moduleName > config > view.yml

As this directory does not exist, we have to create it and place a new file named view.yml inside.

It's interesting because we can manage all the application for a general design and each module separately for a specific render surprise

Another thing to know is how the Controller talks with the Views.

Each Controller have to create methods starting with the execute word.

Example:   

// modules/moduleName/actions/actions.class.php
    public function executeFoobar(sfWebRequest $request)
    {
        $this->myvariable = 'lol';
        $this->myarray = array('bar', 'baz');
    }

We can now catch these foo and bar values directly from the View, like this:

// modules/moduleName/templates/foobarSuccess.php
<div><?php echo $myvariable ?></div>
<div><?php echo $myarray[0] ?></div>
<div><?php echo $myarray[1] ?></div>

The render:

// http://localhost/modulename/foobar
lol
bar
baz

 

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.