Creare controller - Tutorial Symfony

Postat la Sat 20 June 2015 in tutoriale, symfony 2

Contrller-ul e partea de cod ce preia informatiile dintro cerere HTTP (Request) si genereaza un raspuns HTTP (obiect Response). Acest raspuns poate fi o pagina HTML, un document XML sau un obiect JSOn serializat, o imagine, o eroare 404 sau ce ne dorim. Controller-ul contine logica necesara generarii paginii de raspuns dorite.

Un controller poate fi generat prin 2 metode: manual sau cu utilitarul app/console. Ultima alternativa ne ajuta la generarea metodelor incluse in controller

php app/console generate:controller

Every page, and even sections of a page, are rendered by a controller.
This command helps you generate them easily.

First, you need to give the controller name you want to generate.
You must use the shortcut notation like AcmeBlogBundle:Post

Controller name: TutorialTestBundle:Pagina

Determine the format to use for the routing.

Routing format (php, xml, yml, annotation) [annotation]: yml

Determine the format to use for templating.

Template format (twig, php) [twig]:

Instead of starting with a blank controller, you can add some actions now. An action
is a PHP function or method that executes, for example, when a given route is matched.
Actions should be suffixed by Action.


New action name (press <return> to stop adding actions): indexAction
Action route [/index]: /
Templatename (optional) [TutorialTestBundle:Pagina:index.html.twig]:

New action name (press <return> to stop adding actions): newAction
Action route [/new]:
Templatename (optional) [TutorialTestBundle:Pagina:new.html.twig]:

New action name (press <return> to stop adding actions): editAction
Action route [/edit]: {id}/edit
Templatename (optional) [TutorialTestBundle:Pagina:edit.html.twig]:

New action name (press <return> to stop adding actions): deleteAction
Action route [/delete]: {id}/delete
Templatename (optional) [TutorialTestBundle:Pagina:delete.html.twig]:

New action name (press <return> to stop adding actions):

You are going to generate a "TutorialTestBundle:Pagina" controller
using the "yml" format for the routing and the "twig" format
for templating
Do you confirm generation [yes]?

Generating the bundle code: OK

Ce produce urmatorul fisier Tutorial/TestBundle/Controller/PaginaController.php:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php

namespace Tutorial\TestBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class PaginaController extends Controller
{
    public function indexAction()
    {
        return $this->render('TutorialTestBundle:Pagina:index.html.twig', array(
                // ...
            ));    }

    public function newAction()
    {
        return $this->render('TutorialTestBundle:Pagina:new.html.twig', array(
                // ...
            ));    }

    public function editAction($id)
    {
        return $this->render('TutorialTestBundle:Pagina:edit.html.twig', array(
                // ...
            ));    }

    public function deleteAction($id)
    {
        return $this->render('TutorialTestBundle:Pagina:delete.html.twig', array(
                // ...
            ));    }

}

Sunt generate niste pagini de baza pentru afisare in Tutorial/TestBundle/Resources/views/Pagina:

  • delete.html.twig
  • edit.html.twig
  • index.html.twig
  • new.html.twig

De asemenea sunt adaugate rute (descrieri ale actiunilor definite) in fisierul Tutorial/TestBundle/Resources/config/routing.yml (voi descrie aceasta functie intr-un articol viitor):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
index:
    path:     /
    defaults: { _controller: TutorialTestBundle:Pagina:index }

new:
    path:     /new
    defaults: { _controller: TutorialTestBundle:Pagina:new }

edit:
    path:     {id}/edit
    defaults: { _controller: TutorialTestBundle:Pagina:edit }

delete:
    path:     {id}/delete
    defaults: { _controller: TutorialTestBundle:Pagina:delete }

Mai multe gasiti aici.

Anterior - Creare entitati