Controllers in Symfony framework (Lesson 5. Controllers)

After creating bundle (in the previous lesson), it's time to start describing the application logic. For this, we need to understand the principles of working with controllers in Symfony framework. (You can read about what a controller is as a category of MVC here). The simplest controller looks like this:

// src/Acme/HelloBundle/Controller/HelloController.php
namespace AcmeHelloBundleController;

use SymfonyBundleFrameworkBundleControllerController;

class HelloController extends Controller { public function indexAction($name) { //... } }

Each controller in your application should have a namespace directory_your_bundleController (line 2), otherwise it will not be recognized as a controller. Use makes the base controller class Symfony framework available to us. Our controller class is created as a descendant of the base controller class Symfony framework. This is not required, but when writing a complex project, you need to use Symfony's functionality.

Pay attention to the controller name: it must first contain the name, and then the prefix Controller.

The indexAction method is what will handle the input-output and, as a result, provide information to the end user. Again, the name must contain the prefix Action - as in the example.

The controller's Action can have (or not have =)) input parameters. These parameters are determined by routing (about this in the next lesson). For now, it is important for us to remember that the order of arguments does not matter - Symfony took care of this.

Next, let's consider some specific tasks of the controller. For example, redirection is done as follows:

public function indexAction()
 {
  return $this->redirect($this->generateUrl('homepage'));
 }
By default, the redirect is 302. If you want 301, specify it as the second argument:
public function indexAction()
 {
  return $this->redirect($this->generateUrl('homepage'), 301);
 }
If you want to make a more "tricky" redirect - to another controller (as a result of which the user will not even notice the substitution), then
public function indexAction($name)
 {
  $response = $this->forward('AcmeHelloBundle:Hello:fancy', array(
  'name' => $name, 
  'color' => 'green',
));

return $response; }

And the most obvious task of the controller is rendering a template (template):
use SymfonyComponentHttpFoundationResponse;
$content = $this->renderView(
'AcmeHelloBundle:Hello:index.html.twig',
array('name' => $name)
);
return new Response($content);
We will talk about templates a little later. For now, it is important that the controller passes parameters to the template and renders it.

The 404 page is called as follows:

throw $this->createNotFoundException('The product does not exist');
Another interesting feature of the controller is working with session:
$session = $this->getRequest()->getSession();

$session->set('foo', 'bar');

$foo = $session->get('foo');

Symfony framework has an interesting feature of "flash messages" that are stored in the session only until the next request:
$this->get('session')->getFlashBag()->add('notice', 'Your changes were saved!');
Similar things are convenient to use for registration/authentication forms.

On this optimistic note, the introduction to controllers in Symfony framework can be considered complete =)

Read more on the website symfony.com