<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jon Segador &#187; Symfony</title>
	<atom:link href="http://jonsegador.com/tag/symfony/feed/" rel="self" type="application/rss+xml" />
	<link>http://jonsegador.com</link>
	<description>Desarrollador web y android, con todo lo que ello implica</description>
	<lastBuildDate>Wed, 16 May 2012 08:34:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Instalar Symfony2 desde cero</title>
		<link>http://jonsegador.com/2011/07/instalar-symfony2-desde-cero/</link>
		<comments>http://jonsegador.com/2011/07/instalar-symfony2-desde-cero/#comments</comments>
		<pubDate>Tue, 05 Jul 2011 22:23:07 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[Symfony2]]></category>
		<category><![CDATA[localhost]]></category>
		<category><![CDATA[netbeans]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[servidor]]></category>
		<category><![CDATA[Symfony]]></category>

		<guid isPermaLink="false">http://jonsegador.com/?p=463</guid>
		<description><![CDATA[Nos vamos acercando inevitablemente al cambio de Symfony 1.x a Symfony2 y la conclusión que he sacado de mi asistencia al evento #desymfony es que cuanto antes nos pongamos con ello, mucho mejor. Symfony2 mejora a su predecesor en numerosos aspectos y se convertirá en el framework definitivo para el desarrollo de aplicaciones web en [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Nos vamos acercando inevitablemente al cambio de Symfony 1.x a Symfony2</strong> y la conclusión que he sacado de mi asistencia al evento <a href="http://twitter.com/#!/search/desymfony">#desymfony</a> es que cuanto antes nos pongamos con ello, mucho mejor. <a href="http://jonsegador.com/category/symfony2/">Symfony2</a> mejora a su predecesor en numerosos aspectos y <strong>se convertirá en el framework definitivo para el desarrollo de aplicaciones web</strong> en el futuro. Si quieres saber todas las nuevas características de esta versión, te recomiendo que visites la web oficial de Symfony2: <a href="http://symfony.com/">http://symfony.com/</a>.</p>
<p><strong>En este primer post procederemos a su instalación</strong>. Personalmente, me gusta programar en local (en mi propio ordenador) y una vez haya acabado con el desarrollo o quiera probarlo en un entorno de producción, hacer el deploy a un servidor real. Por ello, <strong>primero voy a explicar como configurar un servidor web en nuestro ordenador</strong> y después procederemos con la instalación de Symfony2.</p>
<p>La instalación la voy a hacer en un entorno Linux con la distribución Ubuntu 11.04. Adaptar los comandos según nuestra distribución (o sistema operativo).</p>
<p>1) Primero instalamos Apache</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;"># apt-get install apache2 apache2.2-common apache2-utils ssl-cert  apache2-mpm-prefork</pre></div></div>

<p>2) Instalamos y habilitamos el módulo para utilizar modrewrite</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;"># apt-get install libapache2-mod-proxy-html</pre></div></div>

<p>Para habilitar el modulo en apache, ejecutamos:</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;"># a2enmod rewrite</pre></div></div>

<p>3) Instalamos PHP5</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;"># apt-get install php5 libapache2-mod-php5 php5-common php5-intl</pre></div></div>

<p>4) Instalamos Mysql</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;"># apt-get install mysql-server mysql-client php5-mysql</pre></div></div>

<p>5) Por último instalamos phpmyadmin (opcional)</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;"># apt-get install phpmyadmin</pre></div></div>

<p>Con estos sencillos pasos ya tendremos un servidor web instalado en nuestro ordenador.</p>
<p>Ahora continuamos con la instalación de Symfony2. <strong>Mi IDE favorito para programar es <a href="http://jonsegador.com/2010/10/instalar-netbeans-con-soporte-svnssh-y-theme-de-colores-oscuros/">Netbeans</a></strong>, por lo que es el que utilizaré en este ejemplo. Si no lo hemos cambiado, nuestro workspace (donde se encuentran los proyectos a los que podemos acceder desde Netbeans) lo encontraremos en la carpeta NetBeansProjects en nuestra carpeta Home.</p>
<p>Procedemos a crear una nueva carpeta con el nombre de nuestro proyecto en el workspace de Netbeans:</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">$ <span style="color: #b1b100; font-weight: bold;">mkdir</span> $HOME/NetBeansProjects/miproyecto</pre></div></div>

<p>Entramos en la carpeta que acabamos de crear:</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">$ <span style="color: #b1b100; font-weight: bold;">cd</span> $HOME/NetBeansProjects/miproyecto</pre></div></div>

