![]() |
|
Snippets |
|
A possible way to implement themes in symfony is to use a different layout depending on the environment that is active. Although that's obviously not the original purpose of symfony's environment model, this solution offers great flexibility which is actually superior to regular themes. It is superior because you may have your page completely rearranged and layouted differently without changing one single line of code in your actions.
Let's say we have a website which shall be displayed nicely on mobile devices as well. For this purpose we create two environments: 'frontend' and 'mobile' (names are arbitrary).
We add the following filter class to the /lib directory of our application:
class myEnvironmentLayoutFilter extends sfFilter { public function execute($filterChain) { switch (SF_ENVIRONMENT) { case 'frontend': $this->getContext()->getController()->getActionStack()->getFirstEntry()->getActionInstance()->setLayout('layout_frontend'); break; case 'mobile': $this->getContext()->getController()->getActionStack()->getFirstEntry()->getActionInstance()->setLayout('layout_mobile'); break; } $filterChain->execute(); switch (SF_ENVIRONMENT) { case 'frontend': $this->getContext()->getResponse()->addStyleSheet('frontend'); break; case 'mobile': $this->getContext()->getResponse()->addStyleSheet('mobile'); break; } } }
Note the environment-dependent style sheets.
And in filters.yml:
# generally, you will want to insert your own filters here myEnvironmentLayoutFilter: class: myEnvironmentLayoutFilter
The advantages should be obvious: performance, as no filesystem access is necessary (no need to check for certain directories or template files). flexibility and expandability - features can be easily added to just one theme/layout, components can be arranged as needed, feature-stripped versions of a website may be realized, too. Less redundant template code. Easy to maintain.
With this snippet we can implement the following functionalities from the global configuration file config/config.php:
Access control to the applications in certain environments. For example, disable access in the environment of development from production server.
IP access control to certain applications of the project. For example, to protect backend or administration zones of the project.
To have several global configuration files of config/config.php according to the environment in which we are. For example, to define different constants for each environment.
I developed a mini-library (config/config.lib.php) to include and use in the global config file config/config.php.
<?php /** * IP access control to an environment * * @param string $env environment name * @param array $ips IPs that has access * @param string $clientip client IP */ function envCorrecte ($env,$ips,$clientip) { if (SF_ENVIRONMENT==$env) { $acces=0; foreach($ips as $ip) { $ipclient=substr($clientip,0,strlen($ip)); if ($ipclient==$ip) { $acces=1; } } if ($acces==0) { echo "Keep away! this is not a public environment."; exit; } } } /** * IP access control to an application of the symfony project * * @param array $apps no public applications * @param array $ips IPs that has access * @param string $clientip client IP */ function appCorrecte ($apps,$ips,$clientip) { // solve problem with key '0' in array $apps_aux[0]=''; $apps = $apps_aux + $apps; // if (array_search(SF_APP,$apps)!=false) { $acces=0; foreach($ips as $ip) { $ADR=$HTTP_SERVER_VARS['REMOTE_ADDR']; $ipclient=substr($clientip,0,strlen($ip)); if ($ipclient==$ip) { $acces=1; } } if ($acces==0) { echo "Keep away! ".SF_APP." is not a public application."; exit; } } } ?>
Finally config/config.php file can be like this:
<?php include("config.lib.php"); /** * IP access control to 'env' and 'int' environments */ $ip_dev=array("10.138.0.","192.168."); $ip_int=array("10.138.0.","192.168."); envCorrecte('env',$ip_env,$HTTP_SERVER_VARS['REMOTE_ADDR']); envCorrecte('int',$ip_int,$HTTP_SERVER_VARS['REMOTE_ADDR']); /** * IP access control to backend applications */ $ip_apps=array("10.138.0.","192.168.","w.x.y.z"); $apps=array('myapp_1','myapp_2','myapp_n'); appCorrecte($apps,$ip_apps,$HTTP_SERVER_VARS['REMOTE_ADDR']); /** * Custom config.php file for each environment */ include("config.".SF_ENVIRONMENT.".php"); /** * Common config.php file for all environments */ define("example","content1"); ?>