Symfony 248
Symfony 248
#symfony2
Tabla de contenido
Acerca de 1
Observaciones 2
Versiones 2
Examples 3
Instalación o configuración 3
Introducción 8
Examples 8
Examples 10
Examples 12
Examples 25
Examples 26
Uno a muchos, bidireccional 26
Examples 30
Función ExpressionBuilder IN () 31
Capítulo 8: Enrutamiento 33
Examples 33
Múltiples rutas 33
Examples 36
Rutas de YAML 36
Examples 38
Haga un número corto, por ejemplo, 1 000 -> 1k, 1 000 000 -> 1M, etc. 40
Examples 42
Gestionando la seguridad 42
Examples 44
Examples 45
Agregue los detalles del usuario y los parámetros publicados enviados a los registros. 45
Parámetros 48
Observaciones 48
Examples 48
Entidad de vivienda 49
Examples 52
Parámetros 54
Examples 54
Uso simple 54
Establecer encabezado 54
JsonResponse 55
Examples 56
Observaciones 58
Examples 58
Parámetros 60
Examples 60
Examples 63
Validación de formulario simple usando restricciones 63
Creditos 66
Acerca de
You can share this PDF with anyone you feel could benefit from it, downloaded the latest version
from: symfony2
It is an unofficial and free symfony2 ebook created for educational purposes. All the content is
extracted from Stack Overflow Documentation, which is written by many hardworking individuals at
Stack Overflow. It is neither affiliated with Stack Overflow nor official symfony2.
The content is released under Creative Commons BY-SA, and the list of contributors to each
chapter are provided in the credits section at the end of this book. Images may be copyright of
their respective owners unless otherwise specified. All trademarks and registered trademarks are
the property of their respective company owners.
Use the content presented in this book at your own risk; it is not guaranteed to be correct nor
accurate, please send your feedback and corrections to [email protected]
https://riptutorial.com/es/home 1
Capítulo 1: Empezando con symfony2
Observaciones
Esta sección proporciona una descripción general de qué es Symfony2 y por qué un desarrollador
puede querer usarlo.
También debe mencionar cualquier tema importante dentro de Symfony2 y vincularlo a los temas
relacionados. Dado que la Documentación para Symfony2 es nueva, es posible que necesites
crear versiones iniciales de esos temas relacionados.
Versiones
La última versión estable durante el tiempo de escritura es Symfony 3.1, que se mantendrá hasta
finales de julio de 2017.
Symfony tiene versiones de soporte a largo plazo que se mantienen por un total de 4 años (3
años para correcciones de errores, 1 año adicional para correcciones de errores de seguridad)
Una versión menor estándar se mantiene durante un período de ocho meses para las
correcciones de errores, y durante un período de catorce meses para las correcciones de
problemas de seguridad.
A partir de la versión 3.X, las versiones menores estarán limitadas a 5 y la última versión menor
será LTS.
Syfmony tiene doble modo de mantenimiento, lanzando versiones menores cada seis meses una
vez en mayo y una vez en noviembre. Las versiones principales se lanzan cada dos años, lo que
significa que habrá un período de tiempo de un año para pasar de la versión principal anterior a la
más reciente, dando a los usuarios la posibilidad de elegir entre las características más recientes
de la versión estándar o una versión LTS compatible con la corrección de errores.
Symfony mantiene una estricta compatibilidad con versiones anteriores, todo lo que se rompe BC
se realiza en la próxima versión principal. Una implementación de características que es una
mejora pero que se rompe BC se mantiene junto con la implementación antigua que quedará
obsoleta
https://riptutorial.com/es/home 2
Lea más sobre las versiones y el proceso de desarrollo en detalle en la documentación oficial
[aquí] [1]
Examples
Instalación o configuración
El proyecto symfony se puede crear ejecutando el symfony new my_project [2.8] en Linux / Mac OS
X
o alternativamente, el symfony new my_project lts usará la última versión de soporte a largo plazo
de Symfony.
https://riptutorial.com/es/home 3
• Usa el comando create-project Composer para descargar Symfony
Esto inicia el servidor web en localhost:8000 en el fondo que sirve a su aplicación Symfony.
Luego, abra su navegador y acceda a http://localhost:8000/ URL para ver la página de
bienvenida de Symfony:
https://riptutorial.com/es/home 4
https://riptutorial.com/es/home 5
El ejemplo más simple en Symfony.
<?php
// src/AppBundle/Controller/MyController.php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
class MyController
{
/**
* @Route("/hello")
*/
public function myHelloAction()
{
return new Response(
'<html><body>
I\'m the response for request <b>/hello</b>
</body></html>'
);
}
}
NOTA: Todas las clases de controladores deben tener extremos con la palabra ' Controlador ' y
los métodos relacionados con las rutas deben terminar con la palabra ' Acción '. Además, en qué
controlador se colocan sus Acciones no es relevante hasta que defina un prefijo de enrutamiento
para el controlador.
Requisitos de verificación
https://riptutorial.com/es/home 6
Proyecto en ejecución
Ejecute composer install para instalar todas las dependencias. A continuación, configure el
permiso para var/cache , var/logs y var/sessions .
https://riptutorial.com/es/home 7
Capítulo 2: Configuración de paquetes
propios.
Introducción
Esta es una descripción de cómo puede crear una configuración para su propio paquete en
/app/config/config.{yml,xml}
Examples
Crear configuración en la aplicación / config / config.yml
amazingservice:
url: 'http://amazing.com'
client_id: 'test_client_1'
client_secret: 'test_secret'
Este es un ejemplo básico para crear la configuración en formato yml, para seguir el formato yml
puede tomar una configuración más profunda.
Por ejemplo, tienes un paquete generado por la consola de Symfony. En este caso, en
DependencyInjection / Configuration.php, debe insertar su representación de configuración:
$rootNode->children()
->scalarNode('url')->end()
->scalarNode('client_id')->end()
->scalarNode('client_secret')->end()
->end()
->end();
No es suficiente para obtener la configuración en los servicios. Tienes que llevarlo al contenedor.
$container->setParameter(
'amazingservice.config',
$config
);
https://riptutorial.com/es/home 8
En este caso, la configuración en el contenedor, entonces si su Servicio obtiene el contenedor
como un parámetro de constructor:
base.amazingservice:
class: Base\AmazingBundle\Services\AmazingServices
arguments: [@service_container]
private $config;
https://riptutorial.com/es/home 9
Capítulo 3: Crea servicios web con Symfony
usando Rest.
Examples
Usando Symfony REST Edition
Symfony REST Edition es una aplicación Symfony2 completamente funcional que puedes usar
como esqueleto para tus nuevas aplicaciones.
Disponible en Github
Haz Descripción
https://riptutorial.com/es/home 10
Haz Descripción
https://riptutorial.com/es/home 11
Capítulo 4: Creando servicios web con
Symfony 2.8
Examples
Trabajar con RESTFul API
Se basa en el protocolo HTTP ( HTTP en Wiki ), solicitudes HTTP (GET, POST, PATCH, DELETE
...) / códigos de respuestas (404, 400, 200, 201, 500 ...) y estructura de cuerpos.
Esta es una excelente manera de exponer sus datos a otro sistema en Internet.
Imagine que desea crear una API RESTFul para administrar su StackOverFlower (usuario) en su
base de datos local.
¡Hagamos el ejemplo!
Debes instalar y configurar un servidor web en tu máquina local, consulta Wamp o Lamp o Mamp
: debes tener una versión reciente de PHP ( !!! requisitos de Symfony !!! )
Debe configurar PHP cli (que varía en nuestro sistema), escriba este "Cómo hacer PHP cli [OS-
NAME]" en nuestro amigo Google. Debes instalar compositor, ver instalar compositor
3. Symfony:
Debes instalar Symfony 2.8 (con el compositor, es la mejor manera), abrir un terminal (o cmd en
Windows) e ir a la ruta de tu servidor web.
Symfony 2 funciona con uno de los mejores tipos de estructura: paquetes. Todos son paquetes en
Symfony! Podemos probarlo arriba.
cd /your-web-server-path/
composer create-project symfony/framework-standard-edition example "2.8.*"
Vaya a la estructura de árbol y vea: Symfony 2.8 está instalado en el directorio "ejemplo".
https://riptutorial.com/es/home 12
4. FOSRest (para FriendsOfSymfony) en el paquete JMSSerializer:
JMSSerializer ( Instalar ):
FosRestBundle ( Instalar ):
5. Configuracion basica :
cd /path/to/your/symfony/
php app/console generate:bundle
php app/console doctrine:generate:database
#app/config/config.yml
fos_rest:
format_listener:
rules:
- { path: '^/stackoverflower', priorities: ['xml', 'json'], fallback_format: xml,
prefer_extension: true }
- { path: '^/', priorities: [ 'text/html', '*/*'], fallback_format: html,
prefer_extension: true }
# src/ExampleBundle/Resources/config/doctrine/StackOverFlower.orm.yml
ExampleBundle\Entity\StackOverFlower:
type: entity
table: stackoverflower
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
name:
type: string
length: 100
https://riptutorial.com/es/home 13
php app/console doctrine:schema:update --force
#src/ExampleBundle/Controller/StackOverFlowerController.php
namespace ExampleBundle\Controller;
use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpFoundation\Request;
use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\Annotations\Post;
use FOS\RestBundle\Controller\Annotations\Delete;
use ExampleBundle\Entity\StackOverFlower;
$id = $request->get('id');
$user = $this->getDoctrine()->getManager()-
>getRepository("ExampleBundle:StackOverFlower")->findOneBy(array('id' => $id));
return $user;
}
/**
* validateAndPersistEntity
*
* @param StackOverFlower $user
* @param Boolean $delete
* @return View the view
*/
private function validateAndPersistEntity(StackOverFlower $user, $delete = false) {
$template = "ExampleBundle:StackOverFlower:example.html.twig";
$validator = $this->get('validator');
$errors_list = $validator->validate($user);
if (count($errors_list) == 0) {
$em = $this->getDoctrine()->getManager();
$em->flush();
https://riptutorial.com/es/home 14
$view = $this->view($user)
->setTemplateVar('user')
->setTemplate($template);
} else {
$errors = "";
foreach ($errors_list as $error) {
$errors .= (string) $error->getMessage();
}
$view = $this->view($errors)
->setTemplateVar('errors')
->setTemplate($template);
return $view;
}
/**
* newStackOverFlowerAction
*
* @Get("/stackoverflower/new/{name}")
*
* @param Request $request
* @return String
*/
public function newStackOverFlowerAction(Request $request)
{
$user = new StackOverFlower();
$user->setName($request->get('name'));
$view = $this->validateAndPersistEntity($user);
return $this->handleView($view);
}
/**
* editStackOverFlowerAction
*
* @Get("/stackoverflower/edit/{id}/{name}")
*
* @param Request $request
* @return type
*/
public function editStackOverFlowerAction(Request $request) {
$user = $this->findStackOverFlowerByRequest($request);
if (! $user) {
$view = $this->view("No StackOverFlower found for this id:". $request->get('id'),
404);
return $this->handleView($view);
}
$user->setName($request->get('name'));
$view = $this->validateAndPersistEntity($user);
return $this->handleView($view);
https://riptutorial.com/es/home 15
}
/**
* deleteStackOverFlowerAction
*
* @Get("/stackoverflower/delete/{id}")
*
* @param Request $request
* @return type
*/
public function deleteStackOverFlowerAction(Request $request) {
$user = $this->findStackOverFlowerByRequest($request);
if (! $user) {
$view = $this->view("No StackOverFlower found for this id:". $request->get('id'),
404);
return $this->handleView();
}
return $this->handleView($view);
}
/**
* getStackOverFlowerAction
*
* @Get("/stackoverflowers")
*
* @param Request $request
* @return type
*/
public function getStackOverFlowerAction(Request $request) {
$template = "ExampleBundle:StackOverFlower:example.html.twig";
$users = $this->getDoctrine()->getManager()-
>getRepository("ExampleBundle:StackOverFlower")->findAll();
if (count($users) === 0) {
$view = $this->view("No StackOverFlower found.", 404);
return $this->handleView();
}
$view = $this->view($users)
->setTemplateVar('users')
->setTemplate($template);
return $this->handleView($view);
}
}
#src/ExampleBundle/Resources/views/StackOverFlower.html.twig
{% if errors is defined %}
{{ errors }}
{% else %}
{% if users is defined %}
https://riptutorial.com/es/home 16
{{ users | serialize }}
{% else %}
{{ user | serialize }}
{% endif %}
{% endif %}
Como puede ver en la base de datos, se ha creado un nuevo usuario con el nombre "prueba".
Tiene un ejemplo completo en mi cuenta de github de este ejemplo: ejemplo de Git Hub , en la
rama "maestra" de este ejemplo, y en la rama de "rutas reales", un ejemplo con una URL más
apropiada (como POST y DELETE).
Atentamente,
Mathieu
SOAP (Simple Access Object Protocol) está basado en XML, como XML-RPC, es ancestro , con
un archivo llamado WSDL , que describe el método a exponer.
Como se describió anteriormente, lo más importante para describir su servicio web es el archivo
WSDL , consulte: explicación WSDL en wiki
El trabajo básico será definir qué se expone en su API SOAP, su clase y su proceso de negocio
serán manejados automáticamente por la clase básica de SOAPServer de PHP. ¡Todavía
https://riptutorial.com/es/home 17
necesitas el código!
Con esta información básica, puede lograr todas las API que desee.
Imagina que quieres hacer una api de SOAP para administrar tu StackOverFlower (usuario) en tu
base de datos local.
¡Hagamos el ejemplo!
Instala el servidor web, Php cli, Composer, Symfony 2.8, crea un nuevo paquete "ExampleBundle"
y crea el esquema como se describe anteriormente.
Antes de comenzar a construir nuestra lógica de negocios, teníamos que saber qué exponer de
nuestro controlador. Este trabajo se realiza mediante el uso de WSDL. Este es un ejemplo de una
buena sintaxis de un WSDL:
<definitions name="StackOverFlowerService"
targetNamespace="http://example/soap/stackoverflower.wsdl"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://example/soap/stackoverflower.wsdl"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<message name="NewRequest">
<part name="name" type="xsd:string"/>
</message>
<message name="NewResponse">
<part name="status" type="xsd:string"/>
</message>
<message name="getListRequest"></message>
<message name="getListResponse">
<part name="list" type="xsd:string"/>
</message>
<message name="editRequest">
<part name="id" type="xsd:string"/>
<part name="name" type="xsd:string"/>
</message>
<message name="editResponse">
<part name="status" type="xsd:string"/>
</message>
https://riptutorial.com/es/home 18
<message name="deleteRequest">
<part name="id" type="xsd:string"/>
</message>
<message name="deleteResponse">
<part name="status" type="xsd:string"/>
</message>
<portType name="StackOverFlower_PortType">
<operation name="newStack">
<input message="tns:NewRequest"/>
<output message="tns:NewResponse"/>
</operation>
<operation name="getList">
<input message="tns:getListRequest"/>
<output message="tns:getListResponse"/>
</operation>
<operation name="edit">
<input message="tns:editRequest"/>
<output message="tns:editResponse"/>
</operation>
<operation name="delete">
<input message="tns:deleteRequest"/>
<output message="tns:deleteResponse"/>
</operation>
</portType>
<output>
<soap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="urn:example:new"
use="encoded"/>
</output>
</operation>
<operation name="getList">
<soap:operation soapAction="getList"/>
<input>
<soap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="urn:example:get-list"
use="encoded"/>
</input>
<output>
<soap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="urn:example:get-list"
https://riptutorial.com/es/home 19
use="encoded"/>
</output>
</operation>
<operation name="edit">
<soap:operation soapAction="edit"/>
<input>
<soap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="urn:example:edit"
use="encoded"/>
</input>
<output>
<soap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="urn:example:edit"
use="encoded"/>
</output>
</operation>
<operation name="delete">
<soap:operation soapAction="delete"/>
<input>
<soap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="urn:example:delete"
use="encoded"/>
</input>
<output>
<soap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="urn:example:delete"
use="encoded"/>
</output>
</operation>
</binding>
<service name="StackOverFlower_Service">
<documentation>Description File of StackOverFlowerService</documentation>
<port binding="tns:StackOverFlower_Binding" name="StackOverFlower_Port">
<soap:address
location="http://example/stackoverflower/" />
</port>
</service>
</definitions>
Debemos tomar esto en tu directorio web de Symfony (en el subdirectorio de SOAP y nombrarlo
como "stackoverflower.wsdl").
Realmente inspirado en el ejemplo WSDl . Puede validar eso con un validador WSDl en línea
Después de esto, podemos crear nuestro controlador y servicio básico, inspirados en SOAP
Symfony 2.8 Doc .
https://riptutorial.com/es/home 20
#src\ExampleBundle\Services\StackOverFlowerService.php
namespace ExampleBundle\Services;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use ExampleBundle\Entity\StackOverFlower;
class StackOverFlowerService
{
private $em;
private $stackoverflower;
$this->em->persist($stackoverflower);
$this->em->flush();
return "ok";
}
$stackoverflower->setName($name);
$this->em->persist($stackoverflower);
$this->em->flush();
return "ok";
}
https://riptutorial.com/es/home 21
$this->em->remove($stackoverflower);
$this->em->flush();
return "ok";
}
}
#src\ExampleBundle\Resources\config\services.yml
services:
stackoverflower_service:
class: ExampleBundle\Services\StackOverFlowerService
arguments: [@doctrine.orm.entity_manager]
Como puede ver, inyectamos Doctrine Entity Manger como una dependencia porque tenemos que
usar esto para el objeto StackOverFlower de CRUD.
#src\ExampleBundle\Controller\StackOverFlowerController.php
namespace ExampleBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
$options = array(
'uri' => 'http://example/app_dev.php/soap',
'cache_wsdl' => WSDL_CACHE_NONE,
'exceptions' => true
);
ob_start();
$server->handle();
$response->setContent(ob_get_clean());
return $response;
}
}
Para obtener más información sobre los servicios, consulte: Contenedor de servicios en Symfony
doc.
https://riptutorial.com/es/home 22
La ruta :
example_soap:
path: /soap
defaults: { _controller: ExampleBundle:StackOverFlower:index }
#src\ExampleBundle\Resources\views\Soap\default.html.twig
{% if status is defined %}
{{ status }}
{% else %}
{{ list }}
{% endif %}
https://riptutorial.com/es/home 23
// print_r($result);
Las rutas:
test_new:
path: /stackoverflower/new
defaults: { _controller: ExampleBundle:StackOverFlower:testNew }
test_edit:
path: /stackoverflower/edit
defaults: { _controller: ExampleBundle:StackOverFlower:testEdit }
test_get_list:
path: /stackoverflower/get-list
defaults: { _controller: ExampleBundle:StackOverFlower:testGetList }
test_delete:
path: /stackoverflower/delete
defaults: { _controller: ExampleBundle:StackOverFlower:testDelete }
1. getList
2. nuevo
3. editar
4. borrar
Este es un ejemplo muy básico de una API no segura con SOAP. Puedo hacer un ejemplo de un
ejemplo seguro detrás de una autenticación de clave api más adelante.
Mathieu
https://riptutorial.com/es/home 24
Capítulo 5: Despliegue de Symfony2
Examples
Pasos para mover el proyecto Symfony 2 a hosting manualmente
1. Si tiene una consola SSH, puede hacerlo en el hosting después del paso 2, si no lo ha
hecho localmente: ejecute el comando
3. Compruebe CHMOD para las carpetas app/cache y app/logs , debe haber acceso de
escritura.
4. Si no hay ningún archivo .htaccess en public_html, créelo y agregue dicho código en él:
https://raw.github.com/symfony/symfony-standard/master/web/.htaccess
https://riptutorial.com/es/home 25
Capítulo 6: Doctrina Entidad Relaciones
Examples
Uno a muchos, bidireccional
Una relación bidireccional tiene un lado propietario e inverso . OneToMany relaciones OneToMany
pueden usar tablas de unión, por lo que debe especificar un lado propietario. La asociación
OneToMany es siempre el lado inverso de una asociación bidireccional.
<?php
namespace AppBundle\Entity;
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="username", type="string", length=255)
*/
protected $username;
/**
* @var Group|null
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Group", inversedBy="users")
* @ORM\JoinColumn(name="group_id", referencedColumnName="id", nullable=true)
*/
protected $group;
/**
* @param string $username
* @param Group|null $group
*/
public function __construct($username, Group $group = null)
{
https://riptutorial.com/es/home 26
$this->username = $username;
$this->group = $group;
}
/**
* Set username
*
* @param string $username
*/
public function setUsername($username)
{
$this->username = $username;
}
/**
* Get username
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* @param Group|null $group
*/
public function setGroup(Group $group = null)
{
if($this->group !== null) {
$this->group->removeUser($this);
}
$this->group = $group;
}
/**
* Get group
*
* @return Group|null
*/
public function getGroup()
{
return $this->group;
}
}
<?php
namespace AppBundle\Entity;
https://riptutorial.com/es/home 27
/**
* @ORM\Entity
* @ORM\Table(name="groups")
*/
class Group
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(name="name", type="string", length=255)
*/
protected $name;
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\User", mappedBy="group")
*/
protected $users;
/**
* @param string $name
*/
public function __construct($name)
{
$this->name = $name;
$this->users = new ArrayCollection();
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
https://riptutorial.com/es/home 28
public function getUsers()
{
return $this->users;
}
https://riptutorial.com/es/home 29
Capítulo 7: Doctrine Entity Repository
Examples
Creando un nuevo repositorio
Puede crear un nuevo Repositorio donde lo desee, pero se recomienda crearlos en una carpeta
de Repository separada.
Si bien puede nombrar el archivo y la clase del Repositorio como desee, se recomienda nombrar
el Repository EntityNameRepository , para que pueda encontrarlos rápidamente en su carpeta.
<?php
namespace AppBundle\Entity;
/**
* Project Entity - some information
*
* @ORM\Table(name="project")
* @ORM\Entity(repositoryClass="AppBundle\Repository\ProjectRepository")
*/
class Project
{
// definition of the entity with attributes, getters, setter whatsoever
}
?>
También debe usar la clase \Doctrine\ORM\Mapping para usar las opciones de mapeo.
<?php
namespace AppBundle\Repository;
https://riptutorial.com/es/home 30
->select('p')
->from($this->_entityName, 'p')
->orderBy('p.id', 'DESC')
->setMaxResults(10)
;
// uses the build query and gets the data from the Database
return $qb->getQuery()->getResult();
}
}
?>
Función ExpressionBuilder IN ()
Si desea utilizar el comando MySQL IN() en QueryBuilder, puede hacerlo con la función in() de la
clase ExpressionBuilder .
return $qb->getQuery()->getResult();
Como ejemplo, solo para demostrar CÓMO usar una instrucción de selección de subconsulta
dentro de una instrucción de selección, supongamos que encontramos a todos los usuarios que
aún no han compilado la dirección (no existen registros en la tabla de direcciones):
// create a subquery in order to take all address records for a specified user id
$sub = $this->_em->createQueryBuilder()
->select('a')
->from($this->_addressEntityName, 'a')
->where('a.user = u.id');
$qb = $this->_em->createQueryBuilder()
->select('u')
->from($this->_userEntityName, 'u')
->where($expr->not($expr->exists($sub->getDQL())));
return $qb->getQuery()->getResult();
https://riptutorial.com/es/home 31
entity-repository
https://riptutorial.com/es/home 32
Capítulo 8: Enrutamiento
Examples
Devuelve una respuesta 404
<?php
namespace Bundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
Múltiples rutas
En Symfony es posible definir múltiples rutas para una acción. Esto puede ser muy útil si tiene
funciones que hacen lo mismo pero tienen diferentes parámetros.
https://riptutorial.com/es/home 33
return $this->render('::Test/test.html.twig', array('test' => $test));
}
}
Cuando se encuentra en una instancia de controllerAction Y recibe una solicitud POST , pero
desea redirigirla a una ruta diferente , mientras mantiene el método POST y el objeto de
solicitud , puede usar lo siguiente:
Asumiendo
locale: en
domain: somedomain.com
/**
* @Route(
* "/",
* name="homepage",
* host="{_locale}.{domain}",
* defaults={"_locale" = "%locale%", "domain" = "%domain%"},
* requirements={"_locale" = "%locale%|de|fr", "domain" = "%domain%"}
* )
* @Route(
* "/",
* name="homepage_default",
* defaults={"_locale" = "%locale%"}
* )
*/
Desde este punto, el enrutador puede manejar URI como http://de.somedomain.com . La segunda
anotación de @Route se puede usar como una alternativa para la configuración regional
predeterminada y el subdominio nulo, http://somedomain.com .
profile_user_profile:
path: /profile/{id}
https://riptutorial.com/es/home 34
defaults: { _controller: ProfileBundle:Profile:profile }
requirements:
id: \d+
methods: [get, delete]
Si decide utilizar Routing.yml en lugar de Anotaciones, puede obtener una mejor vista de todas
las rutas y es más fácil buscar y encontrar una.
https://riptutorial.com/es/home 35
Capítulo 9: Enrutamiento básico
Examples
Enrutamiento basado en anotaciones
namespace AppBundle\Controller;
class AcmeController
{
/**
* @Route("/index")
*/
public function indexAction()
{
// ...
}
}
Para que el marco pueda manejar estas rutas, debe importarlas en su routing.yml siguiente
manera (observe el tipo de annotation ):
app:
resource: "@AppBundle/Controller"
type: annotation
Rutas de YAML
app_index:
path: /index
defaults: { _controller: AppBundle:Acme:index }
Las mismas opciones se aplican tanto a las anotaciones como a las configuraciones YAML. Para
importar una configuración de enrutamiento YAML en su configuración de enrutamiento raíz, no
necesita especificar un tipo:
app:
prefix: /app
resource: "@AppBundle/Resources/config/routing.yml"
https://riptutorial.com/es/home 36
Lea Enrutamiento básico en línea: https://riptutorial.com/es/symfony2/topic/5881/enrutamiento-
basico
https://riptutorial.com/es/home 37
Capítulo 10: Extensiones Symfony Twig
Examples
Una extensión de ramita simple - Symfony 2.8
Lo primero que uno tendría que hacer es definir la clase de extensión que albergará los filtros y / o
funciones de ramitas.
<?php
namespace AppBundle\Twig;
/**
* This is where one defines the filters one would to use in their twig
* templates
*
* @return Array
*/
public function getFilters()
{
return array (
new \Twig_SimpleFilter (
'price', // The name of the twig filter
array($this, 'priceFilter')
),
);
}
/**
* Define the functions one would like availed in their twig template
*
* @return Array
*/
public function getFunctions() {
return array (
new \Twig_SimpleFunction (
'lipsum', // The name of the twig function
https://riptutorial.com/es/home 38
array($this, 'loremIpsum')
)
);
}
# app/config/services.yml
services:
app.twig.demo_extension:
class: AppBundle\Twig\DemoExtension
tags:
- { name: twig.extension }
Con esto, tiene todo lo que necesita para poder utilizar su filtro o función de ramita recién creado
en sus plantillas de ramita.
https://riptutorial.com/es/home 39
Haga un número corto, por ejemplo, 1 000 -> 1k, 1 000 000 -> 1M, etc.
Symfony 2.8
# AppBundle\Twig\AppExtension.php
<?php
namespace AppBundle\Twig;
/**
* Shorten the number
*
* @param integer
* @return string
*/
public function shortNumber($number)
{
$k = pow(10,3);
$mil = pow(10,6);
$bil = pow(10,9);
/**
* Get name
*/
public function getName()
{
return 'app_extension';
}
}
# app/config/services.yml
services:
https://riptutorial.com/es/home 40
app.twig_extension:
class: AppBundle\Twig\AppExtension
public: false
tags:
- { name: twig.extension }
Úsalo en TWIG
https://riptutorial.com/es/home 41
Capítulo 11: Gestionando los firewalls y la
seguridad de Symfony.
Examples
Gestionando la seguridad
La seguridad era parte del lado oscuro de la documentación de Symfony, tiene un componente
dedicado llamado Componente de Seguridad .
# app/config/security.yml
security:
providers:
in_memory:
memory: ~
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
default:
anonymous: ~
Puede definir cortafuegos específicos para restringir el acceso a algunas URL a roles
específicos basados en una jerarquía para sus usuarios que están definidos por un proveedor y
codificadores que administran la seguridad de la contraseña.
Por ejemplo, si desea crear un proveedor personalizado, desde su motor de base de datos,
puede definir su security.yml así:
providers:
your_db_provider:
entity:
class: AppBundle:User
property: apiKey
Después de eso, puede definir un firewall para restringir algunas URL basadas en su proveedor
de usuario personalizado (security.yml) explícitamente de esta manera:
firewalls:
secured_area:
https://riptutorial.com/es/home 42
pattern: ^/admin
access_control:
- { path: ^/admin/users, roles: ROLE_SUPER_ADMIN }
- { path: ^/admin, roles: ROLE_ADMIN }
https://riptutorial.com/es/home 43
Capítulo 12: Instala Symfony2 en localhost
Examples
Usando el símbolo del sistema
Mac OS X / Linux
Windows
Si quieres crear el proyecto con una versión específica de Symfony, puedes agregar un
parámetro con el número de versión:
Sugerencia: si piensa que el compositor no hará nada, agregue el -vvv al comando. De esa
manera, recibirás información detallada sobre lo que el compositor está haciendo en este
momento.
https://riptutorial.com/es/home 44
Capítulo 13: Monolog: mejora tus registros.
Examples
Agregue los detalles del usuario y los parámetros publicados enviados a los
registros.
Los registros son muy importantes. Volver a crear un contexto de error puede ser a veces muy
doloroso debido a la falta de información sobre cómo y cuándo ocurrió el error.
• Cómo usar WebProcessor para agregar todos los datos relacionados con la solicitud, como:
○ url
○ ip
○ método http
○ servidor
○ referente
services:
# Permits to convert logs in HTML format for email notification
monolog.formatter.html:
class: Monolog\Formatter\HtmlFormatter
# Custom class to include user's data and posted parameters in the logs
monolog.processor.user:
class: Company\ToolBoxBundle\Services\Monolog\ExtraProcessor
arguments: ["@security.token_storage"]
tags:
- { name: monolog.processor }
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
Código de servicio
namespace Company\ToolBoxBundle\Services\Monolog;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
https://riptutorial.com/es/home 45
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class ExtraProcessor
{
/**
* @var string
*/
private $postParams = null;
/**
* @var TokenStorageInterface
*/
private $tokenStorage = null;
/**
* @var \Company\UserBundle\Entity\User
*/
private $user = null;
return $record;
}
if (!is_object($user = $token->getUser())) {
// e.g. anonymous authentication
return;
}
https://riptutorial.com/es/home 46
}
}
https://riptutorial.com/es/home 47
Capítulo 14: Opciones de envío a una clase
de formulario
Sintaxis
• $ form = $ this-> createForm (HouseholdType :: class, $ household, $ formOptions);
Parámetros
Parámetro Definición
Tipo de Hogar ::
clase de formulario personalizado para la entidad del hogar
clase
Observaciones
Al crear una clase de formulario, los campos de formulario se agregan en la public function
buildForm(FormBuilderInterface $builder, array $options) {...} . El parámetro $options incluye un
conjunto de opciones predeterminadas, como attr y label . Para permitir que sus opciones
personalizadas estén disponibles en la clase de formulario, las opciones deben inicializarse en
configureOptions(OptionsResolver $resolver)
Examples
Un ejemplo del mundo real de un controlador de la casa
Antecedentes: la entidad del hogar incluye un conjunto de opciones, cada una de las cuales es
una entidad que se administra en un backend de administración. Cada opción tiene una bandera
https://riptutorial.com/es/home 48
enabled booleana. Si una opción previamente habilitada está configurada como deshabilitada,
deberá persistir en las ediciones posteriores de Household, pero no podrá ser eliminada. Para
lograr esto, la definición de campo en la clase de formulario mostrará el campo como un campo
de opción deshabilitado si la opción ha enabled = false (pero se mantiene porque el botón de
envío activa un javascript que elimina el atributo disabled ). La definición de campo también evita
las opciones deshabilitadas de ser mostrado.
La clase de formulario debe saber, para una entidad del hogar determinada, cuál de sus opciones
se ha deshabilitado. Se ha definido un servicio que devuelve una matriz de los nombres de las
entidades de opción que se han deshabilitado. Esa matriz es $disabledOptions .
$formOptions = [
'disabledOptions' => $disabledOptions,
];
$form = $this->createForm(HouseholdType::class, $household, $formOptions);
->add('housing', EntityType::class,
array(
'class' => 'AppBundle:Housing',
'choice_label' => 'housing',
'placeholder' => '',
'attr' => (in_array('Housing', $options['disabledOptions']) ? ['disabled' =>
'disabled'] : []),
'label' => 'Housing: ',
'query_builder' => function (EntityRepository $er) use ($options) {
if (false === in_array('Housing', $options['disabledOptions'])) {
return $er->createQueryBuilder('h')
->orderBy('h.housing', 'ASC')
->where('h.enabled=1');
} else {
return $er->createQueryBuilder('h')
->orderBy('h.housing', 'ASC');
}
},
))
Entidad de vivienda
/**
* Housing.
*
* @ORM\Table(name="housing")
* @ORM\Entity
*/
class Housing
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
https://riptutorial.com/es/home 49
*/
protected $id;
/**
* @var bool
*
* @ORM\Column(name="housing", type="string", nullable=false)
* @Assert\NotBlank(message="Housing may not be blank")
*/
protected $housing;
/**
* @var bool
*
* @ORM\Column(name="enabled", type="boolean", nullable=false)
*/
protected $enabled;
/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set housing.
*
* @param int $housing
*
* @return housing
*/
public function setHousing($housing)
{
$this->housing = $housing;
return $this;
}
/**
* Get housing.
*
* @return int
*/
public function getHousing()
{
return $this->housing;
}
/**
* Set enabled.
*
* @param int $enabled
*
* @return enabled
*/
public function setEnabled($enabled)
{
https://riptutorial.com/es/home 50
$this->enabled = $enabled;
return $this;
}
/**
* Get enabled.
*
* @return int
*/
public function getEnabled()
{
return $this->enabled;
}
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\OneToMany(targetEntity="Household", mappedBy="housing")
*/
protected $households;
https://riptutorial.com/es/home 51
Capítulo 15: Patrones de diseño de Symfony
Examples
Patrón de inyección de dependencia
Imagina que tienes un administrador de clase para administrar el envío de correos (se llamará
MailManager).
En esto, tienes que registrar los correos que se envían. Una buena solución es transformar la
clase MailManager en un service y luego inyectar la clase para crear registros ( Monolog por
ejemplo) en el MailManager creando un servicio.
services:
mail.manager.class:
class: Vendor/YourBundle/Manager/MailManager
services:
mail.manager.class:
class: Project/Bundle/Manager/MailManager
arguments: ["@logger"] # inject logger service into constructor
<?php
namespace Project\Bundle\Manager;
use Symfony\Component\HttpKernel\Log\LoggerInterface;
class MailManager
{
protected $logger;
https://riptutorial.com/es/home 52
}
}
<?php
}
}
https://riptutorial.com/es/home 53
Capítulo 16: Respuesta
Parámetros
Parámetro Detalles
Examples
Uso simple
// Action's code
// Action's code
Otro ejemplo:
// Action's code
Establecer encabezado
https://riptutorial.com/es/home 54
public function someAction(){
// Action's code
$response = new Response();
$response->headers->set('Content-Type', 'text/html');
return $response;
}
JsonResponse
use Symfony\Component\HttpFoundation\JsonResponse;
// Action's code
$data = array(
// Array data
);
https://riptutorial.com/es/home 55
Capítulo 17: Servicios de Symfony
Examples
Cómo declarar, escribir y usar un servicio simple en Symfony2
Declaración de servicios:
# src/Acme/YourBundle/Resources/config/services.yml
services:
my_service:
class: Acme\YourBundle\Service\MyService
arguments: ["@doctrine", "%some_parameter%", "@another_service"]
another_service:
class: Acme\YourBundle\Service\AnotherService
arguments: []
Código de servicio :
<?php
namespace Acme\YourBundle\Service\Service;
class MyService
{
/**
* Constructor
* You can had whatever you want to use in your service by dependency injection
* @param $doctrine Doctrine
* @param $some_parameter Some parameter defined in app/config/parameters.yml
* @param $another_service Another service
*/
public function __construct($doctrine, $some_parameter, $another_service)
{
$this->doctrine = $doctrine;
$this->some_parameter = $some_parameter;
$this->another_service = $another_service;
}
Úsalo en un controlador:
<?php
namespace Acme\YourBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Acme\YourBundle\Service\Service\MyService;
https://riptutorial.com/es/home 56
class MyController extends Controller
{
/**
* One action
*/
public function oneAction(Request $request)
{
$myService = $this->get('my_service');
$myService->doMagic();
// ...
}
}
https://riptutorial.com/es/home 57
Capítulo 18: Solicitud
Observaciones
Enlaces de documentación API (maestro):
• Solicitud
• RequestStack
El objeto de solicitud contiene varios datos significativos, como la configuración regional actual y
el controlador coincidente. Puedes usarlos y gestionarlos por eventos de HttpKernel. Para una
comprensión confiable del ciclo en vivo de la Solicitud-Respuesta, lea esta página de documentos
del Componente HttpKernel (¡muy útil!).
Examples
Acceso a la solicitud en un controlador
<?php
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
Tenga en cuenta que el objeto Request inyectado se aplica a la solicitud actual (puede o no ser
igual a la solicitud maestra).
{{ app.request }}
https://riptutorial.com/es/home 58
<p>Request method: {{ app.request.method }}</p>
En plantilla de PHP
https://riptutorial.com/es/home 59
Capítulo 19: Symfony Twig Extenstion
Ejemplo
Parámetros
Parámetros Descripción
Examples
Ejemplo básico de la extensión de ramita de Symfony
<?php
namespace DashboardBundle\Twig\Extension;
use Symfony\Bridge\Doctrine\RegistryInterface;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Security\Core\SecurityContext;
/**
* Class DashboardExtension
* @package DashboardBundle\Twig\Extension
*/
class DashboardExtension extends \Twig_Extension
{
protected $doctrine;
private $context;
/**
* DashboardExtension constructor.
* @param RegistryInterface $doctrine
* @param SecurityContext $context
*/
public function __construct(RegistryInterface $doctrine,SecurityContext $context)
{
$this->doctrine = $doctrine;
$this->context = $context;
}
/**
* @return mixed
*/
public function getUser()
https://riptutorial.com/es/home 60
{
return $this->context->getToken()->getUser();
}
/**
* @return array
*/
public function getFilters()
{
return array(
new \Twig_SimpleFilter('country', array($this, 'countryFilter')),
new \Twig_SimpleFilter('count_printer_tasks', array($this, '_countPrinterTasks')),
);
}
/**
* @param $countryCode
* @param string $locale
* @return mixed
*/
public function countryFilter($countryCode,$locale = "en")
{
$c = \Symfony\Component\Intl\Intl::getRegionBundle()->getCountryNames($locale);
/**
* Returns total count of printer's tasks.
* @return mixed
*/
public function _countPrinterTasks(){
$count = $this->doctrine->getRepository('DashboardBundle:Task')-
>countPrinterTasks($this->getUser());
return $count;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'app_extension';
}
}
Para llamarlo desde la Ramita, solo tenemos que usarlo como se muestra abajo;
<tr>
<td>Nationality</td>
https://riptutorial.com/es/home 61
<td>
{{ user.getnationality|country|ucwords }}
</td>
</tr>
services:
app.twig_extension:
class: DashboardBundle\Twig\Extension\DashboardExtension
arguments: ["@doctrine", @security.context]
tags:
- { name: twig.extension }
https://riptutorial.com/es/home 62
Capítulo 20: Validación de formularios
Examples
Validación de formulario simple usando restricciones
use Symfony\Component\HttpFoundation\Request;
/*
* Now create Form object.
* You can do it manually using FormBuilder (below) or by creating
* FormType class and passing it to builder.
*/
$form = $this->createFormBuilder($book)
->add('title', TextType::class)
->add('pages', IntegerType::class)
->add('save', SubmitType::class, array('label' => 'Create Book'))
->getForm();
/*
* Handling Request by form.
* All data submitted to form by POST(default) is mapped to
* to object passed to FormBuilder ($book object)
*/
$form->handleRequest($request);
/*
* Form Validation
* In this step we check if form is submitted = data passed in POST
* and is your object valid. Object is valid only if it pass form validation
* in function isValid(). Validation constraints are loaded from config files
* depending on format (annotations, YAML, XML etc).
* IMPORTANT - object passed (book) is validated NOT form object
* Function isValid() using Symfony Validator component.
*/
if ($form->isSubmitted() && $form->isValid()) {
/*
* Now object is valid and you can save or update it
* Original object ($book) passed into form builder has been updated
* but you can also get variable by function getData:
* $book = $form->getData();
*/
https://riptutorial.com/es/home 63
}
/*
* If form is not submitted you show empty form to user.
* If validation fail then the form object contains list of FormErrors.
* Form errors are displayed in form_row template (read about form templates)
*/
return $this->render('book/create.html.twig', array(
'form' => $form->createView(),
));
}
@Anotaciones
namespace AppBundle\Entity;
class Book
{
/**
* @Assert\Length(
* min = 2,
* max = 100,
* minMessage = "Book title must be at least {{ limit }} characters long",
* maxMessage = "Book title cannot be longer than {{ limit }} characters"
* )
*/
private $title;
/**
* @Assert\Range(
* min = 3,
* max = 10000,
* minMessage = "Book must have at least {{ limit }} pages",
* maxMessage = "Book cannot have more than {{ limit }} pages"
* )
*/
private $pages;
// [...] getters/setters
}
@YAML
# src/AppBundle/Resources/config/validation.yml
AppBundle\Entity\Book:
properties:
title:
- Length:
min: 2
max: 50
minMessage: 'Book title must be at least {{ limit }} characters long'
maxMessage: 'Book title cannot be longer than {{ limit }} characters'
https://riptutorial.com/es/home 64
pages:
- Range:
min: 3
max: 10000
minMessage: Book must have at least {{ limit }} pages
maxMessage: Book cannot have more than {{ limit }} pages
https://riptutorial.com/es/home 65
Creditos
S.
Capítulos Contributors
No
Empezando con althaus, COil, Community, Dheeraj, Hendra Huang, Jakub Zalas
1
symfony2 , karel, kix, mgh, Redjan Ymeraj, TRiNE, uddhab
Configuración de
2 PumpkinSeed
paquetes propios.
Creando servicios
4 web con Symfony Barathon, Mathieu Dormeval
2.8
Despliegue de
5 sphinks
Symfony2
Doctrina Entidad
6 Federkun, palra
Relaciones
Doctrine Entity
7 doydoy44, KhorneHoly, Matteo
Repository
Extensiones
10 Sand, Strabek
Symfony Twig
Gestionando los
firewalls y la
11 Mathieu Dormeval
seguridad de
Symfony.
Instala Symfony2 en
12 Barathon, KhorneHoly, sentenza
localhost
https://riptutorial.com/es/home 66
Opciones de envío a
14 una clase de geoB
formulario
Patrones de diseño
15 DOZ
de Symfony
16 Respuesta CStff
Servicios de
17 DonCallisto, enricog, moins52
Symfony
Symfony Twig
19 Muhammad Taqi
Extenstion Ejemplo
Validación de
20 Griva
formularios
https://riptutorial.com/es/home 67