Creare controller - Tutorial Symfony
Postat la 20.06.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:
:linenos:
<?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):
:linenos:
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.