<?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>Symfony2 | Nerdpress.org</title>
	<atom:link href="https://nerdpress.org/tag/symfony2/feed/" rel="self" type="application/rss+xml" />
	<link>https://nerdpress.org</link>
	<description>...dev, tech problems and solutions.</description>
	<lastBuildDate>Wed, 24 Aug 2016 15:08:59 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Symfony and named ParamConverters</title>
		<link>https://nerdpress.org/2014/12/15/symfony-named-paramconverters/</link>
		
		<dc:creator><![CDATA[Ivo Bathke]]></dc:creator>
		<pubDate>Mon, 15 Dec 2014 13:07:36 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[ParamConverter]]></category>
		<category><![CDATA[Symfony2]]></category>
		<guid isPermaLink="false">https://nerdpress.org/?p=2591</guid>

					<description><![CDATA[<p>Symfony&#8217;s ParamConverter is a common way to transform some GET param to an entity before your controllers action. This happens most of the time via type hinting and priority detection kinda magic in the background. But as magic is often obscure sometimes you need a bit of explicitness. F.e. when you have more and different &#8230; </p>
<p class="link-more"><a href="https://nerdpress.org/2014/12/15/symfony-named-paramconverters/" class="more-link">Continue reading<span class="screen-reader-text"> "Symfony and named ParamConverters"</span></a></p>
The post <a href="https://nerdpress.org/2014/12/15/symfony-named-paramconverters/">Symfony and named ParamConverters</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></description>
										<content:encoded><![CDATA[<p>Symfony&#8217;s ParamConverter is a common way to transform some GET param to an entity before your controllers action.<br />
This happens most of the time via type hinting and priority detection kinda magic in the background.<br />
But as magic is often obscure sometimes you need a bit of explicitness.</p>
<p>F.e. when you have more and different ParamConverter per entity you want to name them explicitly.<br />
Then you can use named ParamConverters.</p>
<p>In the documentation this issue is a bit fragmented, so here is the compact version:<br />
<span id="more-2591"></span></p>
<p><strong>Declare:</strong><br />
Set the &#8220;converter&#8221; attribute in the param_converter service tag.<br />
Read it in the documentation: <a href="http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html#creating-a-converter" target="_blank">http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html#creating-a-converter</a></p>
<p><em>You can register a converter by priority, by name (attribute &#8220;converter&#8221;) or both.</em></p>
<pre class="brush: xml; title: ; notranslate">
&lt;service id=&quot;my_converter&quot; class=&quot;MyBundle\Request\ParamConverter\MyConverter&quot;&gt;
    &lt;tag name=&quot;request.param_converter&quot; priority=&quot;-2&quot; converter=&quot;my_converter&quot; /&gt;
&lt;/service&gt;
</pre>
<p><strong>Use:</strong><br />
In you controller annotation call the ParamConverter with the converter attribute.<br />
The Symfony <a href="http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html" target="_blank">ParamConverter dokumentation</a> explains it like this:<br />
<em>If an explicit converter choice was made with @ParamConverter(converter=&#8221;name&#8221;) the converter with the given name is chosen.</em></p>
<pre class="brush: php; title: ; notranslate">use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;

/**
 * @ParamConverter(&quot;post&quot;, converter=&quot;my_converter&quot;)
 */
public function showAction(Post $post)
{
}</pre>The post <a href="https://nerdpress.org/2014/12/15/symfony-named-paramconverters/">Symfony and named ParamConverters</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Caching Data in Symfony2</title>
		<link>https://nerdpress.org/2012/07/10/caching-data-in-symfony2/</link>
					<comments>https://nerdpress.org/2012/07/10/caching-data-in-symfony2/#comments</comments>
		
		<dc:creator><![CDATA[Ivo Bathke]]></dc:creator>
		<pubDate>Tue, 10 Jul 2012 10:19:01 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[Symfony2]]></category>
		<guid isPermaLink="false">https://nerdpress.org/?p=2230</guid>

					<description><![CDATA[<p>Symfony2 has a great Caching Layer based on its HTTP Cache. But this aims mainly on caching the views. In some apps however you need to cache data behind the scenes, f.e. responses from API calls or custom objects sets. Symfony2 itself doesnt have such a functionality on first sight (symfony2 doesnt, but Doctrine, see &#8230; </p>
<p class="link-more"><a href="https://nerdpress.org/2012/07/10/caching-data-in-symfony2/" class="more-link">Continue reading<span class="screen-reader-text"> "Caching Data in Symfony2"</span></a></p>
The post <a href="https://nerdpress.org/2012/07/10/caching-data-in-symfony2/">Caching Data in Symfony2</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></description>
										<content:encoded><![CDATA[<p>Symfony2 has a great Caching Layer based on its <a href="http://symfony.com/doc/current/book/http_cache.html">HTTP Cache</a>. But this aims mainly on caching the views.</p>
<p>In some apps however you need to cache data behind the scenes, f.e. responses from API calls or custom objects sets.<br />
Symfony2 itself doesnt have such a functionality on first sight (symfony2 doesnt, but Doctrine, see below) and so I searched for one and first found a bundle which utilize the Zend Cache lib:<br />
<a href="https://github.com/KnpLabs/KnpZendCacheBundle">https://github.com/KnpLabs/KnpZendCacheBundle</a></p>
<p>This worked well but as discussed here(<a href="https://github.com/KnpLabs/KnpZendCacheBundle/issues/2">https://github.com/KnpLabs/KnpZendCacheBundle/issues/2</a>) this adds dependencies to your Symfony2 project.  This is actually not necessary since <a href="https://github.com/doctrine/common">Doctrine/Commons</a> is almost always part of your Symfony2 distribution and the Doctrine/Commons provides a Cache Layer as well.<br />
A very good one, indeed.</p>
<p>So if you need to cache data use <strong>Doctrine/Commons</strong>.<br />
<span id="more-2230"></span><br />
This Cache Layer abstracts the caching functionality and provides already various different backends for your caching data.<br />
These are already build-in in the master version:</p>
<ul>
<li>APC</li>
<li>Array</li>
<li>Filesystem</li>
<li>Memcache</li>
<li>PhpFile</li>
<li>Redis</li>
<li>WinCache</li>
<li>Xcache</li>
<li>ZendData</li>
</ul>
<p>You could even create your own on top of the <a href="https://github.com/doctrine/common/blob/master/lib/Doctrine/Common/Cache/CacheProvider.php">CacheProvider</a> class and the <a href="https://github.com/doctrine/common/blob/master/lib/Doctrine/Common/Cache/Cache.php">Cache</a> interface.</p>
<p>In your Symfony2 project simply register your cache service of choice and your ready to go.<br />
In your <em>config.yml</em> or <em>services.yml</em> add:<br />
<script src="https://gist.github.com/3082497.js?file=services.yml"></script><noscript><pre><code class="language-yaml yaml">cache:
        class: Doctrine\Common\Cache\PhpFileCache
        arguments: [%kernel.cache_dir%]</code></pre></noscript></p>
<p>And in your controller you can call the service and save and load data from the cache.</p>
<script src="https://gist.github.com/3082497.js?file=IndexController.php"></script><noscript><pre><code class="language-php php">$cache = $this-&gt;get(&#039;cache&#039;);
$cache-&gt;setNamespace(&#039;mynamespace.cache&#039;); 
if (false === ($cached_data = $cache-&gt;fetch($cache_key))) {
            $cached_data = $SOMEAPI-&gt;getData($params);
            $cache-&gt;save($cache_key, $cached_data, 3600);//TTL 1h
}</code></pre></noscript>
<p>As you can see you can set a namespace for your cache data, so that you can easy use it for different scenarions in the same app.<br />
Further you can set a time-to-live (TTL) in seconds as third parameter of the save method.</p>
<p>So after all symfony2 has a caching mechanism for data, its just a little hidden in the Doctrine/Commons dependency.</p>
<p><strong>Update from the Update!!</strong><br />
LiipDoctrineCacheBundle is abanndoned, use <a href="https://github.com/doctrine/DoctrineCacheBundle" target="_blank">https://github.com/doctrine/DoctrineCacheBundle</a><br />
<strong>Update!!</strong><br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>I just was told that there is a Bundle for the Doctrine/Commons cache:<br />
<a href="https://github.com/liip/LiipDoctrineCacheBundle">https://github.com/liip/LiipDoctrineCacheBundle</a></p>
<p>So I recommend to use this.<br />
The benefit of this is, you can configure your caches through yml files and you have one central cache reference. I guess this saves some bytes in case you use different caches with different namepaces.</p>
<p>So with the LiipDoctrineCacheBundle the above example would go like this (given you have already registered the bundle):</p>
<p>config.yml<br />
<script src="https://gist.github.com/3205828.js?file=config.yml"></script><noscript><pre><code class="language-yaml yaml">liip_doctrine_cache:
    namespaces:
        mynamespace:
            namespace: mynamespace.cache
            type: php_file</code></pre></noscript></p>
<p>IndexController.php<br />
<script src="https://gist.github.com/3205828.js?file=IndexController.php"></script><noscript><pre><code class="language-php php">$cache = $container-&gt;get(&#039;liip_doctrine_cache.ns.[mynamespace]&#039;);
if (false === ($cached_data = $cache-&gt;fetch($cache_key))) {
            $cached_data = $SOMEAPI-&gt;getData($params);
            $cache-&gt;save($cache_key, $cached_data, 3600);//TTL 1h
}</code></pre></noscript></p>The post <a href="https://nerdpress.org/2012/07/10/caching-data-in-symfony2/">Caching Data in Symfony2</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></content:encoded>
					
					<wfw:commentRss>https://nerdpress.org/2012/07/10/caching-data-in-symfony2/feed/</wfw:commentRss>
			<slash:comments>7</slash:comments>
		
		
			</item>
		<item>
		<title>Symfony2 Starter Tweaks</title>
		<link>https://nerdpress.org/2012/05/23/symfony2-starter-tweaks/</link>
					<comments>https://nerdpress.org/2012/05/23/symfony2-starter-tweaks/#comments</comments>
		
		<dc:creator><![CDATA[Ivo Bathke]]></dc:creator>
		<pubDate>Wed, 23 May 2012 14:32:10 +0000</pubDate>
				<category><![CDATA[Project Setup]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Symfony2]]></category>
		<guid isPermaLink="false">https://nerdpress.org/?p=2186</guid>

					<description><![CDATA[<p>When you start with symfony you probably use the Symfony Standard Edition. This is a quite good start but there are somethings that helped me and might help you aswell. Since every beginning is &#8220;schwer&#8221; :) So here there are. 1. replace web/app*.dev with the following index.php its much nicer to use the DirectoryIndex in &#8230; </p>
<p class="link-more"><a href="https://nerdpress.org/2012/05/23/symfony2-starter-tweaks/" class="more-link">Continue reading<span class="screen-reader-text"> "Symfony2 Starter Tweaks"</span></a></p>
The post <a href="https://nerdpress.org/2012/05/23/symfony2-starter-tweaks/">Symfony2 Starter Tweaks</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></description>
										<content:encoded><![CDATA[<p>When you start with symfony you probably use the <a href="https://github.com/symfony/symfony-standard">Symfony Standard Edition</a>.<br />
This is a quite good start but there are somethings that helped me and might help you aswell.<br />
Since every beginning is &#8220;schwer&#8221; :)<br />
So here there are.<br />
<span id="more-2186"></span><br />
<strong>1. replace web/app*.dev with the following index.php</strong><br />
<script src="https://gist.github.com/2775189.js"></script><noscript><pre><code class="language-php php">&lt;?php
/**
* Tweaked Symfony2 bootstrap file
*/

// if you don&#039;t want to setup permissions the proper way, just uncomment the following PHP line
// read http://symfony.com/doc/current/book/installation.html#configuration-and-setup for more information
umask(0000);

if ( file_exists( dirname(__FILE__).&#039;/../.env.php&#039; ) ){
    $env = require_once( dirname(__FILE__).&#039;/../.env.php&#039; );
}
$env = isset( $env )? $env : &#039;prod&#039;;
$debug = true;
if($env == &#039;prod&#039;){
	$debug = false;
}

require_once __DIR__.&#039;/../app/bootstrap.php.cache&#039;;
require_once __DIR__.&#039;/../app/AppKernel.php&#039;;

use Symfony\Component\HttpFoundation\Request;

$kernel = new AppKernel($env, $debug);
$kernel-&gt;loadClassCache();
if( $env != &#039;dev&#039; ){
//require_once __DIR__.&#039;/../app/AppCache.php&#039;;
//$kernel = new AppCache($kernel);
}
$request = Request::createFromGlobals();
$response = $kernel-&gt;handle($request);
$response-&gt;send();
$kernel-&gt;terminate($request, $response);
</code></pre></noscript></p>
<p>its much nicer to use the DirectoryIndex in your URL and not having the web_app.php, thats the aesthetics.<br />
But it also avoids having some hassle with ajax urls which are defined absolute in some scripts like<br />
url: /myajax/call.json</p>
<p>The Environment can be changed in the .env.php file which just returns the Environment value:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
return $env = 'dev';
</pre>
<p>Personally i dont mind changing the Env in the code rather than in the url, while in development.<br />
It might be a tick more far away but its rather uncommon anyway and it would be more effort to change the vhost DirectoryIndex on web_dev.php and prod everytime to get a clean url.<br />
<em>(thanks max &#038; joshi)</em></p>
<p><strong>2. resolve console and webserver user conflict for cache</strong><br />
Different to Symfony 1.x Symfony2 assumes a more sophisticated User Managment on your machine for Security reasons, which absolutly makes sense. Read more about it here:<a href="http://symfony.com/doc/current/book/installation.html#configuration-and-setup">http://symfony.com/doc/current/book/installation.html#configuration-and-setup</a><br />
This means you have to adjust the rights for the webserver- and for the console user that both can create and delete the cache.<br />
This requires some admin skills which might be a bit too much for starters.<br />
And some people start wondering why the </p>
<pre class="brush: bash; title: ; notranslate">php app/console cache:clear</pre>
<p>command wont work and start using </p>
<pre class="brush: bash; title: ; notranslate">rm -rf app/cache/*</pre>
<p>in desperation. </p>
<p>To fix this Symfony2 recommends as a &#8216;hotfix&#8217; to uncomment the </p>
<pre class="brush: php; title: ; notranslate">umask(0000);</pre>
<p>part on top of your bootstrap files.<br />
So simply uncomment the line in app_dev.php ( <em>if you use the one from above its already done :)</em> ) and in <em>app/console</em>.<br />
Thats alright for local setup, for production go and talk to your admin of trust or live the life of adventure.</p>
<p><strong>3. make a local config that can be placed in gitignore</strong><br />
Here you can store config parameters and overwrite common ones to fit your local setup. Put it in the gitignore so your fellow devs can do the same.<br />
Therefor we need to hack the AppKernel.php and redefine the registerContainerConfiguration method like so.<br />
<script src="https://gist.github.com/2775277.js"></script><noscript><pre><code class="language-php php">public function registerContainerConfiguration(LoaderInterface $loader) {

        if ($this-&gt;getEnvironment() == &#039;dev&#039;) {
            $extrafiles = array(
                __DIR__ . &#039;/config/config_local.yml&#039;,
            );
            foreach ($extrafiles as $filename) {
                if (file_exists($filename) &amp;&amp; is_readable($filename)) {
                    $loader-&gt;load($filename);
                }
            }
        } else {
            $loader-&gt;load(__DIR__ . &#039;/config/config_&#039; . $this-&gt;getEnvironment() . &#039;.yml&#039;);
        }
}</code></pre></noscript></p>
<p>Read more about it:<br />
<a href="http://stackoverflow.com/questions/7338767/can-i-include-an-optional-config-file-in-symfony2/10336451#10336451">http://stackoverflow.com/questions/7338767/can-i-include-an-optional-config-file-in-symfony2/10336451#10336451</a></p>
<p>Now have fun with bundles &#038; services.</p>The post <a href="https://nerdpress.org/2012/05/23/symfony2-starter-tweaks/">Symfony2 Starter Tweaks</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></content:encoded>
					
					<wfw:commentRss>https://nerdpress.org/2012/05/23/symfony2-starter-tweaks/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