<p>Descargamos Symfony2 (comprueba la última versión en <a href="http://symfony.com/download">http://symfony.com/download</a>):</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">$ wget http://symfony.com/download?v=Symfony_Standard_Vendors_2.0.0-RC4.tgz</pre></div></div>

<p>Y descomprimimos:</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">$ tar zxvf Symfony_Standard_Vendors_2.0.0-RC4.tgz</pre></div></div>

<p>Borramos el archivo comprimido que hemos descargado:</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">$ rm Symfony_Standard_Vendors_2.0.0-RC4.tgz</pre></div></div>

<p><strong>Ya tenemos instalado un proyecto básico en Symfony2</strong>. Ahora vamos a configurar Apache para poder acceder desde nuestro navegador favorito.</p>
<p>Creamos un nuevo archivo donde irá la configuración de nuestra web:</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;"># vi /etc/apache2/sites-available/miproyecto</pre></div></div>

<p>Que contendrá el siguiente contenido (cambia &#8220;jonseg&#8221; por el nombre de tu carpeta Home):</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">&lt;VirtualHost *:<span style="color: #cc66cc;">80</span>&gt;
   ServerAdmin email<span style="color: #33cc33;">@</span>email.com
   DocumentRoot /home/jonseg/NetBeansProjects/miproyecto/Symfony/web
   ServerName miproyecto.com
   ServerAlias *.miproyecto.com
   ErrorLog /var/log/apache2/miproyecto_error.log
   CustomLog /var/log/apache2/miproyecto_access.log common
   DirectoryIndex index.php
   &lt;Directory &quot;/home/jonseg/NetBeansProjects/miproyecto/Symfony/web&quot;&gt;
        Options Indexes FollowSymLinks
        Order Allow,Deny
        Allow from all
        AllowOverride all
        &lt;IfModule mod_php5.c&gt;
           php_admin_flag engine on
           php_admin_flag safe_mode off
           php_admin_value open_basedir none
        &lt;/ifModule&gt;
   &lt;/Directory&gt;
&lt;/VirtualHost&gt;</pre></div></div>

<p>Creamos un enlace simbólico de este archivo en sites-enabled para indicar que es una web activa:</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;"># ln -s /etc/apache2/sites-available/miproyecto /etc/apache2/sites-enabled/miproyecto</pre></div></div>

<p>Para que se apliquen los cambios, reiniciamos el servidor Apache:</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;"># /etc/init.d/apache2 restart</pre></div></div>

<p>Por último, forzamos en el archivo /etc/hosts la ip de nuestra web, que en este caso será localhost o 127.0.0.1</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;"># vim /etc/hosts</pre></div></div>

<p>Y añadimos esta línea al final del fichero:</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">127.0.0.1 www.miproyecto.com miproyecto.com</pre></div></div>

<p>Después de todo esto, <strong>ya podremos acceder a nuestro nuevo proyecto en Symfony2 desde el navegador</strong> escribiendo la url que hemos configurado.</p>
<p>Como primer paso, debemos comprobar si cumplimos con la configuración básica para que funcione nuestro proyecto. Lo haremos desde la siguiente url:</p>
<p>http://miproyecto.com/config.php</p>
<p><a href="http://jonsegador.com/wp-content/symfony2_config.png"><img src="http://jonsegador.com/wp-content/symfony2_config.png" alt="Symfony2 config" /></a></p>
<p>Como vemos, nos encontramos con dos problemas (podríamos tener más, deberemos resolverlos según sea necesario). En este caso el error es que las carpetas app/cache y app/logs no tienen los permisos necesarios.</p>
<p>Lo arreglaremos así:</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">chmod <span style="color: #cc66cc;">777</span> $HOME/NetBeansProjects/miproyecto/Symfony/app/cache
chmod <span style="color: #cc66cc;">777</span> $HOME/NetBeansProjects/miproyecto/Symfony/app/logs</pre></div></div>

<p>Una vez arreglados los problemas, ya podemos acceder a nuestro proyecto web con Symfony2 mediante esta url:</p>
<p>http://miproyecto.com/app_dev.php/</p>
<p><a href="http://jonsegador.com/wp-content/symfony2_home.png"><img src="http://jonsegador.com/wp-content/symfony2_home.png" alt="Symfony2 home" /></a></p>
<p>En próximos artículos nos iremos adentrando poco a poco en el desarrollo de Bundles, las plantillas con Twig, etc.</p>
<p>Cualquier duda, en los comentarios!</p>
]]></content:encoded>
			<wfw:commentRss>http://jonsegador.com/2011/07/instalar-symfony2-desde-cero/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Relaciones en Symfony 1.4 con Doctrine</title>
		<link>http://jonsegador.com/2011/06/relaciones-en-symfony-1-4-con-doctrine/</link>
		<comments>http://jonsegador.com/2011/06/relaciones-en-symfony-1-4-con-doctrine/#comments</comments>
		<pubDate>Thu, 16 Jun 2011 22:42:09 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Bases de datos]]></category>
		<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[base de datos]]></category>
		<category><![CDATA[doctrine]]></category>
		<category><![CDATA[relaciones]]></category>

		<guid isPermaLink="false">http://jonsegador.com/?p=436</guid>
		<description><![CDATA[Una de las dificultades que podemos encontrar al utilizar el ORM Doctrine (por ejemplo, en Symfony) es configurar las relaciones entre tablas en el schema.yml que define nuestra base de datos. A modo de ejemplo, voy a mostrar los tres tipos de relaciones que podemos querer configurar: Relaciones uno a uno Un caso típico para [...]]]></description>
			<content:encoded><![CDATA[<p>Una de las dificultades que podemos encontrar al utilizar el ORM <a href="http://jonsegador.com/tag/doctrine/">Doctrine</a> (por ejemplo, en <a href="http://jonsegador.com/category/symfony/">Symfony</a>) es configurar las relaciones entre tablas en el schema.yml que define nuestra <a href="http://jonsegador.com/category/bases-de-datos/">base de datos</a>. A modo de ejemplo, voy a mostrar los tres tipos de relaciones que podemos querer configurar:</p>
<h4><strong>Relaciones uno a uno</strong></h4>
<p>Un caso típico para este tipo de relación es el de un usuario y su perfil. <strong>Un usuario solamente tiene un perfil y un perfil solo puede pertenecer a un usuario.</strong></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">User:
  tableName: user
  columns:
    id:
      type: integer(4)
      primary: true
      notnull: true
      autoincrement: true
    email:
      type: string(255)
      notnull: true
      unique: true
    password:
      type: string(45)
&nbsp;
Profile:
  tableName: profile
  columns:
    user_id:
      type: integer(4)
      primary: true
      notnull: true
      autoincrement: false
    nickname: string(255)
  relations:
    User:
      class: User
      local: user_id
      foreign: id
      foreignAlias: Profile
      type: one
      foreignType: one</pre></div></div>

<p>Con esta relación, podremos acceder al perfil de un usuario así:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Profile</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nickname</span> <span style="color: #666666; font-style: italic;">// obtenemos el nickname del perfil del usuario</span></pre></div></div>

<h4><strong>Relaciones uno a muchos:</strong></h4>
<p>Un ejemplo de este tipo de relación es el de los usuarios y sus números de teléfono. <strong>Un usuario puede tener varios números de teléfono pero un teléfono solo puede pertenecer a un usuario.</strong></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">User:
  tableName: user
  columns:
    id:
      type: integer(4)
      primary: true
      notnull: true
      autoincrement: true
    email:
      type: string(255)
      notnull: true
      unique: true
    password:
      type: string(45)
&nbsp;
Telephone:
  tableName: telephone
  columns:
    id:
      type: integer(4)
      primary: true
      notnull: true
      autoincrement: true
    user_id:
      type: integer(4)
      notnull: true
    number:
      type: string(255)
  relations:
    User:
      class: User
      local: user_id
      foreign: id
      foreignAlias: Telephones
      type: one
      foreignType: many</pre></div></div>

<p>Para acceder a los números de teléfono de un usuario, podemos hacer:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Telephones</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$telephone</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$telephone</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">number</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h4><strong>Relaciones muchos a muchos</strong></h4>
<p>Para terminar, un caso típico de esta relación es la que podemos encontrar entre los artículos de un blog y sus etiquetas. <strong>Un artículo puede tener varias etiquetas y una etiqueta puede asignarse a varios artículos.</strong> En esta relación necesitaremos obligatoriamente una tercera tabla (article_tags) para poder relacionar la tabla articles y la tabla tags.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">Article:
  tableName: articles
  columns:
    id:
      type: integer(4)
      primary: true
      notnull: true
      autoincrement: true
    title:
      type: string(100)
      notnull: true
&nbsp;
Tag:
  tableName: tags
  columns:
    id:
      type: integer(4)
      primary: true
      notnull: true
      autoincrement: true
    name:
      type: string(100)
      notnull: true
  relations:
    Article:
      foreignAlias: Tags
      class: Article
      refClass: ArticleTag
&nbsp;
ArticleTag:
  tableName: article_tags
  columns:
    article_id:
      type: integer(4)
      primary: true
    tag_id:
      type: integer(4)
      primary: true
  relations:
    Article:
      foreignAlias: ArticleTags
    Tag:
      foreignAlias: ArticleTags</pre></div></div>

<p>Podremos obtener el nombre de todos los tags de un artículo de la siguiente manera:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ArticleTags</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$article_tag</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$article_tag</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Tag</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>¡Cualquier duda, en los comentarios!</p>
]]></content:encoded>
			<wfw:commentRss>http://jonsegador.com/2011/06/relaciones-en-symfony-1-4-con-doctrine/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Symfony 1.4 task error: The default context does not exist</title>
		<link>http://jonsegador.com/2011/05/symfony-1-4-task-error-the-default-context-does-not-exist/</link>
		<comments>http://jonsegador.com/2011/05/symfony-1-4-task-error-the-default-context-does-not-exist/#comments</comments>
		<pubDate>Wed, 11 May 2011 23:19:12 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[context]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[sfContext]]></category>
		<category><![CDATA[task]]></category>

		<guid isPermaLink="false">http://jonsegador.com/?p=427</guid>
		<description><![CDATA[Existe un problema bastante frecuente cuando ejecutamos un task en Symfony 1.4, pareciéndonos el siguiente error: &#8220;The default context does not exist&#8221; El problema es que estás utilizando un método de un objeto que utiliza sfContext (para recuperar valores del archivo app.yml, por ejemplo). Parece ser que Symfony no crea una instancia de sfContext y [...]]]></description>
			<content:encoded><![CDATA[<p>Existe un problema bastante frecuente cuando ejecutamos un <strong>task</strong> en <strong>Symfony 1.4</strong>, pareciéndonos el siguiente error:</p>
<p><strong>&#8220;The default context does not exist&#8221;</strong></p>
<p>El problema es que estás utilizando un método de un objeto que utiliza <strong>sfContext</strong> (para recuperar valores del archivo app.yml, por ejemplo). Parece ser que Symfony no crea una instancia de sfContext y nos devuelve este error.</p>
<p>La siguiente línea debería resolver el problema. La colocaremos en primer lugar dentro de la función execute:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">sfContext<span style="color: #339933;">::</span><span style="color: #004000;">createInstance</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">configuration</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Por ejemplo:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">protected <span style="color: #000000; font-weight: bold;">function</span> execute <span style="color: #009900;">&#40;</span><span style="color: #000088;">$arguments</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  sfContext<span style="color: #339933;">::</span><span style="color: #004000;">createInstance</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">configuration</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// Aqui nuestro codigo</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://jonsegador.com/2011/05/symfony-1-4-task-error-the-default-context-does-not-exist/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Formulario de login (y logout) básico en Symfony 1.4</title>
		<link>http://jonsegador.com/2011/03/formulario-de-login-y-logout-basico-en-symfony-1-4/</link>
		<comments>http://jonsegador.com/2011/03/formulario-de-login-y-logout-basico-en-symfony-1-4/#comments</comments>
		<pubDate>Thu, 03 Mar 2011 09:44:17 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[formularios]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[logout]]></category>
		<category><![CDATA[registro]]></category>

		<guid isPermaLink="false">http://jonsegador.com/?p=371</guid>
		<description><![CDATA[En el post anterior creamos un sistema de registro de usuarios básico para Symfony 1.4. Lo que vamos a hacer ahora es un sistema de login (y logout) para que el usuario pueda iniciar sesión en nuestra web. Primero creamos el formulario de login, por lo que creamos un nuevo archivo: /lib/form/LoginForm.class.php 1 2 3 [...]]]></description>
			<content:encoded><![CDATA[<p>En el post anterior creamos un sistema de <a href="http://jonsegador.com/2011/03/formulario-de-registro-basico-en-symfony-1-4/"><strong>registro de usuarios básico para Symfony 1.4</strong></a>. Lo que vamos a hacer ahora es un <strong>sistema de login (y logout) para que el usuario pueda iniciar sesión en nuestra web</strong>.</p>
<p>Primero creamos el formulario de login, por lo que creamos un nuevo archivo:</p>
<p>/lib/form/LoginForm.class.php</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> LoginForm <span style="color: #000000; font-weight: bold;">extends</span> BaseForm
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> configure<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    parent<span style="color: #339933;">::</span><span style="color: #004000;">configure</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setWidgets</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'email'</span>        <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormInput<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'password'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormInputPassword<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setValidators</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'email'</span>        <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfValidatorEmail<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'required'</span><span style="color: #339933;">=&gt;</span>true<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'required'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'El email es obligatorio'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'password'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfValidatorString<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'required'</span><span style="color: #339933;">=&gt;</span>true<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'required'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Escribe tu contraseña'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">widgetSchema</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setNameFormat</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'login[%s]'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">widgetSchema</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFormFormatterName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'list'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>El formulario no tiene mayor misterior. Un input text para el email y un input password para la contraseña. Validamos que se hayan rellenado los dos campos.</p>
<p>Editamos las actions del módulo user que creamos en el post anterior para añadir el login.</p>
<p>/apps/frontend/modules/user/actions/actions.class.php</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> userActions <span style="color: #000000; font-weight: bold;">extends</span> sfActions
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeLogin<span style="color: #009900;">&#40;</span>sfWebRequest <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LoginForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;post&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;login&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
           <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> UserTable<span style="color: #339933;">::</span><span style="color: #004000;">login</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;email&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
               <span style="color: #666666; font-style: italic;">// No hemos conseguido loguear al usuario</span>
               <span style="color: #666666; font-style: italic;">// Redirigimos de nuevo al login con un mensaje de error</span>
               <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;datos incorrectos&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;@user_login&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
           <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
               <span style="color: #666666; font-style: italic;">// Logueamos</span>
               <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAuthenticated</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
               <span style="color: #666666; font-style: italic;">// Comprobamos si tiene referer, si no, le llevamos a la homepage</span>
               <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;referer&quot;</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>?<span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;@homepage&quot;</span><span style="color: #339933;">;</span>
               <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;referer&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
           <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Tenemos que comprobar si existe ese email junto con esa contraseña en nuestra base de datos. Si es así, logueamos al usuario y, si no, mostramos un mensaje de error.<br />
La comprobación de si existe el usuario en la base de datos la hacemos en la linea 12, donde llamamos al método estático &#8220;login&#8221; del archivo UserTable.class.php del modelo.</p>
<p>/lib/model/doctrine/UserTable.class.php</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> UserTable <span style="color: #000000; font-weight: bold;">extends</span> Doctrine_Table
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> login<span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #339933;">,</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> Doctrine_Query<span style="color: #339933;">::</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User u'</span><span style="color: #009900;">&#41;</span>
	<span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'u.email = ?'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">andWhere</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'u.password = ?'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// Podrimos usar otro algoritmo, en este caso utilizamos md5</span>
	<span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchOne</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Y eso es todo, lo más sencillo para el final: la plantilla donde mostramos el formulario.</p>
<p>/apps/frontend/modules/user/templates/loginSuccess.php</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sf_user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    &lt;div class=&quot;error&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$sf_user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFlash</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/div&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;form action=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> url_for<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;@user_login&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; method=&quot;post&quot;&gt;
  <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
  &lt;input type=&quot;submit&quot; value=&quot;Entrar&quot; /&gt;
&lt;/form&gt;</pre></td></tr></table></div>

<p>En este paso sería lógico desarrollar el logout para poder cerrar la sesión en la web. Para ello modificamos de nuevo la actions de nuestro módulo para añadir la nueva funcionalidad:</p>
<p>/apps/frontend/modules/user/actions/actions.class.php</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> userActions <span style="color: #000000; font-weight: bold;">extends</span> sfActions
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeLogout<span style="color: #009900;">&#40;</span>sfWebRequest <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAuthenticated</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttributeHolder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">clear</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;@homepage&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Para ejecutar el logout, solo debemos crear un enlace en nuestra web donde se llame a esta action. No necesitamos plantilla ya que después de logout siempre redirigiremos a la homepage.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> link_to<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cerrar sesión&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'@user_logout'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Debemos añadir las siguientes rutas en el archivo routing.yml:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">user_login:
  url:   /login
  param: { module: user, action: login }
&nbsp;
user_logout:
  url:   /logout
  param: { module: user, action: logout }</pre></div></div>

<p>Cualquier duda en los comentarios!</p>
]]></content:encoded>
			<wfw:commentRss>http://jonsegador.com/2011/03/formulario-de-login-y-logout-basico-en-symfony-1-4/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Formulario de registro básico en Symfony 1.4</title>
		<link>http://jonsegador.com/2011/03/formulario-de-registro-basico-en-symfony-1-4/</link>
		<comments>http://jonsegador.com/2011/03/formulario-de-registro-basico-en-symfony-1-4/#comments</comments>
		<pubDate>Tue, 01 Mar 2011 10:36:43 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[formularios]]></category>
		<category><![CDATA[registro]]></category>

		<guid isPermaLink="false">http://jonsegador.com/?p=392</guid>
		<description><![CDATA[Es dificil no desarrollar una web sin tener registro de usuarios. Para Symfony 1.4 podemos encontrar gran cantidad de plugins que se encargan de esta función como pueden ser sfGuardPlugin (para Propel) y sfDoctrineGuardPlugin (para Doctrine). Estos plugins son muy completos y útiles, pudiéndolos usar en cualquiera de nuestros proyectos donde necesitemos las operaciones básicas [...]]]></description>
			<content:encoded><![CDATA[<p>Es dificil no desarrollar una web sin tener registro de usuarios. Para Symfony 1.4 podemos encontrar gran cantidad de plugins que se encargan de esta función como pueden ser <strong>sfGuardPlugin (para Propel)</strong> y <strong>sfDoctrineGuardPlugin (para Doctrine)</strong>. Estos plugins son muy completos y útiles, pudiéndolos usar en cualquiera de nuestros proyectos donde necesitemos las operaciones básicas con usuarios (registro, login, etc).</p>
<p>Para este post, la intención es <strong>crear un formulario de registro de usuarios sencillo desde cero para Symfony 1.4</strong>, sin la complejidad de un plugin ya que muchas veces tienen funcionalidades que no necesitaremos en nuestro proyecto.</p>
<p>Antes de empezar, evidentemente, crearemos un proyecto nuevo y generamos un modulo llamado &#8220;user&#8221;.</p>
<p>Primero <strong>modificamos el archivo schema.yml</strong>, añadiendo una tabla user básica y sencilla.</p>
<p>/config/doctrine/schema.yml</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;">User:
  tableName: user
  actAs:
    Timestampable: ~
  columns:
    email:
      type: string(255)
    password:
      type: string(45)</pre></td></tr></table></div>

<p>Indicamos que se comporte como Timestampable para que se creen automáticamente los campos created_at y updated_at. No nos olvidemos generar las consultas sql y ejecutarlas en nuestra base de datos.</p>
<p>En el segundo paso, <strong>vamos a crear el formulario para el registro del usuario</strong>. Creamos un archivo nuevo en /lib/form/ llamado RegisterForm.class.php </p>
<p>/lib/form/RegisterForm.class.php</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> RegisterForm <span style="color: #000000; font-weight: bold;">extends</span> BaseForm
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> configure<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        parent<span style="color: #339933;">::</span><span style="color: #004000;">configure</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setWidgets</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	    <span style="color: #0000ff;">'email'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormInput<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	    <span style="color: #0000ff;">'password'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormInputPassword<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'password2'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormInputPassword<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setValidators</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'email'</span>        <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfValidatorEmail<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'required'</span><span style="color: #339933;">=&gt;</span>true<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'required'</span><span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;El email es obligatorio&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'password'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfValidatorString<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'required'</span><span style="color: #339933;">=&gt;</span>true<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'required'</span><span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;La contraseña es obligatoria&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'password2'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfValidatorString<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'required'</span><span style="color: #339933;">=&gt;</span>true<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'required'</span><span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;La contraseña es obligatoria&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">widgetSchema</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setNameFormat</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'register[%s]'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">widgetSchema</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFormFormatterName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'list'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validatorSchema</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setPostValidator</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> sfValidatorAnd<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #000000; font-weight: bold;">new</span> sfValidatorSchemaCompare<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'password2'</span><span style="color: #339933;">,</span> sfValidatorSchemaCompare<span style="color: #339933;">::</span><span style="color: #004000;">EQUAL</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'password'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'throw_global_error'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'invalid'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Las dos contraseñas no coinciden&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            <span style="color: #000000; font-weight: bold;">new</span> sfValidatorDoctrineUnique<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'model'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'User'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'column'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'invalid'</span><span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Este email ya está en uso&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Los formularios pueden darnos muchos quebraderos de cabeza al principio ya que es algo dificil de entender a la primera, por lo que voy a explicarlo un poco más en detalle.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setWidgets</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'email'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormInput<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'password'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormInputPassword<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'password2'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormInputPassword<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Indicamos que para el campo email queremos un input normal. Como se puede apreciar, le decimos que para los campos contraseña queremos un campo input password.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setValidators</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'email'</span>        <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfValidatorEmail<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'required'</span><span style="color: #339933;">=&gt;</span>true<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'required'</span><span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;El email es obligatorio&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'password'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfValidatorString<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'required'</span><span style="color: #339933;">=&gt;</span>true<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'required'</span><span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;La contraseña es obligatoria&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'password2'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> sfValidatorString<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'required'</span><span style="color: #339933;">=&gt;</span>true<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'required'</span><span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;La contraseña es obligatoria&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>Ahora los validadores</strong>. El campo email, evidentemente, debe ser un email. <strong>El validador sfValidatorEmail se encarga de comprobar que sea un formato de email válido</strong>.<br />
<strong>Los campos password son obligatorios y no pueden estar vacíos</strong>. Podríamos decirle que tienen que tener una longitud mínima y máxima con las opciones max_length y min_length.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validatorSchema</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setPostValidator</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> sfValidatorAnd<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #000000; font-weight: bold;">new</span> sfValidatorSchemaCompare<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'password2'</span><span style="color: #339933;">,</span> sfValidatorSchemaCompare<span style="color: #339933;">::</span><span style="color: #004000;">EQUAL</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'password'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'throw_global_error'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'invalid'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Las dos contraseñas no coinciden&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #000000; font-weight: bold;">new</span> sfValidatorDoctrineUnique<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'model'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'User'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'column'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'invalid'</span><span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Este email ya está en uso&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Vamos a añadir unos validadores un poco más complejos. En el primero, comprobamos que <strong>los dos campos de contraseña deben ser iguales</strong>. Para ello <strong>utilizamos el validador sfValidatorSchemaCompare</strong>. Es típico hacer esta comprobación en todos los formularios de registro de usuarios.<br />
En el segundo caso, comprobamos que <strong>el email introducido por el usuario no esté ya introducido en la base de datos</strong> y, por tanto, en uso por otro usuario. Utilizamos el validador <strong>sfValidatorDoctrineUnique</strong>.</p>
<p>Ya tenemos el formulario preparado para su uso. Editamos el archivo actions.class.php de nuestro módulo &#8220;user&#8221; y añadimos la acción &#8220;Register&#8221;:</p>
<p>/apps/frontend/modules/user/actions/actions.class.php</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> userActions <span style="color: #000000; font-weight: bold;">extends</span> sfActions
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeRegister<span style="color: #009900;">&#40;</span>sfWebRequest <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirectIf</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isAuthenticated</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;@homepage&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RegisterForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;post&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
       <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;register&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
           <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
           <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">email</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;email&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
           <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">password</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// podriamos utilizar otro sistema para encriptar la contraseña</span>
           <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
           <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;referer&quot;</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>?<span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;@homepage&quot;</span><span style="color: #339933;">;</span>
           <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;referer&quot;</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
           <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>En la línea 7 le indicamos que si el usuario ya está logueado le redirija a la homepage.</p>
<p>Hemos utilizado el algoritmo md5 para encriptar la contraseña antes de guardarla en la base de datos. Podríamos utilizar otro algoritmo como sha1 o uno propio. <strong>Es totalmente recomendable encriptar las contraseñas y no guardarlas en texto plano en nuestra base de datos por motivos de seguridad.</strong></p>
<p>Por último, creamos la plantilla donde se mostrará el formulario de registro.</p>
<p>/apps/frontend/modules/user/templates/registerSuccess.php</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&lt;form action=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> url_for<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;@user_register&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; method=&quot;post&quot;&gt;
  <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$form</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
  &lt;input type=&quot;submit&quot; value=&quot;Registrarse&quot; /&gt;
&lt;/form&gt;</pre></td></tr></table></div>

<p>Debemos añadir la siguiente ruta en el archivo routing.yml:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">user_register:
  url:   /register
  param: { module: user, action: register }</pre></div></div>

<p>Y eso es todo, ya hemos creado un <strong>sistema muy básico de registro de usuarios en Symfony 1.4</strong>. A partir de aquí se puede expandir e incluir las funcionalidades que queramos.<br />
<a href="http://jonsegador.com/2011/03/formulario-de-login-y-logout-basico-en-symfony-1-4/"><strong>En un próximo post, realizaremos el login y el logout.</strong></a></p>
<p>Cualquier duda o si hay que explicar algo mejor, en los comentarios <img src='http://jonsegador.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://jonsegador.com/2011/03/formulario-de-registro-basico-en-symfony-1-4/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Error con la función token_get_all() al crear proyecto con Symfony 1.4</title>
		<link>http://jonsegador.com/2011/02/error-con-la-funcion-token_get_all-al-crear-proyecto-con-symfony-1-4/</link>
		<comments>http://jonsegador.com/2011/02/error-con-la-funcion-token_get_all-al-crear-proyecto-con-symfony-1-4/#comments</comments>
		<pubDate>Thu, 10 Feb 2011 00:02:12 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Servidores]]></category>
		<category><![CDATA[opensuse]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php5-tokenizer]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[zypper]]></category>

		<guid isPermaLink="false">http://jonsegador.com/?p=354</guid>
		<description><![CDATA[Cuando tenemos un servidor recién instalado, nos podemos encontrar con que algunos paquetes necesarios no están instalados en el sistema. Esto me ha ocurrido al intentar generar un nuevo proyecto con Symfony 1.4 en un servidor con OpenSuse 11.1. Al intentar crear un nuevo proyecto con el comando: php symfony generate:project miproyecto La orden no [...]]]></description>
			<content:encoded><![CDATA[<p>Cuando tenemos un servidor recién instalado, nos podemos encontrar con que algunos paquetes necesarios no están instalados en el sistema. Esto me ha ocurrido al intentar generar un nuevo proyecto con <strong>Symfony 1.4</strong> en un servidor con <strong>OpenSuse 11.1</strong>.</p>
<p>Al intentar crear un nuevo proyecto con el comando:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">php symfony generate:project miproyecto</pre></div></div>

<p>La orden no se ejecutaba en su totalidad y me dejaba unos &#8220;bonitos&#8221; mensajes de error, relacionados con la función <strong>token_get_all()</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">PHP Notice:  Use of undefined constant T_FINAL - assumed <span style="color: #ff0000;">'T_FINAL'</span> <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>symfony14<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>util<span style="color: #000000; font-weight: bold;">/</span>sfClassManipulator.class.php on line <span style="color: #000000;">52</span>
PHP Notice:  Use of undefined constant T_ABSTRACT - assumed <span style="color: #ff0000;">'T_ABSTRACT'</span> <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>symfony14<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>util<span style="color: #000000; font-weight: bold;">/</span>sfClassManipulator.class.php on line <span style="color: #000000;">52</span>
PHP Notice:  Use of undefined constant T_STATIC - assumed <span style="color: #ff0000;">'T_STATIC'</span> <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>symfony14<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>util<span style="color: #000000; font-weight: bold;">/</span>sfClassManipulator.class.php on line <span style="color: #000000;">52</span>
PHP Notice:  Use of undefined constant T_PUBLIC - assumed <span style="color: #ff0000;">'T_PUBLIC'</span> <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>symfony14<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>util<span style="color: #000000; font-weight: bold;">/</span>sfClassManipulator.class.php on line <span style="color: #000000;">52</span>
PHP Notice:  Use of undefined constant T_PROTECTED - assumed <span style="color: #ff0000;">'T_PROTECTED'</span> <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>symfony14<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>util<span style="color: #000000; font-weight: bold;">/</span>sfClassManipulator.class.php on line <span style="color: #000000;">52</span>
PHP Notice:  Use of undefined constant T_PRIVATE - assumed <span style="color: #ff0000;">'T_PRIVATE'</span> <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>symfony14<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>util<span style="color: #000000; font-weight: bold;">/</span>sfClassManipulator.class.php on line <span style="color: #000000;">52</span>
PHP Notice:  Use of undefined constant T_FUNCTION - assumed <span style="color: #ff0000;">'T_FUNCTION'</span> <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>symfony14<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>util<span style="color: #000000; font-weight: bold;">/</span>sfClassManipulator.class.php on line <span style="color: #000000;">52</span>
PHP Fatal error:  Call to undefined <span style="color: #000000; font-weight: bold;">function</span> token_get_all<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>symfony14<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>util<span style="color: #000000; font-weight: bold;">/</span>sfClassManipulator.class.php on line <span style="color: #000000;">117</span></pre></div></div>

<p>Este problema con la función <a href="http://es.php.net/manual/es/function.token-get-all.php">token_get_all()</a> lo solucionaremos instalando la extensión para php &#8220;php5-tokenizer&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">zypper <span style="color: #c20cb9; font-weight: bold;">install</span> php5-tokenizer</pre></div></div>

<p>Me he encontrado este error al generar un nuevo proyecto con Symfony pero nada tiene que ver con el framework. Quizás para cualquier otro proyecto donde no utilicemos Symfony no nos haga falta instalarlo y no lo echemos de menos. Visto lo visto, con Symfony es totalmente necesario tener esta extensión instalada.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonsegador.com/2011/02/error-con-la-funcion-token_get_all-al-crear-proyecto-con-symfony-1-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Transacciones MySQL con Doctrine y Symfony</title>
		<link>http://jonsegador.com/2010/11/transacciones-mysql-con-doctrine-y-symfony/</link>
		<comments>http://jonsegador.com/2010/11/transacciones-mysql-con-doctrine-y-symfony/#comments</comments>
		<pubDate>Mon, 22 Nov 2010 09:19:01 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Bases de datos]]></category>
		<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[Seguridad]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[doctrine]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[transacción]]></category>
		<category><![CDATA[transacciones]]></category>

		<guid isPermaLink="false">http://jonsegador.com/?p=320</guid>
		<description><![CDATA[Lo primero de todo, ¿qué es una transacción MySQL? Una transacción en MySQL es la combinación de una o más consultas SQL que juntas forman una unidad de trabajo. El ejemplo clásico es el movimiento de dinero en un banco entre dos cuentas bancarias. Si la persona A quiere hacer una transferencia bancaria a la [...]]]></description>
			<content:encoded><![CDATA[<p>Lo primero de todo, <strong>¿qué es una transacción MySQL?</strong> Una transacción en MySQL es la combinación de una o más consultas SQL que juntas forman una unidad de trabajo. El ejemplo clásico es el movimiento de dinero en un banco entre dos cuentas bancarias. Si la persona A quiere hacer una transferencia bancaria a la persona B, habrá dos consultas: 1) la que descuenta el dinero a la persona A y 2) la que aumenta el saldo de la persona B. Si falla la consulta 2, tenemos un problema: Le habremos descontado el dinero a la persona A pero la persona B nunca recibirá su dinero.</p>
