<?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>Blog Symfony2 - Lexik Montpellier &#187; tips</title>
	<atom:link href="http://www.lexik.fr/blog/symfony/category/tips/feed" rel="self" type="application/rss+xml" />
	<link>http://www.lexik.fr/blog/symfony</link>
	<description>Blog sur le développement Web PHP avec Symfony 2</description>
	<lastBuildDate>Thu, 02 Feb 2012 14:00:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Installation de SuPHP sur Ubuntu</title>
		<link>http://www.lexik.fr/blog/symfony/symfony/installation-de-suphp-sur-ubuntu-1665</link>
		<comments>http://www.lexik.fr/blog/symfony/symfony/installation-de-suphp-sur-ubuntu-1665#comments</comments>
		<pubDate>Fri, 26 Aug 2011 08:00:40 +0000</pubDate>
		<dc:creator>laurent</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[Symfony2]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://www.lexik.fr/blog/symfony/?p=1665</guid>
		<description><![CDATA[Un problème récurrent pour beaucoup de développeurs utilisant Symfony est l&#8217;éternel conflit avec l&#8217;utilisateur &#171;&#160;www-data&#160;&#187; lors des accès, par exemple, aux fichiers du cache et aux logs. Sur Symfony 2 ce problème devient encore plus visible avec l&#8217;utilisation massive du &#8230;<p class="more aright"><a href="http://www.lexik.fr/blog/symfony/symfony/installation-de-suphp-sur-ubuntu-1665">...</a></p>]]></description>
			<content:encoded><![CDATA[<p>Un problème récurrent pour beaucoup de développeurs utilisant Symfony est l&#8217;éternel conflit avec l&#8217;utilisateur &laquo;&nbsp;www-data&nbsp;&raquo; lors des accès, par exemple, aux fichiers du cache et aux logs. Sur Symfony 2 ce problème devient encore plus visible avec l&#8217;utilisation massive du répertoire cache même en environnement &laquo;&nbsp;dev&nbsp;&raquo;.</p>
<p>Le cas de conflit le plus flagrant est certainement le <code>app/console cache:clear</code> qui renvoie une erreur car le cache a été généré par www-data lors du chargement du site par apache. Le bon gros <code>sudo rm -rf app/cache/*</code> n&#8217;est pas une solution acceptable, tout juste une rustine.<span id="more-1665"></span></p>
<p>Il existe différents moyens d&#8217;éviter ces conflits, que ce soit via scripts personnalisés, configurations avancées, ou packages additionnels. SuPHP est un module pour Apache très facile à mettre en place pour un environnement de &laquo;&nbsp;dev&nbsp;&raquo;, et il va répondre à notre problème d&#8217;une manière très simple: Apache ne va plus éxécuter les sites via &#8216;www-data&#8217;, mais via l&#8217;utilisateur propriétaire des fichiers éxécutés, c&#8217;est-à-dire le plus souvent le compte du développeur.</p>
<p>L&#8217;installation comprend deux packages dispos sur synaptic ou directement depuis la console:</p>
<pre>sudo apt-get install suphp-common libapache2-mod-suphp</pre>
<p>Pour une station de dev, une configuration fonctionnelle et simple est très bien décrite sur <a href="http://doc.ubuntu-fr.org/suphp#installation_de_suphp">http://doc.ubuntu-fr.org/suphp#installation_de_suphp</a>. Cette configuration empêche l&#8217;intervention de suphp dans les répertoires de /usr/share pour permettre entre autres à phpmyadmin (dans son emplacement par défaut) de continuer à fonctionner sans problèmes.</p>
<p>Il faudra éventuellement supprimer les fichiers de sessions en cours appartenent encore à www-data (dans /var/lib/php5 par défaut).</p>
<p>Si vos projets ne sont pas stockés dans les répertoires /var/www ou ~/public_html, il faut éditer la valeur de &laquo;&nbsp;docroot&nbsp;&raquo; dans /etc/suphp/suphp.conf pour y ajouter les répertoires supplémentaires. Par exemple, mes projets sont dans ~/www/ :</p>
<pre>docroot=/var/www:${HOME}/public_html</pre>
<p>devient alors:</p>
<pre>docroot=/var/www:${HOME}/public_html:${HOME}/www</pre>
<p>Pensez à nettoyer vos fichiers appartenant encore à www-data (cette fois avec des <code>sudo rm</code> par contre), comme les répertoires cache et logs, et redémarrez simplement Apache (<code>sudo service apache2 restart</code>). Après ça vous ne devriez plus voir de www-data dans vos projets, et vous pourrez oublier ce mauvais réflexe du <code>sudo rm -rf</code> à tout bout de champ !</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lexik.fr/blog/symfony/symfony/installation-de-suphp-sur-ubuntu-1665/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Présentation du plugin lxJavascriptPlugin</title>
		<link>http://www.lexik.fr/blog/symfony/symfony/presentation-du-plugin-lxjavascriptplugin-1408</link>
		<comments>http://www.lexik.fr/blog/symfony/symfony/presentation-du-plugin-lxjavascriptplugin-1408#comments</comments>
		<pubDate>Tue, 18 Jan 2011 09:53:28 +0000</pubDate>
		<dc:creator>laurent</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[Méthodologie]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[error]]></category>

		<guid isPermaLink="false">http://www.lexik.fr/blog/symfony/?p=1408</guid>
		<description><![CDATA[L&#8217;article d&#8217;hier sur les bonnes pratiques Javascript dans un projet Symfony faisait mention d&#8217;un plugin que nous utilisons en interne et qui est maintenant disponible sur Github: lxJavascriptPlugin. Ce court article va brièvement présenter son fonctionnement et son utilisation. L&#8217;installation &#8230;<p class="more aright"><a href="http://www.lexik.fr/blog/symfony/symfony/presentation-du-plugin-lxjavascriptplugin-1408">...</a></p>]]></description>
			<content:encoded><![CDATA[<p>L&#8217;article d&#8217;hier sur les bonnes pratiques Javascript dans un projet Symfony faisait mention d&#8217;un plugin que nous utilisons en interne et qui est maintenant disponible sur Github: lxJavascriptPlugin. Ce court article va brièvement présenter son fonctionnement et son utilisation.</p>
<p><span id="more-1408"></span></p>
<p>L&#8217;installation du plugin est très rapide:</p>
<ul>
<li>téléchargez <a href="https://github.com/lexik/lxJavascriptPlugin" target="_blank">les sources sur Github</a> dans le répertoire plugins de votre projet,</li>
<li>éditez le fichier config/settings.yml pour y inclure le helper lxJavascript:

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1408code9'); return false;">View Code</a> YML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p14089"><td class="code" id="p1408code9"><pre class="yml" style="font-family:monospace;">all:
  .settings:
    standard_helpers: [ ... , lxJavascript ]</pre></td></tr></table></div>

</li>
</ul>
<p>Et c&#8217;est tout! Voyons maintenant le fonctionnement.</p>
<p>Le premier but du plugin est de solutionner les problèmes de granularité du code Javascript dans un projet Symfony. Lorsque l&#8217;on utilise par exemple des widgets JQuery, des partials pour les réseaux sociaux, ou des composants pour des GoogleMap, on se retrouve vite avec du code Javascript éparpillé dans tout notre projet, et par conséquent tout autant éparpillé dans le code des pages générées.</p>
<p>Pour régler ce problème lxJavascriptPlugin propose un premier outil: le helper lxJavascriptHelper. Son utilisation est très similaire à celle du javascriptHelper de Symfony, mais avec la méthode lx_javascript:</p>
<ul>
<li>pour inclure un fichier source externe à la page:

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1408code10'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p140810"><td class="code" id="p1408code10"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> lx_javascript<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

</li>
<li>pour inclure directement du code javascript, par exemple le bout de code pour Google Analytics, le fonctionnement est similaire à celui d&#8217;un slot:

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1408code11'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p140811"><td class="code" id="p1408code11"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> lx_javascript<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
  <span style="color: #000000; font-weight: bold;">var</span> _gaq <span style="color: #339933;">=</span> _gaq <span style="color: #339933;">||</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  _gaq<span style="color: #339933;">.</span>push<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'_setAccount'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'*********'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  _gaq<span style="color: #339933;">.</span>push<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'_trackPageview'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> ga <span style="color: #339933;">=</span> document<span style="color: #339933;">.</span>createElement<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'script'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    ga<span style="color: #339933;">.</span>src <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'https:'</span> <span style="color: #339933;">==</span> document<span style="color: #339933;">.</span>location<span style="color: #339933;">.</span>protocol ? <span style="color: #0000ff;">'https://ssl'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'http://www'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">'.google-analytics.com/ga.js'</span><span style="color: #339933;">;</span>
    ga<span style="color: #339933;">.</span>setAttribute<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'async'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'true'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    document<span style="color: #339933;">.</span>documentElement<span style="color: #339933;">.</span>firstChild<span style="color: #339933;">.</span>appendChild<span style="color: #009900;">&#40;</span>ga<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> lx_end_javascript<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Ici la balise &lt;script&gt; n&#8217;est pas nécessaire, mais vous pouvez l&#8217;utiliser si vous le souhaitez (par exemple pour garder la coloration syntaxique de votre IDE)</li>
<li>pour insérer tout le code javascript déclaré via lx_javascript() à la fin de votre layout.php:

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1408code12'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p140812"><td class="code" id="p1408code12"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;en&quot; lang=&quot;en&quot;&gt;
  &lt;head&gt;
    ...
  &lt;/head&gt;
  &lt;body&gt;
    ...
    &lt;?php lx_include_javascripts() ?&gt;
  &lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

</li>
</ul>
<p>Le soucis de javascript dans les templates est adressé. Reste maintenant le problème du code javascript inséré via d&#8217;autres moyens, comme par exemple un widget qui utilise JQuery. Pour cela lxJavascriptPlugin propose une classe statique lxJavascriptStorage qui offre les fonctionnalités du helper depuis n&#8217;importe où dans votre code:</p>
<ul>
<li>pour ajouter une source externe:

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1408code13'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p140813"><td class="code" id="p1408code13"><pre class="php" style="font-family:monospace;">lxJavascriptStorage<span style="color: #339933;">::</span><span style="color: #004000;">addRemoteJavascript</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</li>
<li>pour ajouter du code javascript. Voyons par exemple la méthode render() du widget sfWidgetFormJQueryDate:

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1408code14'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p140814"><td class="code" id="p1408code14"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> render<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$attributes</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errors</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><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: #666666; font-style: italic;">//code</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOption</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'date_widget'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">render</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #339933;">,</span> <span style="color: #000088;">$attributes</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errors</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;">renderTag</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'input'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'hidden'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'size'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">generateId</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'_jquery_control'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'disabled'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'disabled'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>
           <a href="http://www.php.net/sprintf"><span style="color: #990000;">sprintf</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000cc; font-style: italic;">&lt;&lt;&lt;EOF
&lt;script type=&quot;text/javascript&quot;&gt;// &lt;![CDATA[
  [...]
// ]]&gt;&lt;/script&gt;
EOF</span>
    <span style="color: #339933;">,</span>
    <span style="color: #666666; font-style: italic;">//arguments du sprintf</span>
  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Nous voyons bien le javascript retourné dans le code du widget, et donc en milieu de page. Cette fonction render() pourrait, avec lxJavascriptPlugin, ressembler à ça:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1408code15'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p140815"><td class="code" id="p1408code15"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> render<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$attributes</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errors</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><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: #666666; font-style: italic;">//code &lt;/code&gt;</span>
&nbsp;
  <span style="color: #000088;">$js</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/sprintf"><span style="color: #990000;">sprintf</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000cc; font-style: italic;">&lt;&lt;&lt;EOF
&lt;script type=&quot;text/javascript&quot;&gt;// &lt;![CDATA[
  [...]
// ]]&gt;&lt;/script&gt;
EOF</span>
    <span style="color: #339933;">,</span>
    <span style="color: #666666; font-style: italic;">//arguments du sprintf</span>
  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  lxJavascriptStorage<span style="color: #339933;">::</span><span style="color: #004000;">addJavascriptCode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$js</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOption</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'date_widget'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">render</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #339933;">,</span> <span style="color: #000088;">$attributes</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errors</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;">renderTag</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'input'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'hidden'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'size'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">generateId</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'_jquery_control'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'disabled'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'disabled'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Peu de changements, mais ici le javascript sera rendu avec tout le reste du javascript en pied de page. </code></li>
</ul>
<p>Notez enfin que la méthode lx_include_javascripts() rendra en premier les sources externes, puis le code javascript concaténé dans une unique balise &lt;script&gt;. Si tous les exemples précédents sont utilisés dans une application, le javascript en pied de page donnera ceci:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1408code16'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p140816"><td class="code" id="p1408code16"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span><span style="color: #006600; font-style: italic;">// &lt;![CDATA[</span>
  <span style="color: #006600; font-style: italic;">//javascript du widget</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> _gaq <span style="color: #339933;">=</span> _gaq <span style="color: #339933;">||</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  _gaq.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'_setAccount'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'*********'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  _gaq.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'_trackPageview'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> ga <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'script'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    ga.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'https:'</span> <span style="color: #339933;">==</span> document.<span style="color: #660066;">location</span>.<span style="color: #660066;">protocol</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">'https://ssl'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'http://www'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'.google-analytics.com/ga.js'</span><span style="color: #339933;">;</span>
    ga.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'async'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'true'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    document.<span style="color: #660066;">documentElement</span>.<span style="color: #660066;">firstChild</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>ga<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">/* ]]&gt; */</span><span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

</li>
</ul>
<p>Vous pouvez obtenir <a href="https://github.com/lexik/lxJavascriptPlugin">lxJavascriptPlugin sur Github</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lexik.fr/blog/symfony/symfony/presentation-du-plugin-lxjavascriptplugin-1408/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Astuces de développement javascript avec symfony</title>
		<link>http://www.lexik.fr/blog/symfony/symfony/astuces-de-developpement-javascript-avec-symfony-1382</link>
		<comments>http://www.lexik.fr/blog/symfony/symfony/astuces-de-developpement-javascript-avec-symfony-1382#comments</comments>
		<pubDate>Mon, 17 Jan 2011 15:06:09 +0000</pubDate>
		<dc:creator>jeremy</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.lexik.fr/blog/symfony/?p=1382</guid>
		<description><![CDATA[Dans cet article nous allons voir quelques astuces et bonnes pratiques, non pas directement de développement symfony mais de développement javascript au sein d’un projet symfony. Si l&#8217;on reprend quelques bases de bonnes pratiques de développement javascript, on constate : &#8230;<p class="more aright"><a href="http://www.lexik.fr/blog/symfony/symfony/astuces-de-developpement-javascript-avec-symfony-1382">...</a></p>]]></description>
			<content:encoded><![CDATA[<p>Dans cet article nous allons voir quelques astuces et bonnes pratiques, non pas directement de développement symfony mais de développement javascript au sein d’un projet symfony.</p>
<p>Si l&#8217;on reprend quelques bases de bonnes pratiques de développement javascript, on constate :</p>
<ul>
<li>que vos javascripts doivent être non-intrusifs, autrement dit là pour améliorer l&#8217;expérience utilisateur et en aucun cas être indispensable au fonctionnement d&#8217;une page ;</li>
<li>que les javascript doivent être combinés en 1 seul fichier et minifié (ceci afin de limiter le nombre de requête HTTP et car le chargement de la page est arrêté à chaque balise script, notamment à cause d&#8217;un éventuel <code>document.write();</code> ;</li>
<li>que l&#8217;appel au javascript doit être en bas de page ;</li>
</ul>
<p><em>Cette liste est bien entendu non exhaustive, vous trouverez une <a href="http://developer.yahoo.com/performance/rules.html" target="_blank">liste plus détaillée par ici</a>.</em></p>
<p>Toutes ces bonnes pratiques de développement Javascript n’ont pas toujours été facilitées dans symfony, notamment à la grande époque des helpers link_to_remote() &#038; co. qui en ont ravi certains et fait cauchemarder d’autres. Et aujourd&#8217;hui encore bon nombre de widgets de formulaire retournent directement du code javascript (dépendant de jQuery ou autre) et qui ne fonctionneront évidemment plus dès lors que vos javascripts se trouveront en bas de page.</p>
<p><span id="more-1382"></span></p>
<h4>1. Où placer les javascripts dans le document</h4>
<p>Idéalement et selon les recommandations de Yslow, en bas de page, donc juste avant la balise fermante <code><body></code> de votre layout.php. Ceci afin de ne pas ralentir inutilement le début du chargement de votre page, puisque chaque balise script bloque le chargement de la page (notamment pour gérer le <code>document.write();</code>).<br />
Source : <a href="http://developer.yahoo.com/performance/rules.html#js_bottom" target="_blank">http://developer.yahoo.com/performance/rules.html#js_bottom</a></p>
<p>Si cette méthode ne vous gênera pas pour les librairies tierces telles que jQuery, qu&#8217;en est-il par contre de vos codes javascript ? Ils doivent également se trouver dans des fichiers séparés, dissociés de votre document HTML. Néanmoins comme je vois souvent le besoin pour les développeurs d&#8217;avoir le code javascript au sein du template symfony, voici une technique pour qu&#8217;il soit tout de même placé en fin de page : via un slot.</p>
<p>Un petit exemple de code :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1382code18'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p138218"><td class="code" id="p1382code18"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// indexSuccess.php :</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> slot<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'javascript'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
  console<span style="color: #339933;">.</span><a href="http://www.php.net/log"><span style="color: #990000;">log</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'javascript @ bottom'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> end_slot<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// layout.php :</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> include_javascripts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> include_slot<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'javascript'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Vous pourrez ainsi éditer votre code javascript depuis votre template PHP, bénéficier par exemple de valeurs dynamiques de PHP et pour autant que le code soit bien inclus en bas de page. Toutefois je vous recommande d&#8217;écrire du javascript là où il doit se trouver, à savoir dans un fichier JS&#8230;</p>
<p>De plus l&#8217;inconvénient majeur du slot est qu&#8217;il ne peut contenir qu&#8217;une valeur, il est écrasé à chaque utilisation. La technique fonctionne mais est très limitée. Pour ces besoins nous avons écrit chez Lexik un petit plugin &laquo;&nbsp;lxJavascript&nbsp;&raquo;, très similaire à l&#8217;utilisation d&#8217;un slot mais optimisé pour l&#8217;inclusion de javascripts. Il gère donc le multiple ajout, supprime les éventuelles multiples balises script pour en conserver qu&#8217;une seule, etc. Vous en saurez plus demain, le plugin sort en OpenSource sur le <a href="https://github.com/lexik" target="_blank">Github de Lexik</a>.</p>
<h4>2. Compression</h4>
<p>La compression des javascripts (c&#8217;est valable aussi pour les CSS) est une des tâches d&#8217;optimisations les plus importantes et également une des plus simples, du moins via les plugins symfony. Cette technique consiste à compresser tous vos fichiers javascript en 1 seul et surtout d&#8217;y appliquer un numéro de version (un timestamp le plus souvent) afin de prévenir des problèmes de cache dans vos mises à jour.</p>
<p>Les principaux plugins :</p>
<ul>
<li><a href="http://www.symfony-project.org/plugins/sfCombinePlugin" target="_blank">sfCombinePlugin</a>
<li><a href="http://www.symfony-project.org/plugins/npAssetsOptimizerPlugin" target="_blank">npAssetsOptimizerPlugin</a></li>
<li><a href="http://www.symfony-project.org/plugins/swCombinePlugin" target="_blank">swCombinePlugin</a></li>
</ul>
<p>Chez Lexik, nous utilisons <a href="http://www.symfony-project.org/plugins/npAssetsOptimizerPlugin" target="_blank">npAssetsOptimizerPlugin</a>, un plugin de <a href="http://www.akei.com/" target="_blank">Nicolas Perriault</a>, extrêmement simple à installer et paramétrer. Sur votre environnement de production, vous obtiendrez ainsi un fichier javascript compressé <code>/js/optimized.js?1294759429</code> qui contient un timestamp permettant à chaque modifications des fichiers d&#8217;être prises en compte sans problème de cache.</p>
<p>Nous n&#8217;appliquons pas la compression pour les librairies externes telles que jQuery que nous déléguons aux <a href="http://code.google.com/apis/libraries/devguide.html" target="_blank">serveurs CDN de Google</a>, cela apporte ces avantages notables :</p>
<ul>
<li>l&#8217;avantage du <abbr title="Content Delivery Network">CDN</abbr>, à savoir de proposer des données statiques via un réseau de multiples serveurs dans de multiples points géographiques, afin de permettre un accès très court ;</li>
<li>les navigateurs sont limités sur le nombre de connexions HTTP simultanées sur un même domaine, le téléchargement d&#8217;un javascript sur les serveurs de Google se fera en parallèle ;</li>
<li>les librairies du CDN ont les bonnes en-têtes de cache, par exemple la version 1.4.4 de jQuery restera en cache pendant 1 an sur le client, cela signifie également qu&#8217;un internaute peut bénéficier du cache téléchargé depuis un autre site, pas forcément sur le votre.</li>
</ul>
<h4>3. Accéder à certaines données dynamiques depuis javascript</h4>
<p>Il peut parfois être très utile depuis javascript d&#8217;accéder à certaines données dynamiques telles que la culture de l&#8217;utilisateur, du routing pour des appels Ajax ou encore de l&#8217;i18n. Il suffit de s&#8217;appuyer sur un simple fichier de configuration via un tableau de données JSON généré depuis une action symfony. J&#8217;ai écrit sur mon blog personnel un article détaillé sur le sujet <a href="http://blog.jeremybarthe.com/post/1465024153/configurations-javascript-dynamiques-en-symfony" target="_blank">Configurations Javascript dynamiques en Symfony</a>.</p>
<h4>4. Javascript depuis les widget</h4>
<p>Maintenant que vos inclusions de javascripts se trouvent en bas de page, jQuery par exemple, que faire de nos chers widgets jQuery Autocompleter <code>sfWidgetFormJQueryAutocompleter</code> et autre Datepicker <code>sfWidgetFormJQueryDate</code> qui retournent des champs de formulaire et le javascript d’initialisation du composant ? Eh bien, ne plus les utiliser&#8230; Simplement car ils seront inclus avant jQuery et vous aurez donc droit à l’erreur &laquo;&nbsp;jQuery is not defined&nbsp;&raquo;. Sans être aussi extrême, vous pouvez charger jQuery en haut de page (via les CDN de Google) et votre script compressé en bas de page, les widgets de formulaire seront alors à nouveau fonctionnels.</p>
<p>Cela fait déjà quelques temps que chez Lexik nous avons fait le choix d&#8217;enlever tous ces widgets pour les ré-écrire et les adapter à nos besoins. Les javascripts des widgets passent par notre plugin lxJavascript ce qui permet de les inclure là où on le souhaite. Egalement, le javascript retourné par ces widgets correspond à une utilisation basique du composant, dans un cas concrêt il est souvent nécessaire d&#8217;en ré-écrire une bonne part. Il peut être pratique par contre que le widget retourne seulement la sémantique HTML adaptée, par exemple dans le cas d&#8217;un Autocomplete un champ hidden pour stocker la valeur et un champ texte pour l&#8217;autocomplete, aussi nos widgets ont une option pour retourner ou non le code javascript.</p>
<h4>5. Optimisation frontend</h4>
<p>Presque aussi important que la compression, la configuration d&#8217;apache pour mettre en cache certains de vos fichiers statiques. Typiquement, maintenant que vos fichiers javascripts et CSS sont compressés en 1 seul et possèdent un timestamp unique par version, il serait dommage de les recharger à chaque chargement de page&#8230; Vous devez donc configurer le module Expires d&#8217;apache via le fichier de config du vhost ou via le .htaccess, en voici un exemple :</p>
<pre>
<IfModule mod_expires.c>
  ExpiresActive on
  ExpiresByType text/css "access plus 1 year"
  ExpiresByType text/javascript "access plus 1 year"
</IfModule>
</pre>
<p>Vos fichiers javascript et CSS seront ainsi en cache pendant 1 an, attention du coup à ne pas avoir de fichier sans numéro de version, car ils resteraient en cache et vos internautes ne bénéficieraient pas de vos mises à jour&#8230;</p>
<p>&nbsp;</p>
<p>Dernière chose, si ce n&#8217;est pas encore fait, installez et testez <a href="http://developer.yahoo.com/yslow/" target="_blank">Yslow</a> pour trouver des pistes d&#8217;optimisations de performance de vos sites.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lexik.fr/blog/symfony/symfony/astuces-de-developpement-javascript-avec-symfony-1382/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Exemple d&#8217;application utilisant la Graph API de Facebook</title>
		<link>http://www.lexik.fr/blog/symfony/symfony/exemple-dapplication-utilisant-la-graph-api-de-facebook-1187</link>
		<comments>http://www.lexik.fr/blog/symfony/symfony/exemple-dapplication-utilisant-la-graph-api-de-facebook-1187#comments</comments>
		<pubDate>Wed, 07 Jul 2010 12:42:30 +0000</pubDate>
		<dc:creator>laurent</dc:creator>
				<category><![CDATA[1.4.x]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[application]]></category>
		<category><![CDATA[developpeur]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.lexik.fr/blog/symfony/?p=1187</guid>
		<description><![CDATA[Facebook a lancé il y a maintenant quelque smois sa nouvelle Graph API, mais la documentation qui l'entoure se fait encore rare et peu détaillée. Je vais donc profiter de cet article pour présenter quelques aspects de cette nouvelle API à l'aide d'une petite iframe application Facebook, et avec Symfony (pour rester dans la thématique du blog !).<p class="more aright"><a href="http://www.lexik.fr/blog/symfony/symfony/exemple-dapplication-utilisant-la-graph-api-de-facebook-1187">...</a></p>]]></description>
			<content:encoded><![CDATA[<p>Facebook a lancé il y a maintenant quelques mois sa nouvelle Graph API, qui permet d&#8217;accèder et d&#8217;interagir avec les informations Facebook de l&#8217;utilisateur encore plus simplement. Seuelement la documentation qui l&#8217;entoure se fait encore rare et peu détaillée. Je vais donc profiter de cet article pour présenter quelques aspects de cette nouvelle API à l&#8217;aide d&#8217;une petite iframe application Facebook, et avec Symfony (pour rester dans la thématique du blog !).</p>
<p><span id="more-1187"></span></p>
<p>Pour commencer nous avons bien sûr besoin de créer notre application Facebook. Cela n&#8217;étant pas le but de cet article, je vous laisse trouver la marche à suivre sur le net; les sites couvrant le sujet sont légion (par exemple <a href="http://www.creer-une-application-facebook.com" target="_blank">créer une application Facebook</a>).</p>
<p>On paramètre correctement l&#8217;application et celle-ci s&#8217;occupera ensuite de charger nos pages dynamiques depuis l&#8217;url spécifiée:</p>
<p><a href="http://www.lexik.fr/blog/symfony/wp-content/uploads/2010/07/canvas.png"><img class="alignnone size-medium wp-image-1188" title="canvas" src="http://www.lexik.fr/blog/symfony/wp-content/uploads/2010/07/canvas-300x232.png" alt="" width="300" height="232" /></a></p>
<p>L&#8217;application Symfony n&#8217;a ici qu&#8217;un seul module &laquo;&nbsp;demo&nbsp;&raquo; qui s&#8217;occupe des quelques actions nécessaires. Editons &laquo;&nbsp;app.yml&nbsp;&raquo; pour y ajouter l&#8217;ID et le SECRET obtenus lors de la création de notre application:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1187code19'); return false;">View Code</a> YML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118719"><td class="code" id="p1187code19"><pre class="yml" style="font-family:monospace;">all:
  facebook:
    app_id: 116399618389033
    api_key: *******************************************
    secret: *****************************************
    base_url: http://facebook.prestataire-symfony.com
    app_url: http://apps.facebook.com/demolexik</pre></td></tr></table></div>

<p>Il nous faut ensuite récupérer <a href="http://github.com/facebook/php-sdk/downloads" target="_blank">l&#8217;api Facebook PHP sur Github</a>, que nous allons coller dans /lib/facebook/facebook.php.<br />
Afin de ne pas avoir à initialiser les appels à l&#8217;api depuis chaque action, un filter va grandement nous simplifier la tâche:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1187code20'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118720"><td class="code" id="p1187code20"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// lib/filter/FacebookFilter.class.php</span>
<span style="color: #000000; font-weight: bold;">class</span> FacebookFilter <span style="color: #000000; font-weight: bold;">extends</span> sfFilter
<span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> execute<span style="color: #009900;">&#40;</span>sfFilterChain <span style="color: #000088;">$filterChain</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$context</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$request</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$context</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">//initialisation de l'appel à l'api...</span>
    <span style="color: #000088;">$facebook</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Facebook<span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
          <span style="color: #0000ff;">'appId'</span>   <span style="color: #339933;">=&gt;</span> sfConfig<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'app_facebook_app_id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
          <span style="color: #0000ff;">'secret'</span>  <span style="color: #339933;">=&gt;</span> sfConfig<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'app_facebook_secret'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
          <span style="color: #0000ff;">'cookie'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</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;">$context</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getController</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">getActionStack</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLastEntry</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">getActionInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">facebook</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$facebook</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$filterChain</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</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>Pensez à éditer votre &laquo;&nbsp;filters.yml&nbsp;&raquo; en conséquence.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1187code21'); return false;">View Code</a> YML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118721"><td class="code" id="p1187code21"><pre class="yml" style="font-family:monospace;">#apps/frontend/config/filters.yml
rendering: ~
security:  ~
# insert your own filters here
Facebook:
  class: FacebookFilter
cache:     ~
execution: ~</pre></td></tr></table></div>

<p>Pour l&#8217;api Javascript, nous allons éditer le layout de l&#8217;application:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1187code22'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118722"><td class="code" id="p1187code22"><pre class="html" style="font-family:monospace;">&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xmlns:fb=&quot;http://www.facebook.com/2008/fbml&quot;&gt;
  &lt;head&gt;
    &lt;?php include_http_metas() ?&gt;
    &lt;?php include_metas() ?&gt;
    &lt;title&gt;Facebook App&lt;/title&gt;
    &lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
    &lt;style&gt;
      div.section {
        margin-bottom: 10px;
        background: #EEEEEE;
        padding: 5px 10px;
      }
      body {
        font-family: Arial;
        font-size: 12px;
      }
      pre {
        font-size: 10px;
        color: #333333;
        margin-left: 50px;
      }
    &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id=&quot;fb-root&quot;&gt;&lt;/div&gt;
    &lt;script src=&quot;http://connect.facebook.net/en_US/all.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/javascript&quot;&gt;
    FB.init({
       appId  : '&lt;?php echo sfConfig::get('app_facebook_app_id') ?&gt;',
       status : true, // vérifie le status de la connection
       cookie : true, // autorise les cookies pour permettre au serveur d'accéder à la session'
       xfbml  : true  // active le parsing de XFBML
      });
    &lt;/script&gt;
    ...</pre></td></tr></table></div>

<p>Nous en profitons pour charger JQuery afin de faciliter l&#8217;ajax et le remaniement du Dom.</p>
<p>Voilà, nous sommes prêts à attaquer les actions.</p>
<p>La première action va être l&#8217;authentification de l&#8217;utilisateur Facebook, et la gestion des permissions que celui-ci devra accorder à notre application.<br />
Le nouveau modèle de Facebook veut que nous demandions à l&#8217;utilisateur, dès le premier accès à l&#8217;application, qu&#8217;il accepte simultanément toutes les permissions nécessaires. Pour plus d&#8217;informations à ce sujet, consultez la liste des permissions sur <a href="http://developers.facebook.com/docs/authentication/permissions" target="_blank">le wiki Facebook Developer</a>.<br />
Voici l&#8217;action Login, qui sera appelée lorsque l&#8217;utilisateur n&#8217;a pas accordé ces permissions, ou lors d&#8217;une première visite de l&#8217;application:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1187code23'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118723"><td class="code" id="p1187code23"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//apps/frontend/modules/demo/actions/actions.class.php</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: #666666; font-style: italic;">//on génère l'url qui permettra le login à l'application</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loginUrl</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLoginUrl</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'canvas'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'fbconnect'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span>
            <span style="color: #666666; font-style: italic;">//ici on demande les permissions email, publication sur le mur, et changement du status</span>
            <span style="color: #0000ff;">'req_perms'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'email,publish_stream,status_update'</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'next'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span>
        <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Comme nous sommes dans une application iframe, il faut charger l&#8217;url de login depuis la frame parente, et notre template va s&#8217;en charger:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1187code24'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118724"><td class="code" id="p1187code24"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;!--</span>loginSuccess<span style="color: #339933;">.</span>php<span style="color: #339933;">--&gt;</span>
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
top<span style="color: #339933;">.</span>location<span style="color: #339933;">.</span>href <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;?php echo <span style="color: #006699; font-weight: bold;">$sf_data-&gt;getRaw</span>('loginUrl') ?&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></pre></td></tr></table></div>

<p>Il faut maintenant que notre index redirige vers ce login dans les cas précédemment cités:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1187code25'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118725"><td class="code" id="p1187code25"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//apps/frontend/modules/demo/actions/actions.class.php</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeIndex<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: #666666; font-style: italic;">//on vérifie si l'utilisateur a une session facebook active</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">session</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">facebook</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;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">session</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//sinon il doit s'identifier</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;">'demo/login'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//s'il a une session active, on vérifie si elle est valide pour notre application</span>
    try
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">uid</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">facebook</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;">;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fbme</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">api</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/me'</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>FacebookApiException <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//sinon il doit à nouveau s'identifier</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;">'demo/login'</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>On peut maintenant voir une demande de permissions s&#8217;afficher lorsque l&#8217;on se connecte à l&#8217;application:<br />
<a href="http://www.lexik.fr/blog/symfony/wp-content/uploads/2010/07/perms.png"><img class="alignnone size-medium wp-image-1189" title="perms" src="http://www.lexik.fr/blog/symfony/wp-content/uploads/2010/07/perms-300x172.png" alt="" width="300" height="172" /></a></p>
<p>En vérifiant la validité de la session, nous avons récupéré toutes les informations sur l&#8217;utilisateur auxquelles nos permissions nous donnent accès. Voyons l&#8217;étendue de ces informations en les affichant dans notre vue:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1187code26'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118726"><td class="code" id="p1187code26"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;!--</span> indexSuccess<span style="color: #339933;">.</span>php <span style="color: #339933;">--&gt;</span>
<span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;section&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>h3<span style="color: #339933;">&gt;</span>Informations utilisateur<span style="color: #339933;">&lt;/</span>h3<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>pre<span style="color: #339933;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?</span>php <a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fbme</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> <span style="color: #339933;">/</span>pre<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p><a href="http://www.lexik.fr/blog/symfony/wp-content/uploads/2010/07/infos.png"><img class="alignnone size-medium wp-image-1195" title="infos" src="http://www.lexik.fr/blog/symfony/wp-content/uploads/2010/07/infos-300x174.png" alt="" width="300" height="174" /></a><br />
Nous accédons bien à l&#8217;adresse email comme spécifié, ainsi qu&#8217;aux informations génériques et publiques sur l&#8217;utilisateur.</p>
<p>Il est maintenant temps d&#8217;interagir avec l&#8217;utilisateur. Il nous a également accordé l&#8217;accès à son status, nous allons donc lui proposer de l&#8217;éditer via notre application:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1187code27'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118727"><td class="code" id="p1187code27"><pre class="html" style="font-family:monospace;">&lt;!-- indexSuccess.php --&gt;
...
&lt;div class=&quot;section&quot;&gt;
  &lt;a onclick=&quot;updateStatus();return false;&quot; href=&quot;#&quot;&gt;Changer mon status via Ajax et l'API PHP&lt;/a&gt;
  &lt;textarea id=&quot;status_text&quot; cols=&quot;40&quot; rows=&quot;2&quot;&gt;  &lt;/textarea&gt;
&lt;/div&gt;
...
&lt;script type=&quot;text/javascript&quot;&gt;
function updateStatus()
  {
    var status = document.getElementById('status_text').value;
&nbsp;
    $.ajax({
      type: 'post',
      url: '&lt;?php echo url_for('demo/statusUpdate') ?&gt;',
      data: 'status=' + status,
      success: function(response) {
        alert(response);
      },
      error: function(response) {
        alert(response);
      }
    });
  }
&lt;/script&gt;</pre></td></tr></table></div>

<p>La méthode js &laquo;&nbsp;updateStatus()&nbsp;&raquo; va faire un appel ajax à une nouvelle action updateStatus de notre application:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1187code28'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118728"><td class="code" id="p1187code28"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//apps/frontend/modules/demo/actions/actions.class.php</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeUpdateStatus<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;">forward404Unless</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isXmlHttpRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$status</span> <span style="color: #339933;">=</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;">'status'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    try
    <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//un simple appel a l'api suffit à poster un nouveau status sur le profile de l'utilisateur</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">api</span><span style="color: #009900;">&#40;</span>
          <span style="color: #0000ff;">'/me/feed'</span><span style="color: #339933;">,</span>
          <span style="color: #0000ff;">'post'</span><span style="color: #339933;">,</span>
          <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'message'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$status</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cb'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span> <span style="color: #009900;">&#41;</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>FacebookApiException <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">renderText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Erreur: '</span><span style="color: #339933;">.</span><span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">renderText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Status mis-à-jour'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>L&#8217;api Javascript est toute aussi simple à utiliser. Proposons cette fois à l&#8217;utilisateur de parler de notre application sur son mur à l&#8217;aide de javascript:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1187code29'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118729"><td class="code" id="p1187code29"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;!--</span> indexSuccess<span style="color: #339933;">.</span>php <span style="color: #339933;">--&gt;</span>
<span style="color: #339933;">...</span>
<span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;section&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>a onclick<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;publishWall();return false;&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#&quot;</span><span style="color: #339933;">&gt;</span>Poster un article sur mon mur via l<span style="color: #0000ff;">'API Javascript&lt;/a&gt;
&lt;/div&gt;
...
&lt;script type=&quot;text/javascript&quot;&gt;
function publishWall()
  {
    FB.ui(
    {
      method: '</span>stream<span style="color: #339933;">.</span>publish<span style="color: #0000ff;">',
      message: '</span><span style="color: #0000ff;">',
      attachment:
      {
        name: &quot;Démo Symfony+Facebook&quot;,
        caption: '</span><span style="color: #0000ff;">',
        description: &quot;Une démo simple d'</span>application Facebook avec Symfony et les dernières API Javascript et PHP de Facebook<span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;,
        href: &quot;</span>http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//www.lexik.fr/blog/symfony/non-classe/exemple-dapplication-utilisant-la-graph-api-de-facebook-1187&quot;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
      user_prompt_message<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Application de démo Symfony+Facebook&quot;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></pre></td></tr></table></div>

<p>Le résultat obtenu en cliquant sur le lien est le pseudo-popup bien connu de Facebook:<br />
<a href="http://www.lexik.fr/blog/symfony/wp-content/uploads/2010/07/publish.png"><img class="alignnone size-medium wp-image-1190" title="publish" src="http://www.lexik.fr/blog/symfony/wp-content/uploads/2010/07/publish-300x129.png" alt="" width="300" height="129" /></a></p>
<p>Nous venons de voir quelques méthodes simples d&#8217;accès ou de publication de données. Une liste plus complète des méthodes éxistantes est sur <a href="http://developers.facebook.com/docs/reference/rest/" target="_blank">le wiki Facebook Developer</a>.<br />
Dans le cadre d&#8217;applications plus complexes, il sera souvent utile de pouvoir formuler précisément les informations que l&#8217;on souhaite récupérer, et celles-ci ne correspondront pas nécessairement à une méthode éxistante. Pour cela Facebook met à notre disposition FQL, un langage de requète similaire à SQL dans sa syntaxe, pour générer nos requètes spécifiques.</p>
<p>Par exemple, récupérons la liste des amis de l&#8217;utilisateur avec leur nom, leur uid, et leur photo. Voici la requète FQL pour cela, et son éxécution via l&#8217;api PHP:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1187code30'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118730"><td class="code" id="p1187code30"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//apps/frontend/modules/demo/actions/actions.class.php</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeIndex<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: #339933;">...</span>
    <span style="color: #666666; font-style: italic;">//récupération des amis via FQL</span>
    <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT name, uid, pic_square FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">uid</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">amis</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">api</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'method'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'fql.query'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'query'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$query</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'callback'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span>
    <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Et l&#8217;affichage de la liste dans la vue:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1187code31'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p118731"><td class="code" id="p1187code31"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;!--</span> indexSuccess<span style="color: #339933;">.</span>php <span style="color: #339933;">--&gt;</span>
<span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;section&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>h3<span style="color: #339933;">&gt;</span>Liste d<span style="color: #0000ff;">'amis&lt;/h3&gt;
&lt;?php foreach ($amis as $ami): ?&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;img src=&quot;&lt;?php echo $ami['</span>pic_square<span style="color: #0000ff;">'] ?&gt;&quot; alt=&quot;&quot; /&gt;&lt;/li&gt;
	&lt;li&gt;&lt;strong&gt;&lt;?php echo $ami ['</span>name<span style="color: #0000ff;">'] ?&gt;&lt;/strong&gt;&lt;/li&gt;
	&lt;li&gt;uid: &lt;?php echo $ami['</span>uid<span style="color: #0000ff;">'] ?&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;?php endforeach; ?&gt;
&lt;/div&gt;</span></pre></td></tr></table></div>

<p>Cet article n&#8217;était pas dédié à Symfony, mais l&#8217;utilisait plutôt pour proposer un environnement familier à notre application. Ce survol rapide de la nouvelle Graph Api permet surtout de donner un point de départ sur l&#8217;utilisation de la nouvelle librairie à un moment ou la documentation à son sujet n&#8217;est pas vraiment fournie, en particulier en français !<br />
Vous pouvez voir l&#8217;application tourner sur <a href="http://apps.facebook.com/demoLexik" target="_blank">http://apps.facebook.com/demoLexik</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lexik.fr/blog/symfony/symfony/exemple-dapplication-utilisant-la-graph-api-de-facebook-1187/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Validation d&#8217;un numéro siret : sfValidatorSiret</title>
		<link>http://www.lexik.fr/blog/symfony/symfony/1-4-x/validation-dun-numero-siret-sfvalidatorsiret-1159</link>
		<comments>http://www.lexik.fr/blog/symfony/symfony/1-4-x/validation-dun-numero-siret-sfvalidatorsiret-1159#comments</comments>
		<pubDate>Tue, 25 May 2010 06:30:00 +0000</pubDate>
		<dc:creator>thomas</dc:creator>
				<category><![CDATA[1.4.x]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[siret]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[validator]]></category>

		<guid isPermaLink="false">http://www.lexik.fr/blog/symfony/?p=1159</guid>
		<description><![CDATA[Bonjour, comme vous avez pu le voir avec les récents posts, nous avons eu pas mal de gestion de facturation: conversion de devises, validation d&#8217;un numéro de tva intracommunautaire. Pour continuer sur la lancée, nous allons faire un petit validator &#8230;<p class="more aright"><a href="http://www.lexik.fr/blog/symfony/symfony/1-4-x/validation-dun-numero-siret-sfvalidatorsiret-1159">...</a></p>]]></description>
			<content:encoded><![CDATA[<p>Bonjour,</p>
<p>comme vous avez pu le voir avec les récents posts, nous avons eu pas mal de gestion de facturation: <a title="conversion de devises" href="http://www.lexik.fr/blog/symfony/symfony/tips-conversions-de-devises-1137" target="_blank">conversion de devises</a>, <a title="tva intracommunautaire" href="http://www.lexik.fr/blog/symfony/symfony/un-validator-tva-bien-pratique-1123" target="_blank">validation d&#8217;un numéro de tva intracommunautaire</a>.</p>
<p>Pour continuer sur la lancée, nous allons faire un petit validator sur les numéros siret.</p>
<p><span id="more-1159"></span>Un numéro siret correspond à un petit algorithme assez simple <a title="algorithme numéro siret" href="http://fr.wikipedia.org/wiki/Syst%C3%A8me_d%E2%80%99identification_du_r%C3%A9pertoire_des_%C3%A9tablissements#Calcul_et_validit.C3.A9_d.27un_num.C3.A9ro_SIRET" target="_blank">expliqué ici</a> (<a title="Luhn" href="http://fr.wikipedia.org/wiki/Luhn" target="_blank">algorithme de Luhn</a>)<br />
Le principe est le suivant : on multiplie les chiffres de rang impair à partir de la droite par 1, ceux de rang pair par 2 ; la somme des chiffres obtenus doit être congrue au modulo 10, c&#8217;est-à-dire qu&#8217;elle doit être multiple de 10.<br />
Un numéro siret comporte 14 chiffre.</p>
<p>Voici le code du validator: sfValidatorSiret.class.php</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1159code33'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p115933"><td class="code" id="p1159code33"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> sfValidatorSiret <span style="color: #000000; font-weight: bold;">extends</span> sfValidatorBase 
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> doClean<span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$siret</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$siret</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <a href="http://www.php.net/strlen"><span style="color: #990000;">strlen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$siret</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">14</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> sfValidatorError<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Le numéro siret est invalide'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000088;">$sum</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">14</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</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: #000088;">$i</span><span style="color: #339933;">%</span><span style="color:#800080;">2</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$tmp</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$siret</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$tmp</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tmp</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">9</span> ? <span style="color: #000088;">$tmp</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">9</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$tmp</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: #000088;">$tmp</span><span style="color: #339933;">=</span> <span style="color: #000088;">$siret</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000088;">$sum</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$tmp</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sum</span><span style="color: #339933;">%</span><span style="color:#800080;">10</span> <span style="color: #339933;">!==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> sfValidatorError<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Le numéro siret est invalide'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$siret</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Pour expliquer rapidement:<br />
1. si la valeur est vide ou de longueur différente de 14 on retourne une exception.<br />
2. on parcours les 14 chiffres, si le chiffre est paire alors on le multiplie par 2 et si ce résultat est supérieur à  9 alors on retranche 9.<br />
ex: 7 =&gt; 7&#215;2 = 14 =&gt; 14 &#8211; 9 = 5.<br />
faire 14 -9  reviens à additionner les 2 chiffres: 1 + 4 = 5.<br />
3. On additionne tous les chiffres paires x2 et les chiffres impaires.<br />
4. si le résultat est divisible par 10 alors c&#8217;est un numéro siret.</p>
<p>Attention ceci indique que le numéro fourni peut être un numéro siret, MAIS PAS que ce numéro est réellement un numéro siret inscrit à L&#8217;INSEE, actif et correspond à une société.</p>
<p><a href="http://www.lexik.fr/blog/symfony/wp-content/uploads/2010/05/sfValidatorSiret.tar.gz">Vous pouvez télécharger le validator ici.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.lexik.fr/blog/symfony/symfony/1-4-x/validation-dun-numero-siret-sfvalidatorsiret-1159/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

