<?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; Desarrollo</title>
	<atom:link href="http://jonsegador.com/category/desarrollo/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>Mon, 23 Jan 2012 19:24:37 +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>Atajos de teclado útiles en Netbeans para PHP</title>
		<link>http://jonsegador.com/2011/11/atajos-teclado-utiles-netbeans-php/</link>
		<comments>http://jonsegador.com/2011/11/atajos-teclado-utiles-netbeans-php/#comments</comments>
		<pubDate>Wed, 23 Nov 2011 17:57:54 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[atajos]]></category>
		<category><![CDATA[netbeans]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[phpdoc]]></category>
		<category><![CDATA[productividad]]></category>
		<category><![CDATA[programación]]></category>
		<category><![CDATA[teclado]]></category>

		<guid isPermaLink="false">http://jonsegador.com/?p=594</guid>
		<description><![CDATA[Netbeans es mi IDE de desarrollo favorito cuando estoy programando en PHP. Podemos mejorar nuestra productividad utilizando Netbeans haciendo uso de sus numerosos atajos de teclado que nos facilitará el trabajo. Aunque sé que hay bastantes más (y por supuesto para otros lenguajes), voy a intentar enumerar los más útiles que conozco. Si vosotros conocéis [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Netbeans</strong> es mi IDE de desarrollo favorito cuando estoy programando en PHP. Podemos <strong>mejorar nuestra productividad</strong> utilizando Netbeans haciendo uso de sus numerosos atajos de teclado que nos facilitará el trabajo. Aunque sé que hay bastantes más (y por supuesto para otros lenguajes), voy a intentar enumerar los más útiles que conozco. Si vosotros conocéis alguno más, no dudéis en escribir un comentario y lo añadiré al post.</p>
<ul>
<li><strong>[CTRL] + [SPACE]</strong> : Completa el código que estamos escribiendo. Por ejemplo, si queremos escribir LocateRegistry solamente escribimos Loc y presionamos el atajo para que nos sugiera la palabra completa. Si presionamos dos veces el atajo, muestra todas las opciones que comiencen con los caracteres escritos.</li>
<li><strong>[ALT] + [INSERT]</strong> : Agrega constructores, métodos accesores, propiedaes, overrides, etc..</li>
<li><strong>[ALT] + [ENTER]</strong> : Se usa sobre el texto subrayado para ver la sugerencia que nos hace el IDE cuando hay algún error de sintaxis o se necesita realizar algun import o surround</li>
<li><strong>[CTRL] + [R]</strong>: Si lo hacemos sobre el identificador, renombra todas las ocurrencias.</li>
<li>Generador de documentación <strong>PHPDoc</strong>: Si posicionamos el cursor en la línea anterior donde empieza la declaración de la función, escribimos <strong>/**</strong> y pulsamos [ENTER], automáticamente se nos generará la documentación PHPDoc de esa función.</li>
<li><strong>[ALT]+[SHIFT]+F</strong> : Tabula el código automáticamente.</li>
<li><strong>[SHIFT]+[ESC]</strong> : Maximiza o minimiza la ventana donde tecleamos el codigo.</li>
<li><strong>[CTRL]+B</strong> ó <strong>[CTRL]+(click)</strong> : Nos abre el archivo donde se encuentra la declaración de la variable/funcion/clase.</li>
<li><strong>[CTRL]+[TAB]</strong> : Lista de los archivos que tenemos abierto para cambiar de uno a otro rápidamente.</li>
<li><strong>[CTRL]+[SHIFT]+UP</strong> ó <strong>[CTRL]+[SHIFT]+DOWN</strong> : Copia la linea en la que estamos en la fila superior o inferior.</li>
<li><strong>[ALT]+[SHIFT]+UP</strong> ó <strong>[ALT]+[SHIFT]+DOWN</strong> : Moveremos la línea en la que nos encontramos hacia arriba o hacia abajo.</li>
<li><strong>[CTRL]+E</strong> : Borra la línea en la que estamos.</li>
<li><strong>[CTRL]+[SHIFT]+C</strong> : Comenta la línea actual. Si hacemos una selección, comentará todo el código seleccionado.</li>
<li><strong>[CTRL]+R</strong> : Renombramos una variable/función/clase y hace el cambio automáticamente en el resto del código (MUY útil).</li>
</ul>
<p>Vamos a ver a continuación unos atajos para escribir código mucho más rápido con <a href="http://jonsegador.com/tag/netbeans/">Netbeans</a>. Tan solo tendremos que escribir la primera palabra que se indica y pulsar la tecla del tabulador.</p>
<ul>
<li><strong>fore + [TAB]</strong> : Genera un bucle foreach típico.

<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;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

</li>
<li><strong>forek + [TAB]</strong> : Nos generará un bucle foreach, extrayendo también la clave del array.

<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;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

</li>
<li><strong>if + [TAB]</strong> : Autocompleta el código de un bloque if.

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</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: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

</li>
<li><strong>swi + [TAB]</strong> : Genera un bloque switch.

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$categoriesLlistat</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #000088;">$value</span><span style="color: #339933;">:</span>
&nbsp;
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
&nbsp;
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</li>
<li><strong>while + [TAB]</strong> : Escribirá automáticamente un bucle while.

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">while</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: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

</li>
<li><strong>my_fa + [TAB]</strong> : Genera un bucle while, extrayendo los datos de una consulta mysql en forma de array.

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

</li>
<li><strong>my_fo + [TAB]</strong> : Genera un bucle while, extrayendo los datos de una consulta mysql en forma de objeto.

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_object</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

</li>
<li><strong>my_fr + [TAB]</strong> : Genera un bucle while, extrayendo los datos de una consulta mysql en forma de filas.

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

</li>
</ul>
<p>También se pueden generar atajos de código definidos por nosotros mismos. Ese es un tema que da para otro post <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/11/atajos-teclado-utiles-netbeans-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Redireccionar a página de mantenimiento con htaccess, manteniendo los estilos css y las imágenes</title>
		<link>http://jonsegador.com/2011/11/redireccionar-pagina-mantenimiento-htaccess-manteniendo-estilos-css-imagenes/</link>
		<comments>http://jonsegador.com/2011/11/redireccionar-pagina-mantenimiento-htaccess-manteniendo-estilos-css-imagenes/#comments</comments>
		<pubDate>Sat, 12 Nov 2011 19:29:44 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[Servidores]]></category>
		<category><![CDATA[302]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[htaccess]]></category>
		<category><![CDATA[imágenes]]></category>
		<category><![CDATA[ip]]></category>
		<category><![CDATA[mantenimiento]]></category>
		<category><![CDATA[redirect]]></category>

		<guid isPermaLink="false">http://jonsegador.com/?p=575</guid>
		<description><![CDATA[Si vamos a realizar cambios en nuestra web y se tratan de cambios que pueden afectar al funcionamiento de la página (cambios en la base de datos, cambio de funcionalidades, etc), lo ideal es redirigir a una página de mantenimiento para informar a los usuarios mientras se realizan los cambios. En este caso lo vamos [...]]]></description>
			<content:encoded><![CDATA[<p>Si vamos a realizar cambios en nuestra web y se tratan de cambios que pueden afectar al funcionamiento de la página (cambios en la base de datos, cambio de funcionalidades, etc), lo ideal es redirigir a una <strong>página de mantenimiento</strong> para informar a los usuarios mientras se realizan los cambios. En este caso lo vamos a hacer editando el archivo <strong>.htaccess</strong> y dando <strong>permiso a nuestra ip para acceder</strong> a la web normalmente y poder ver los cambios. El resto de usuarios verán la página de mantenimiento a la que les redirigiremos.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="dos" style="font-family:monospace;">&lt;IfModule mod_rewrite.c&gt;
 RewriteEngine on
 RewriteCond <span style="color: #33cc33;">%</span><span style="color: #66cc66;">&#123;</span>REMOTE_ADDR<span style="color: #66cc66;">&#125;</span> !^<span style="color: #cc66cc;">999</span>\.999\.999\.999
 RewriteCond <span style="color: #33cc33;">%</span><span style="color: #66cc66;">&#123;</span>REQUEST_URI<span style="color: #66cc66;">&#125;</span> !/mantenimiento.html$ <span style="color: #66cc66;">&#91;</span>NC<span style="color: #66cc66;">&#93;</span>
 RewriteRule .* /mantenimiento.html <span style="color: #66cc66;">&#91;</span>R=<span style="color: #cc66cc;">302</span>,L<span style="color: #66cc66;">&#93;</span>
&lt;/IfModule&gt;</pre></td></tr></table></div>

<p>En la línea 3, cambiaremos la ip por la nuestra propia para poder acceder normalmente.<br />
La línea 4 indica que no redirija si ya estamos en la página de mantenimiento (para evitar un bucle infinito).<br />
Y en la línea 5 indicamos la ruta de la propia página de mantenimiento con una redirección 302 (<strong>redirección temporal</strong>).</p>
<p>Hasta aquí todo bien. El problema es que desde la página de mantenimiento no podemos acceder a las <strong>imágenes o estilos css</strong> porque también se ejecuta la redirección. Para evitar esto, añadiremos una línea más al código anterior:</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="dos" style="font-family:monospace;">&lt;IfModule mod_rewrite.c&gt;
 RewriteEngine on
 RewriteCond <span style="color: #33cc33;">%</span><span style="color: #66cc66;">&#123;</span>REMOTE_ADDR<span style="color: #66cc66;">&#125;</span> !^<span style="color: #cc66cc;">999</span>\.999\.999\.999
 RewriteCond <span style="color: #33cc33;">%</span><span style="color: #66cc66;">&#123;</span>REQUEST_URI<span style="color: #66cc66;">&#125;</span> !/mantenimiento.html$ <span style="color: #66cc66;">&#91;</span>NC<span style="color: #66cc66;">&#93;</span>
 RewriteCond <span style="color: #33cc33;">%</span><span style="color: #66cc66;">&#123;</span>REQUEST_URI<span style="color: #66cc66;">&#125;</span> !\.<span style="color: #66cc66;">&#40;</span>jpe?g?|png|gif|css<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#91;</span>NC<span style="color: #66cc66;">&#93;</span>
 RewriteRule .* /mantenimiento.html <span style="color: #66cc66;">&#91;</span>R=<span style="color: #cc66cc;">302</span>,L<span style="color: #66cc66;">&#93;</span>
&lt;/IfModule&gt;</pre></td></tr></table></div>

<p>Ahora todo debería funcionar correctamente. Cuando hayamos terminado de hacer los cambios, solo tendremos que comentar las líneas anteriores.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonsegador.com/2011/11/redireccionar-pagina-mantenimiento-htaccess-manteniendo-estilos-css-imagenes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Funciones anónimas (closures) en PHP 5.3</title>
		<link>http://jonsegador.com/2011/11/funciones-anonimas-closures-en-php-5-3/</link>
		<comments>http://jonsegador.com/2011/11/funciones-anonimas-closures-en-php-5-3/#comments</comments>
		<pubDate>Sat, 05 Nov 2011 17:50:43 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[closures]]></category>
		<category><![CDATA[funciones]]></category>
		<category><![CDATA[funciones anónimas]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://jonsegador.com/?p=567</guid>
		<description><![CDATA[Según la documentación oficial de PHP, las funciones anónimas (o closures) permiten la creación de funciones que no tienen un nombre especificado. Ejemplo sencillo de lo que quiere decir: $saludo = function&#40;$nombre&#41; &#123; printf&#40;&#34;Hola %s\r\n&#34;, $nombre&#41;; &#125;; &#160; $saludo&#40;'Mundo'&#41;; $saludo&#40;'PHP'&#41;; Pero en este ejemplo vamos a ir un poco más allá. Imaginemos que tenemos este [...]]]></description>
			<content:encoded><![CDATA[<p>Según la <a href="http://php.net/manual/es/functions.anonymous.php">documentación oficial de PHP</a>, las <strong>funciones anónimas</strong> (o <strong>closures</strong>) permiten la creación de funciones que no tienen un nombre especificado.</p>
<p>Ejemplo sencillo de lo que quiere decir:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$saludo</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$nombre</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hola <span style="color: #009933; font-weight: bold;">%s</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$nombre</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$saludo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Mundo'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$saludo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PHP'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Pero en este ejemplo vamos a ir un poco más allá.<br />
Imaginemos que tenemos este array:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$usuarios</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
             <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'nombre'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Jon'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cumple'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1984-02-17'</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;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'nombre'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Luis'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cumple'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1985-07-07'</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;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'nombre'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Antonio'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cumple'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1974-11-17'</span><span style="color: #009900;">&#41;</span>
         <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Si quisiéramos obtener los ids de cada usuario, haríamos algo parecido a esto:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$ids</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: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$usuarios</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$usuario</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$ids</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$usuario</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Pero hay una forma mucho más elegante de hacerlo <strong>utilizando las funciones anónimas</strong> que nos proporciona PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$ids</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_map</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$usuario</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$usuario</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$usuarios</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Mucho más limpio y elegante&#8230; Vamos a rizar más el rizo. Ahora queremos obtener los usuario que hayan nacido a partir de 1980. Normalmente haríamos algo así:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$usuarios_filtrados</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;">$fecha_inicial</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'1980-01-01'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$usuarios</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$usuario</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: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$usuario</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'cumple'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$fecha_inicial</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$usuarios_filtrados</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$usuario</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>En esta ocasión lo haremos utilizando de nuevo las <strong>funciones anónimas o closures</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$fecha_inicial</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'1980-01-01'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$usuarios_filtrados</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_filter</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$usuarios</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$usuario</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$fecha_inicial</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$usuario</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'cumple'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$fecha_inicial</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Y esto solo son algunos ejemplos. Las herramientas las tenemos ahí, depende de nosotros si las utilizamos o no <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/11/funciones-anonimas-closures-en-php-5-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Desloguear (logout) a un usuario mediante programación en Symfony2</title>
		<link>http://jonsegador.com/2011/10/desloguear-a-un-usuario-mediante-programacion-en-symfony2/</link>
		<comments>http://jonsegador.com/2011/10/desloguear-a-un-usuario-mediante-programacion-en-symfony2/#comments</comments>
		<pubDate>Tue, 25 Oct 2011 10:01:10 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[Symfony2]]></category>
		<category><![CDATA[logout]]></category>
		<category><![CDATA[redirect]]></category>
		<category><![CDATA[RedirectResponse]]></category>
		<category><![CDATA[setFlash]]></category>

		<guid isPermaLink="false">http://jonsegador.com/?p=562</guid>
		<description><![CDATA[¿Cómo se puede desloguear un usuario sin tener que acceder a la ruta correspondiente del logout? Para algunas funcionalidades que queramos añadir a nuestro proyecto, es posible que necesitemos desloguear al usuario. Un ejemplo podría ser el caso de desactivar/borrar su cuenta, pero puede que haya más (todo depende de nuestra imaginación ). Vamos a [...]]]></description>
			<content:encoded><![CDATA[<p>¿Cómo se puede <strong>desloguear un usuario sin tener que acceder a la ruta correspondiente del logout</strong>? Para algunas funcionalidades que queramos añadir a nuestro proyecto, es posible que necesitemos desloguear al usuario. Un ejemplo podría ser el caso de desactivar/borrar su cuenta, pero puede que haya más (todo depende de nuestra imaginación <img src='http://jonsegador.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  ). Vamos a ver como hacerlo con <strong>Symfony2</strong>.</p>

<div class="wp_syntax"><div 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;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;request&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">invalidate</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;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;security.context&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setToken</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Como extra, podemos informar al usuario mediante un mensaje flash y le redirigimos a la página que queramos:</p>

<div class="wp_syntax"><div 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;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;session&quot;</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;">'message.success'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> RedirectResponse<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">generateUrl</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'homepage'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Evidentemente, la página a la que vamos a redirigir tiene que poder ser vista por usuarios anónimos (no debe necesitar autentificación).</p>
<p>Pd.: Ya escribí como <a href="http://jonsegador.com/2011/07/instalar-symfony2-desde-cero/"><strong>Instalar Symfony2 desde cero</strong></a></p>
]]></content:encoded>
			<wfw:commentRss>http://jonsegador.com/2011/10/desloguear-a-un-usuario-mediante-programacion-en-symfony2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Error svn: Directory .svn containing working copy admin area is missing</title>
		<link>http://jonsegador.com/2011/09/error-svn-directory-svn-containing-working-copy-admin-area-is-missing/</link>
		<comments>http://jonsegador.com/2011/09/error-svn-directory-svn-containing-working-copy-admin-area-is-missing/#comments</comments>
		<pubDate>Fri, 02 Sep 2011 08:05:19 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[Servidores]]></category>
		<category><![CDATA[control de versiones]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[svn]]></category>

		<guid isPermaLink="false">http://jonsegador.com/?p=520</guid>
		<description><![CDATA[El motivo por el que nos puede aparecer este error (utilizando el sistema de control de versiones svn) al intentar hacer un commit, es porque hemos borrado (o ha desaparecido misteriosamente ) la carpeta .svn de alguno de los directorios del proyecto. svn: Directory .svn containing working copy admin area is missing Hay dos soluciones: [...]]]></description>
			<content:encoded><![CDATA[<p>El motivo por el que nos puede aparecer este error (utilizando el <strong>sistema de control de versiones svn</strong>) al intentar hacer un <strong>commit</strong>, es porque hemos borrado (o ha desaparecido misteriosamente <img src='http://jonsegador.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  ) la carpeta <strong>.svn</strong> de alguno de los directorios del proyecto.</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">svn: Directory .svn containing working <span style="color: #b1b100; font-weight: bold;">copy</span> admin area is missing</pre></div></div>

<p>Hay dos soluciones:</p>
<p><strong>1) Descargarnos de nuevo el proyecto completo (checkout).</strong></p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">svn co http://servidorsvn/proyecto/trunk proyecto</pre></div></div>

<p><strong>2)</strong> Si la carpeta .svn ha desaparecido solamente de uno de los directorios del proyecto, <strong>podemos bajarnos solo esa carpeta y mover el archivo .svn a nuestro proyecto</strong> (a la carpeta a la que falta el .svn).</p>
<p>En este ejemplo, se ha borrado la carpeta .svn del directorio css.</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">svn co http://servidorsvn/proyecto/trunk/css css_copia</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">mv css_copia/.svn /ruta_al_proyecto/proyecto/css/</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">rm -rf css_copia</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://jonsegador.com/2011/09/error-svn-directory-svn-containing-working-copy-admin-area-is-missing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Integrar Sphinx en Symfony2 con Doctrine2</title>
		<link>http://jonsegador.com/2011/07/integrar-sphinx-en-symfony2-con-doctrine2/</link>
		<comments>http://jonsegador.com/2011/07/integrar-sphinx-en-symfony2-con-doctrine2/#comments</comments>
		<pubDate>Fri, 22 Jul 2011 08:30:58 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[Symfony2]]></category>
		<category><![CDATA[autoload]]></category>
		<category><![CDATA[controlador]]></category>
		<category><![CDATA[doctrine2]]></category>
		<category><![CDATA[servicio]]></category>
		<category><![CDATA[sphinx]]></category>

		<guid isPermaLink="false">http://jonsegador.com/?p=513</guid>
		<description><![CDATA[En el grupo de Symfony2 hay una gran cantidad de preguntas entre las que, de vez en cuando, podemos encontrar un buen hilo donde se trata un tema interesante. Entre tanta &#8220;paja&#8221; (preguntas fáciles de responder solamente leyendo la documentación) es posible que pasemos por alto alguna de estas buenas preguntas junto a sus respuestas. [...]]]></description>
			<content:encoded><![CDATA[<p>En el <a href="http://groups.google.com/group/symfony2">grupo de Symfony2</a> hay una gran cantidad de preguntas entre las que, de vez en cuando, <strong>podemos encontrar un buen hilo donde se trata un tema interesante</strong>. Entre tanta &#8220;paja&#8221; (preguntas fáciles de responder solamente leyendo la documentación) es posible que pasemos por alto alguna de estas buenas preguntas junto a sus respuestas.</p>
<p>Voy a intentar recopilar este tipo de hilos y traducirlos al castellano, siempre dejando el enlace al hilo original del grupo de Symfony2 por si hay alguna nueva respuesta.</p>
<p>Para empezar, podemos encontrar como <strong>utilizar Sphinx haciendo uso de la api como un servicio en Symfony2</strong>.</p>
<p>1) <strong>Nos descargamos la api de Sphinx</strong> de la página oficial <a href="http://sphinxsearch.com/">http://sphinxsearch.com/</a>, colocándolo en la carpeta vendor/sphinx-client. Debemos renombrar la clase a SphinxClient.</p>
<p>2) <strong>Lo añadimos en el autoload.php</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000088;">$loader</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerPrefixes</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
   <span style="color: #666666; font-style: italic;">// ...</span>
   <span style="color: #0000ff;">'Sphinx'</span>           <span style="color: #339933;">=&gt;</span> __DIR__<span style="color: #339933;">.</span><span style="color: #0000ff;">'/../vendor/sphinx-client'</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>3) <strong>Añadimos Sphinx como servicio de Symfony2</strong> en el fichero config.yml</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"> services<span style="color: #339933;">:</span>
     search<span style="color: #339933;">:</span>
         <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">:</span> SphinxClient
         calls<span style="color: #339933;">:</span>
             <span style="color: #339933;">-</span> <span style="color: #009900;">&#91;</span>setArrayResult<span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span>
             <span style="color: #339933;">-</span> <span style="color: #009900;">&#91;</span>setLimits<span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span></pre></div></div>

<p>4) Por último, podemos <strong>hacer uso del servicio desde el controlador</strong> de esta forma:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$search</span>  <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'search'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$search</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$search_terms</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'myindex'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Evidentemente <strong>deberemos leer la documentación de Sphinx</strong> para instarlo y configurarlo correctamente. La página oficial es bastante completa en ese sentido.</p>
<p>Hilo original: <a href="http://groups.google.com/group/symfony2/browse_thread/thread/4487f6224b5b6d79/e287841d59787a73">doctrine2 and sphinx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jonsegador.com/2011/07/integrar-sphinx-en-symfony2-con-doctrine2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>8</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>0</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>10</slash:comments>
		</item>
	</channel>
</rss>