<p>En casos como estos es necesario tomar estas dos consultas como un todo y si falla una de las dos operaciones, volver atrás y deshacer la operación. Para ello se utilizan las transacciones.</p>
<p>En este ejemplo vamos a ver como realizar transacciones con el <a href="http://www.doctrine-project.org/"><strong>ORM Doctrine</strong></a> y <a href="http://www.symfony-project.org/"><strong>Symfony</strong></a> (no necesariamente debemos utilizar Symfony).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$conn</span> <span style="color: #339933;">=</span> Doctrine_Manager<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCurrentConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$conn</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">beginTransaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
try<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/** Hacemos las operaciones necesarias
      * para realizar la transferencia bancaria */</span>
&nbsp;
    <span style="color: #000088;">$userA</span> <span style="color: #339933;">=</span> UserTable<span style="color: #339933;">::</span><span style="color: #004000;">getById</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$userA_id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$userB</span> <span style="color: #339933;">=</span> UserTable<span style="color: #339933;">::</span><span style="color: #004000;">getById</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$userB_id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$userA</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">money</span> <span style="color: #339933;">-=</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$userA</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$userB</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">money</span> <span style="color: #339933;">+=</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$userB</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$conn</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">commit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$conn</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rollBack</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    throw <span style="color: #000088;">$e</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Si las dos operaciones se realizan con éxito, quedará reflejado en la base de datos. Si por el contrario cualquiera de las dos consultas fallará, se desharán los cambios (rollBack) y no se guardarán en la base de datos (mostramos el error y deberemos tratarlo según nuestros intereses).</p>
]]></content:encoded>
			<wfw:commentRss>http://jonsegador.com/2010/11/transacciones-mysql-con-doctrine-y-symfony/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Logs manuales y personalizados en Symfony</title>
		<link>http://jonsegador.com/2010/07/logs-manuales-personalizados-en-symfony/</link>
		<comments>http://jonsegador.com/2010/07/logs-manuales-personalizados-en-symfony/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 07:17:39 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[logs]]></category>

		<guid isPermaLink="false">http://jonsegador.com/?p=247</guid>
		<description><![CDATA[Suele ser una buena práctica tener un archivo de logs con las últimas acciones más importantes que se han hecho en nuestro sitio web: nuevo usuario registrado, se ha escrito un nuevo comentario, se ha editado o borrado un artículo, etc. Symfony tiene un sistema de logs bastante decente, que nos informa de todo lo [...]]]></description>
			<content:encoded><![CDATA[<p>Suele ser una buena práctica tener un archivo de logs con las últimas acciones más importantes que se han hecho en nuestro sitio web: nuevo usuario registrado, se ha escrito un nuevo comentario, se ha editado o borrado un artículo, etc.</p>
<p>Symfony tiene un sistema de logs bastante decente, que nos informa de todo lo que ocurre cada vez que cargamos una nueva página. Vamos a hacer algo sencillo: <strong>vamos a escribir nuestros propios mensajes de log personalizados y los escribiremos manualmente</strong> en el momento que consideremos oportuno y deseamos tener un control. Además, <strong>escribiremos estos logs en un archivo aparte</strong> del que utiliza Symfony por defecto. Así podremos leer directamente ese archivo sin tener otros logs mezclados con los que realmente nos interesa.</p>
<p>Primero, vamos a crear un nuevo archivo llamado CustomLog.class.php en la carpeta /lib/:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> CustomLog<span style="color: #009900;">&#123;</span>
  static <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> newLog<span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000088;">$logFile</span> <span style="color: #339933;">=</span> sfConfig<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sf_log_dir'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/custom_logs.log'</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$custom_log</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfFileLogger<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> sfEventDispatcher<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'file'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$logFile</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$custom_log</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">info</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>  
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>El archivo donde guardaremos nuestros logs personalizados, lo encontraremos en &#8220;/log/custom_logs.log&#8221;.</p>
<p>Para escribir un nuevo log desde cualquier parte del código, simplemente deberemos hacer:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">CustomLog<span style="color: #339933;">::</span><span style="color: #004000;">newLog</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Escribimos un nuevo log&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Si abrimos nuestro archivo de logs personalizados, encontraremos algo como esto:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">Jul <span style="color: #208080;">05</span> <span style="color: #cc66cc;">22</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">31</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">41</span> symfony <span style="color: #009900;">&#91;</span>info<span style="color: #009900;">&#93;</span> Escribimos un nuevo <span style="color: #990000;">log</span></pre></td></tr></table></div>

<p>Así podremos llevar un control de lo que ocurre en nuestra web.</p>
<p>Quizás haya una forma mejor de hacerlo. Si es así, no dudes en dejar tu propuesta en los comentarios. A lo mejor también sería conveniente escribir esas 3 lineas dentro de un try.. catch, por lo que pudiera pasar.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonsegador.com/2010/07/logs-manuales-personalizados-en-symfony/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

