<?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>controller &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/controller/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>How to Dump the Generated Zend_Db SQL Query</title>
		<link>/2012/02/09/how-to-dump-the-generated-zend_db-sql-query/</link>
		<comments>/2012/02/09/how-to-dump-the-generated-zend_db-sql-query/#comments</comments>
		<pubDate>Thu, 09 Feb 2012 14:39:48 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[Cross-platform software]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[From]]></category>
		<category><![CDATA[Insert]]></category>
		<category><![CDATA[PHP programmer]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[select]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL keywords]]></category>

		<guid isPermaLink="false">/?p=2709</guid>
		<description><![CDATA[The Typical PHP Approach Typically a PHP programmer will write his SQL query as a string and will execute it via mysql_query. $sql = "SELECT * FROM my_table"; $resource = mysql_query($sql); So eventually when you want to dump this &#8220;complex&#8221; query, or whatever query there is, you can simply &#8220;echo&#8221; it and see what’s its &#8230; <a href="/2012/02/09/how-to-dump-the-generated-zend_db-sql-query/" class="more-link">Continue reading <span class="screen-reader-text">How to Dump the Generated Zend_Db SQL Query</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/04/29/debugging-in-zend-framework/" rel="bookmark" title="Debugging in Zend Framework">Debugging in Zend Framework </a></li>
<li><a href="/2010/08/05/fetching-rows-with-zend_db-fetch/" rel="bookmark" title="Fetching Rows With Zend_Db fetch()">Fetching Rows With Zend_Db fetch() </a></li>
<li><a href="/2010/07/28/which-model-should-contain-that-method/" rel="bookmark" title="Which Model Should Contain That Method?">Which Model Should Contain That Method? </a></li>
<li><a href="/2010/04/26/mysql-expressions-in-zend-framework/" rel="bookmark" title="MySQL Expressions in Zend Framework">MySQL Expressions in Zend Framework </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>The Typical PHP Approach</h2>
<p>Typically a PHP programmer will write his SQL query as a string and will execute it via <a href="http://php.net/manual/en/function.mysql-query.php" title="PHP: mysql_query" target="_blank">mysql_query</a>.</p>
<pre lang="PHP">
$sql = "SELECT * FROM my_table";
$resource = mysql_query($sql);
</pre>
<p>So eventually when you want to dump this &#8220;complex&#8221; query, or whatever query there is, you can simply &#8220;echo&#8221; it and see what’s its syntax. </p>
<pre lang="PHP">
// this query is WRONG because of the where clause
$sql = "SELECT * FROM my_table WHERE id = ";

// dump and debug the wrong query
die($sql);

// this line won't be executed
$resource = mysql_query($sql);
</pre>
<p>So far so good, but things appear to be a bit different when you start to work with <a href="http://framework.zend.com/" title="Zend Framework" target="_blank">Zend Framework</a>. Higher levels of abstraction come with slightly more difficult ways to dump (debug) your SQL queries.</p>
<p>OK you&#8217;ve two options. Using <a href="http://framework.zend.com/manual/en/zend.db.select.html" title="Zend_Db_Select" target="_blank">Zend_Db_Select</a> or &#8230; not.<br />
<span id="more-2709"></span></p>
<h2>Dump Zend_Db_Select Query</h2>
<p>Debugging queries generated with Zend_Db_Select is as easy as the following snippet. Let’s say there’s a db table “users” and typically the model is called Users.</p>
<pre lang="PHP">
class Users extends Zend_Db_Table
{
	protected $_name = 'users';

	public function getSomeUsers()
	{
		$select = $this->select()
				->from(this->_name)
				->where('id > ');

		$rows = $this->fetchAll($select);
	}
}
</pre>
<p>Obviously this code is wrong, because the &#8220;where&#8221; clause is wrong and we don&#8217;t pass any values to it. So now the question is how can we dump this SQL. Well, since we use Zend_Db_Select, so the simplest way to dump the SQL is by calling __toString(). The code above will become as follows.</p>
<pre lang="PHP">
class Users extends Zend_Db_Table
{
	protected $_name = ‘users’;

	public function getSomeUsers()
	{
		$select = $this->select()
				->from(this->_name)
				->where('id > ');

		die($select->__toString());

		$rows = $this->fetchAll($select);
	}
}
</pre>
<p>That&#8217;s great, but how can we dump update/insert queries for instance?</p>
<h2>Dump with Zend_Db_Profiler</h2>
<p>The way you should dump insert or update queries is a bit different. You should use <a href="http://framework.zend.com/manual/en/zend.db.profiler.html" title="Zend_Db_Profiler" target="_blank">Zend_Db_Profiler</a>, which is a more complex way to debug (profile) your queries. You can not only debug insert or update queries, of course, but any queries generated by the Zend_Db abstraction. Let’s see how.</p>
<p>Now from a controller perspective this model can be just called as:</p>
<pre lang="PHP">
$users = new Users();
$users->update(array('name' => 'my name'), 'id =');
</pre>
<p>Obviously this code is wrong, again because of the where clause and now the question is how we can debug the generated SQL. The answer is: using Zend_Db_Profiler. Looking back again to the model, this should look like this.</p>
<pre lang="PHP">
class Users extends Zend_Db_Table
{
	protected $_name = ‘users’;

	public function updateSomeUsers()
	{
		// first enable the profiler
		$this->getProfiler()->setEnabled(true);

		// try to execute
		$this->update(array('name' => 'my name'), 'id = ');

		// dump the SQL
		Zend_Debug::dump($this->getProfiler()->getLastQueryProfile()->getQuery());

		// don't forget to disable the profiler is
		// you don't need it anymore
		$this->getProfiler()->setEnabled(false);
	}
}
</pre>
<p>The thing is that since Zend_Db is using prepared statements, the last row (the one with the Zend_Debug::&#8230;) will dump something like the following line.</p>
<pre lang="SQL">
UPDATE `users` SET `name` = ? WHERE (id =)
</pre>
<p>So as you can see, actually here you don’t see the name’s value, which should be “my name”. To see this you should call getQueryParams().</p>
<pre lang="PHP">
class Users extends Zend_Db_Table
{
	protected $_name = 'users';

	public function updateSomeUsers()
	{
		// first enable the profiler
		$this->getProfiler()->setEnabled(true);

		// try to execute
		$this->update(array('name' => 'my name'), 'id = ');

		// dump the SQL
		Zend_Debug::dump($this->getProfiler()->getLastQueryProfile()->getQuery());
		Zend_Debug::dump($this->getProfiler()->getLastQueryProfile()->getQueryParams());

		// don't forget to disable the profiler is
		// you don't need it anymore
		$this->getProfiler()->setEnabled(false);
</pre>
<p>Now the debug info will be something like this.</p>
<pre lang="PHP">
UPDATE `users` SET `name` = ? WHERE (id =)
array
	1 => string 'my name' (length 7)
</pre>
<p>Calling this from the controller there are some changes. Take a look at the example bellow.</p>
<pre lang="PHP">
$users = new Users();

// enable the profiler
$users->getAdapter()->getProfiler()->setEnabled(true);

// execute (or at least try to)
$users->update(array('name' => 'my name'), 'id =');

// debugdump the SQL
Zend_Debug::dump($users->getAdapter()->getProfiler()->getLastQueryProfile()->getQuery());
Zend_Debug::dump($users->getAdapter()->getProfiler()->getLastQueryProfile()->getQueryParams());

// don't forget to disable the profiler is
// you don't need it anymore
$users->getAdapter()->getProfiler()->setEnabled(false);
</pre>
<p>In this case you should use the longer model_name->getAdapter()</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/04/29/debugging-in-zend-framework/" rel="bookmark" title="Debugging in Zend Framework">Debugging in Zend Framework </a></li>
<li><a href="/2010/08/05/fetching-rows-with-zend_db-fetch/" rel="bookmark" title="Fetching Rows With Zend_Db fetch()">Fetching Rows With Zend_Db fetch() </a></li>
<li><a href="/2010/07/28/which-model-should-contain-that-method/" rel="bookmark" title="Which Model Should Contain That Method?">Which Model Should Contain That Method? </a></li>
<li><a href="/2010/04/26/mysql-expressions-in-zend-framework/" rel="bookmark" title="MySQL Expressions in Zend Framework">MySQL Expressions in Zend Framework </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/02/09/how-to-dump-the-generated-zend_db-sql-query/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Models in Zend Framework &#8211; Initialize All Methods with init()</title>
		<link>/2010/08/09/models-in-zend-framework-initialize-all-methods-with-init/</link>
		<comments>/2010/08/09/models-in-zend-framework-initialize-all-methods-with-init/#comments</comments>
		<pubDate>Mon, 09 Aug 2010 19:05:05 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[Education]]></category>
		<category><![CDATA[given controller]]></category>
		<category><![CDATA[Init]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Web 2.0]]></category>
		<category><![CDATA[Web application frameworks]]></category>

		<guid isPermaLink="false">/?p=1890</guid>
		<description><![CDATA[In the Zend Framework&#8217;s documentation there are lots of examples how you can initialize all the actions in a given controller &#8211; by simply adding the init() public method in the controller&#8217;s code:<div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/08/06/setting-up-zend-framework-with-modules/" rel="bookmark" title="Setting Up Zend Framework with Modules">Setting Up Zend Framework with Modules </a></li>
<li><a href="/2010/06/23/bind-zend-action-with-non-default-view-part-2/" rel="bookmark" title="Bind Zend Action with Non-Default View &#8211; Part 2">Bind Zend Action with Non-Default View &#8211; Part 2 </a></li>
<li><a href="/2010/07/07/default-error-handling-in-zend-framework/" rel="bookmark" title="Default Error Handling in Zend Framework">Default Error Handling in Zend Framework </a></li>
<li><a href="/2010/07/06/zend-framework-simple-acl-front-controller-plugin/" rel="bookmark" title="Zend Framework: Simple Acl Front Controller Plugin">Zend Framework: Simple Acl Front Controller Plugin </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>In the Zend Framework&#8217;s documentation there are lots of examples how you can initialize all the actions in a given controller &#8211; by simply adding the init() public method in the controller&#8217;s code:</p>
<pre lang="php">
<?php

class IndexController extends Zend_Controller_Action
{
	public function init()
	{
		echo 'foo';	
	}	
	
	public function indexAction()
	{
		// first the 'foo' string will be printed
		echo 'bar';	
	}
}
</pre>
<p>But did you know that you can do the same thing with any model in ZF? However you can setup a cache for every method or something else, but definitely it will execute for every method:</p>
<pre lang="php">
<?php

class MyModel
{
	public function init()
	{
		// prepare the cache setup
	}	
	
	public function readAll()
	{
		// the cache is already setup
		$sql = '...';
		// ...
	}
}
</pre>
<p>Of course that means that directly calling the readAll() function the init() method is called also - automatically.</p>
<h2>Conclusion</h2>
<p>There are good and bad parts about this. You'll have this code executed for every method and if you have twenty of them and the init() method is practically used for only a couple of the member functions - than this will be useless.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/08/06/setting-up-zend-framework-with-modules/" rel="bookmark" title="Setting Up Zend Framework with Modules">Setting Up Zend Framework with Modules </a></li>
<li><a href="/2010/06/23/bind-zend-action-with-non-default-view-part-2/" rel="bookmark" title="Bind Zend Action with Non-Default View &#8211; Part 2">Bind Zend Action with Non-Default View &#8211; Part 2 </a></li>
<li><a href="/2010/07/07/default-error-handling-in-zend-framework/" rel="bookmark" title="Default Error Handling in Zend Framework">Default Error Handling in Zend Framework </a></li>
<li><a href="/2010/07/06/zend-framework-simple-acl-front-controller-plugin/" rel="bookmark" title="Zend Framework: Simple Acl Front Controller Plugin">Zend Framework: Simple Acl Front Controller Plugin </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/08/09/models-in-zend-framework-initialize-all-methods-with-init/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Setting Up Zend Framework with Modules</title>
		<link>/2010/08/06/setting-up-zend-framework-with-modules/</link>
		<comments>/2010/08/06/setting-up-zend-framework-with-modules/#respond</comments>
		<pubDate>Fri, 06 Aug 2010 12:59:21 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[Cross-platform software]]></category>
		<category><![CDATA[Front Controller pattern]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[modular zend]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Software architecture]]></category>
		<category><![CDATA[Software design patterns]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[Software framework]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[zend framework modules]]></category>

		<guid isPermaLink="false">/?p=1826</guid>
		<description><![CDATA[Typical Zend App Typically you&#8217;ve one module in your Zend App &#8211; the default one. In the basic installation of the framework, you put all the controllers, models and views directly in the application folder, as described below. /application - /controllers - /IndexController.php - /models - /MyModel.php - /views - /scripts - /index/index.phtml /library - &#8230; <a href="/2010/08/06/setting-up-zend-framework-with-modules/" class="more-link">Continue reading <span class="screen-reader-text">Setting Up Zend Framework with Modules</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/07/21/setting-up-global-cache-in-zend-framework/" rel="bookmark" title="Setting Up Global Cache in Zend Framework">Setting Up Global Cache in Zend Framework </a></li>
<li><a href="/2010/01/29/escaping-strings-in-a-zend-framework-view-prevent-unclosed-tags/" rel="bookmark" title="Escaping strings in a Zend Framework view. Prevent unclosed tags!">Escaping strings in a Zend Framework view. Prevent unclosed tags! </a></li>
<li><a href="/2010/06/24/zend-framework-inject-javascript-code-in-a-actionview/" rel="bookmark" title="Zend Framework: Inject JavaScript Code in a Action/View">Zend Framework: Inject JavaScript Code in a Action/View </a></li>
<li><a href="/2010/06/23/bind-zend-action-with-non-default-view-part-2/" rel="bookmark" title="Bind Zend Action with Non-Default View &#8211; Part 2">Bind Zend Action with Non-Default View &#8211; Part 2 </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Typical Zend App</h2>
<p>Typically you&#8217;ve one module in your Zend App &#8211; the default one. In the basic installation of the framework, you put all the controllers, models and views directly in the application folder, as described below.</p>
<pre lang="php">
/application
	- /controllers
		- /IndexController.php
	- /models
		- /MyModel.php
	- /views
		- /scripts
			- /index/index.phtml
/library
	- /Zend
/public_html
	- /images
	- /scripts
</pre>
<h2>Bigger Apps &#8211; More Code</h2>
<p>When the application becomes bigger and bigger the controller, models and views/scripts directories contain more and more files. That&#8217;s a bit odd, because it becomes difficult to maintain, and than the modules come in hand.</p>
<h2>Modules in a Zend App</h2>
<p>When it comes to setting up modular Zend App there are tons of articles in the web, but let me show you a simple directory layout and &#8230; sample code that sets up the framework.</p>
<pre lang="php">
/application
	- /modules
		+ /admin
			- /controllers
				- /IndexController.php
			- /views
				- /scripts
					- /index/index.phtml
		+ /default
			- /controllers
				- /IndexController.php
			- /views
				- /scripts
					- /index/index.phtml
	- /models
/library
	- /Zend
/public_html
	- /images
	- /scripts
</pre>
<h2>Source</h2>
<p>Simply add this into the bootstrap:</p>
<pre lang="php">
$frontController->addModuleDirectory(APPLICATION_PATH . '/modules');
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/07/21/setting-up-global-cache-in-zend-framework/" rel="bookmark" title="Setting Up Global Cache in Zend Framework">Setting Up Global Cache in Zend Framework </a></li>
<li><a href="/2010/01/29/escaping-strings-in-a-zend-framework-view-prevent-unclosed-tags/" rel="bookmark" title="Escaping strings in a Zend Framework view. Prevent unclosed tags!">Escaping strings in a Zend Framework view. Prevent unclosed tags! </a></li>
<li><a href="/2010/06/24/zend-framework-inject-javascript-code-in-a-actionview/" rel="bookmark" title="Zend Framework: Inject JavaScript Code in a Action/View">Zend Framework: Inject JavaScript Code in a Action/View </a></li>
<li><a href="/2010/06/23/bind-zend-action-with-non-default-view-part-2/" rel="bookmark" title="Bind Zend Action with Non-Default View &#8211; Part 2">Bind Zend Action with Non-Default View &#8211; Part 2 </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/08/06/setting-up-zend-framework-with-modules/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setting Up Global Cache in Zend Framework</title>
		<link>/2010/07/21/setting-up-global-cache-in-zend-framework/</link>
		<comments>/2010/07/21/setting-up-global-cache-in-zend-framework/#comments</comments>
		<pubDate>Wed, 21 Jul 2010 11:17:48 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[Front Controller pattern]]></category>
		<category><![CDATA[Human Interest]]></category>
		<category><![CDATA[large scale web application]]></category>
		<category><![CDATA[PHP For Applications]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[Software framework]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[Zend Technologies]]></category>

		<guid isPermaLink="false">/?p=1830</guid>
		<description><![CDATA[In a large scale web application, especially based on Zend Framework, there are lot&#8217;s of components that support built in cache support. Such are Zend_Db, Zend_Translate, Zend_Date, etc. Also you may need cache support wherever in the app, so my advice is to setup a cache instance in the &#8220;beginning&#8221;, into the bootstrap.php or even &#8230; <a href="/2010/07/21/setting-up-global-cache-in-zend-framework/" class="more-link">Continue reading <span class="screen-reader-text">Setting Up Global Cache in Zend Framework</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/07/19/zend-framework-cache-database-table-schemes/" rel="bookmark" title="Zend Framework: Cache Database Table Schemes">Zend Framework: Cache Database Table Schemes </a></li>
<li><a href="/2010/08/06/setting-up-zend-framework-with-modules/" rel="bookmark" title="Setting Up Zend Framework with Modules">Setting Up Zend Framework with Modules </a></li>
<li><a href="/2010/01/27/theory-of-caching-zend_cache-zend-optimizer/" rel="bookmark" title="Theory of caching. Zend_Cache &#038; Zend Optimizer.">Theory of caching. Zend_Cache &#038; Zend Optimizer. </a></li>
<li><a href="/2010/08/09/models-in-zend-framework-initialize-all-methods-with-init/" rel="bookmark" title="Models in Zend Framework &#8211; Initialize All Methods with init()">Models in Zend Framework &#8211; Initialize All Methods with init() </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>In a large scale web application, especially based on Zend Framework, there are lot&#8217;s of components that support built in cache support. Such are <a href="http://framework.zend.com/manual/en/zend.db.html">Zend_Db</a>, <a href="http://zendframework.com/manual/en/zend.translate.html">Zend_Translate</a>, <a href="http://framework.zend.com/manual/en/zend.date.html">Zend_Date</a>, etc. Also you may need cache support wherever in the app, so my advice is to setup a cache instance in the &#8220;beginning&#8221;, into the bootstrap.php or even better &#8211; into a front controller plugin, and to store it into the Zend_Registry. Thus you&#8217;ve to change only the lifetime for specific needs:</p>
<pre lang="php">
<?php

class CacheInit extends Zend_Controller_Plugin_Abstract
{
    public function __construct()
    {
        $frontendOptions = array(
            'automatic_serialization' => true,
            'lifetime' => 60
        );

        $backendOptions  = array(
            'cache_dir' => realpath(APPLICATION_PATH . '/../cache')
        );

        $cache = Zend_Cache::factory('Core',
                                     'File',
                                     $frontendOptions,
                                     $backendOptions);

        Zend_Registry::set('cache', $cache);
    }
}
</pre>
<p>Than add it as a front controller plugin:</p>
<pre lang="php">
// cache plugin
$frontController->registerPlugin(new CacheInit());
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/07/19/zend-framework-cache-database-table-schemes/" rel="bookmark" title="Zend Framework: Cache Database Table Schemes">Zend Framework: Cache Database Table Schemes </a></li>
<li><a href="/2010/08/06/setting-up-zend-framework-with-modules/" rel="bookmark" title="Setting Up Zend Framework with Modules">Setting Up Zend Framework with Modules </a></li>
<li><a href="/2010/01/27/theory-of-caching-zend_cache-zend-optimizer/" rel="bookmark" title="Theory of caching. Zend_Cache &#038; Zend Optimizer.">Theory of caching. Zend_Cache &#038; Zend Optimizer. </a></li>
<li><a href="/2010/08/09/models-in-zend-framework-initialize-all-methods-with-init/" rel="bookmark" title="Models in Zend Framework &#8211; Initialize All Methods with init()">Models in Zend Framework &#8211; Initialize All Methods with init() </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/07/21/setting-up-global-cache-in-zend-framework/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Zend Framework: Connect MySQL</title>
		<link>/2010/07/20/zend-framework-connect-mysql/</link>
		<comments>/2010/07/20/zend-framework-connect-mysql/#respond</comments>
		<pubDate>Tue, 20 Jul 2010 11:12:45 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[Adapter pattern]]></category>
		<category><![CDATA[Applied mathematics]]></category>
		<category><![CDATA[Bootstrapping]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[MySQL Americas Inc.]]></category>
		<category><![CDATA[Resampling]]></category>
		<category><![CDATA[Software design patterns]]></category>
		<category><![CDATA[Statistical inference]]></category>
		<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">/?p=1832</guid>
		<description><![CDATA[There are so many tutorials about that, but let me add it one more time. You&#8217;ve to use the PDO_MYSQL adapter: // where params include adapter, host // username, password and dbname $db = new Zend_Db_Adapter_Pdo_Mysql($params); and set the default adapter Zend_Db_Table::setDefaultAdapter($db); That can be done in the bootstrap or into a front controller plugin!<div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2009/06/19/zend-framework-quick-tutorial-part-3-front-controller-plugins/" rel="bookmark" title="Zend Framework &#8211; quick tutorial (part 3) &#8211; front controller plugins">Zend Framework &#8211; quick tutorial (part 3) &#8211; front controller plugins </a></li>
<li><a href="/2010/07/21/setting-up-global-cache-in-zend-framework/" rel="bookmark" title="Setting Up Global Cache in Zend Framework">Setting Up Global Cache in Zend Framework </a></li>
<li><a href="/2009/12/09/connect-mysql-from-zend-server-ce-trough-unix-socket/" rel="bookmark" title="Connect MySQL from Zend Server CE trough unix socket!">Connect MySQL from Zend Server CE trough unix socket! </a></li>
<li><a href="/2010/04/26/mysql-expressions-in-zend-framework/" rel="bookmark" title="MySQL Expressions in Zend Framework">MySQL Expressions in Zend Framework </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>There are so many tutorials about that, but let me add it one more time. You&#8217;ve to use the PDO_MYSQL adapter:</p>
<pre lang="php">
// where params include adapter, host
// username, password and dbname
$db = new Zend_Db_Adapter_Pdo_Mysql($params);
</pre>
<p>and set the default adapter</p>
<pre lang="php">
Zend_Db_Table::setDefaultAdapter($db);
</pre>
<p>That can be done in the bootstrap or into a front controller plugin!</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2009/06/19/zend-framework-quick-tutorial-part-3-front-controller-plugins/" rel="bookmark" title="Zend Framework &#8211; quick tutorial (part 3) &#8211; front controller plugins">Zend Framework &#8211; quick tutorial (part 3) &#8211; front controller plugins </a></li>
<li><a href="/2010/07/21/setting-up-global-cache-in-zend-framework/" rel="bookmark" title="Setting Up Global Cache in Zend Framework">Setting Up Global Cache in Zend Framework </a></li>
<li><a href="/2009/12/09/connect-mysql-from-zend-server-ce-trough-unix-socket/" rel="bookmark" title="Connect MySQL from Zend Server CE trough unix socket!">Connect MySQL from Zend Server CE trough unix socket! </a></li>
<li><a href="/2010/04/26/mysql-expressions-in-zend-framework/" rel="bookmark" title="MySQL Expressions in Zend Framework">MySQL Expressions in Zend Framework </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/07/20/zend-framework-connect-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Media RSS and ZF &#8211; Part 2</title>
		<link>/2010/07/14/media-rss-and-zf-part-2/</link>
		<comments>/2010/07/14/media-rss-and-zf-part-2/#respond</comments>
		<pubDate>Wed, 14 Jul 2010 19:05:15 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[Computer file formats]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[RSS]]></category>
		<category><![CDATA[Software architecture]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[Software framework]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[Web 2.0]]></category>
		<category><![CDATA[Web application frameworks]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">/?p=1803</guid>
		<description><![CDATA[Here&#8217;s the promised chunk of code making the most simple bridge between Zend Framework and Media RSS. Step 1 Just add a simple action in a controller: class IndexController extends Zend_Controller_Action { public function indexAction() { $this->getResponse()->setHeader('Content-Type', 'application/xml'); $this->view->somevar = 'some value'; echo $this->view->render('index/index.xml'); $this->_helper->layout()->disableLayout(); $this->_helper->viewRenderer->setNoRender(true); } } Step 2 After you&#8217;ve setup the xml &#8230; <a href="/2010/07/14/media-rss-and-zf-part-2/" class="more-link">Continue reading <span class="screen-reader-text">Media RSS and ZF &#8211; Part 2</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/07/13/zend-framework-and-media-rss/" rel="bookmark" title="Zend Framework and Media RSS">Zend Framework and Media RSS </a></li>
<li><a href="/2010/06/23/bind-zend-action-with-non-default-view-part-2/" rel="bookmark" title="Bind Zend Action with Non-Default View &#8211; Part 2">Bind Zend Action with Non-Default View &#8211; Part 2 </a></li>
<li><a href="/2010/05/25/download-files-with-zend-framework/" rel="bookmark" title="Download Files with Zend Framework">Download Files with Zend Framework </a></li>
<li><a href="/2010/08/06/setting-up-zend-framework-with-modules/" rel="bookmark" title="Setting Up Zend Framework with Modules">Setting Up Zend Framework with Modules </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>Here&#8217;s the promised chunk of code making the most simple bridge between Zend Framework and Media RSS.</p>
<h2>Step 1</h2>
<p>Just add a simple action in a controller:</p>
<pre lang="php">
class IndexController extends Zend_Controller_Action
{
	public function indexAction()
	{
	    $this->getResponse()->setHeader('Content-Type', 'application/xml');
	    $this->view->somevar = 'some value';
	
	    echo $this->view->render('index/index.xml');
	
	    $this->_helper->layout()->disableLayout();
	    $this->_helper->viewRenderer->setNoRender(true);
	}
}
</pre>
<h2>Step 2</h2>
<p>After you&#8217;ve setup the xml file as a view script &#8211; you can simply access it as a normal Zend Framework view:</p>
<pre lang="xml">
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:av="http://www.searchvideo.com/schemas/av/1.0">
  <channel>
   <title><?php echo $this->somevar ?></title>
   ...
   other media rss elements
   ...
</channel>
</rss>
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/07/13/zend-framework-and-media-rss/" rel="bookmark" title="Zend Framework and Media RSS">Zend Framework and Media RSS </a></li>
<li><a href="/2010/06/23/bind-zend-action-with-non-default-view-part-2/" rel="bookmark" title="Bind Zend Action with Non-Default View &#8211; Part 2">Bind Zend Action with Non-Default View &#8211; Part 2 </a></li>
<li><a href="/2010/05/25/download-files-with-zend-framework/" rel="bookmark" title="Download Files with Zend Framework">Download Files with Zend Framework </a></li>
<li><a href="/2010/08/06/setting-up-zend-framework-with-modules/" rel="bookmark" title="Setting Up Zend Framework with Modules">Setting Up Zend Framework with Modules </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/07/14/media-rss-and-zf-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Default Error Handling in Zend Framework</title>
		<link>/2010/07/07/default-error-handling-in-zend-framework/</link>
		<comments>/2010/07/07/default-error-handling-in-zend-framework/#respond</comments>
		<pubDate>Wed, 07 Jul 2010 09:28:59 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[Exception handling]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[Technology/Internet]]></category>

		<guid isPermaLink="false">/?p=1775</guid>
		<description><![CDATA[Zend_Controller_Action_Exception There&#8217;s a cool feature in Zend Framework when a controller or action doesn&#8217;t exists. That&#8217;s really useful, because you simply write an ErrorController.php in you default module and every action&#8217;s exception sends the user to this controller/action and the job is done for you. What is important here is to show the error message &#8230; <a href="/2010/07/07/default-error-handling-in-zend-framework/" class="more-link">Continue reading <span class="screen-reader-text">Default Error Handling in Zend Framework</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/06/16/zend-examples-get-parameters-default-value/" rel="bookmark" title="Zend Examples: GET Parameters Default Value">Zend Examples: GET Parameters Default Value </a></li>
<li><a href="/2010/05/25/download-files-with-zend-framework/" rel="bookmark" title="Download Files with Zend Framework">Download Files with Zend Framework </a></li>
<li><a href="/2010/07/06/zend-framework-simple-acl-front-controller-plugin/" rel="bookmark" title="Zend Framework: Simple Acl Front Controller Plugin">Zend Framework: Simple Acl Front Controller Plugin </a></li>
<li><a href="/2009/12/20/redirecting-with-zend-framework-part-2/" rel="bookmark" title="Redirecting with Zend Framework &#8211; part 2">Redirecting with Zend Framework &#8211; part 2 </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Zend_Controller_Action_Exception</h2>
<p>There&#8217;s a cool feature in Zend Framework when a controller or action doesn&#8217;t exists. That&#8217;s really useful, because you simply write an ErrorController.php in you default module and every action&#8217;s exception sends the user to this controller/action and the job is done for you. What is important here is to show the error message at least on the development stage. Actually the request stores it as a parameter. Here&#8217;s some source:</p>
<pre lang="php">
<?php

class ErrorController extends Zend_Controller_Action
{
    public function errorAction()
    {
        $errorHandler = $this->getRequest()->getParam('error_handler');
        /* @var $error Zend_Controller_Action_Exception */
        $error = $errorHandler->exception;
        echo $error->getMessage();
    }
}
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/06/16/zend-examples-get-parameters-default-value/" rel="bookmark" title="Zend Examples: GET Parameters Default Value">Zend Examples: GET Parameters Default Value </a></li>
<li><a href="/2010/05/25/download-files-with-zend-framework/" rel="bookmark" title="Download Files with Zend Framework">Download Files with Zend Framework </a></li>
<li><a href="/2010/07/06/zend-framework-simple-acl-front-controller-plugin/" rel="bookmark" title="Zend Framework: Simple Acl Front Controller Plugin">Zend Framework: Simple Acl Front Controller Plugin </a></li>
<li><a href="/2009/12/20/redirecting-with-zend-framework-part-2/" rel="bookmark" title="Redirecting with Zend Framework &#8211; part 2">Redirecting with Zend Framework &#8211; part 2 </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/07/07/default-error-handling-in-zend-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend Framework: Simple Acl Front Controller Plugin</title>
		<link>/2010/07/06/zend-framework-simple-acl-front-controller-plugin/</link>
		<comments>/2010/07/06/zend-framework-simple-acl-front-controller-plugin/#respond</comments>
		<pubDate>Tue, 06 Jul 2010 19:00:59 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[access control list]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[Front Controller pattern]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Software design patterns]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[Web 2.0]]></category>
		<category><![CDATA[Web application frameworks]]></category>

		<guid isPermaLink="false">/?p=1771</guid>
		<description><![CDATA[Almost every web site need some abstraction over the access control list (ACL) to grant access of its users. As usual Zend Framework has quite good mechanism to deal with this &#8211; Zend_Acl. Out in the web there are a lot of resources about Zend_Acl&#8217;s usage, so I ain&#8217;t going to cover it one more &#8230; <a href="/2010/07/06/zend-framework-simple-acl-front-controller-plugin/" class="more-link">Continue reading <span class="screen-reader-text">Zend Framework: Simple Acl Front Controller Plugin</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2009/06/19/zend-framework-quick-tutorial-part-3-front-controller-plugins/" rel="bookmark" title="Zend Framework &#8211; quick tutorial (part 3) &#8211; front controller plugins">Zend Framework &#8211; quick tutorial (part 3) &#8211; front controller plugins </a></li>
<li><a href="/2010/08/06/setting-up-zend-framework-with-modules/" rel="bookmark" title="Setting Up Zend Framework with Modules">Setting Up Zend Framework with Modules </a></li>
<li><a href="/2010/07/21/setting-up-global-cache-in-zend-framework/" rel="bookmark" title="Setting Up Global Cache in Zend Framework">Setting Up Global Cache in Zend Framework </a></li>
<li><a href="/2010/06/10/json-and-zend-framework-zend_json/" rel="bookmark" title="JSON and Zend Framework? &#8211; Zend_Json">JSON and Zend Framework? &#8211; Zend_Json </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p><a href="/wp-content/uploads/2010/07/access.jpg"><img class="aligncenter size-full wp-image-1776" title="access" src="/wp-content/uploads/2010/07/access.jpg" alt="access with Zend_Acl" width="430" height="286" srcset="/wp-content/uploads/2010/07/access.jpg 430w, /wp-content/uploads/2010/07/access-300x199.jpg 300w" sizes="(max-width: 430px) 100vw, 430px" /></a></p>
<p>Almost every web site need some abstraction over the access control list (ACL) to grant access of its users. As usual Zend Framework has quite good mechanism to deal with this &#8211; <a title="Zend_Acl" href="http://framework.zend.com/manual/en/zend.acl.html" target="_blank">Zend_Acl</a>.</p>
<p>Out in the web there are a lot of resources about Zend_Acl&#8217;s usage, so I ain&#8217;t going to cover it one more time, but simply copy/paste a very small front controller plugin implementing the basic usage of Zend_Acl.</p>
<p>Note that instead of defining the __construct() here is called preDispatch where the request is passed as a parameter. However only by copy pasting not every answer will be given. That&#8217;s why I&#8217;m going to write more about Zend_Acl in my future posts, for now only the source code:</p>
<pre lang="php">
<?php

class AclInit extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        // create new acl object
        $acl = new Zend_Acl();

        // define resources. typically there are
        // only four resources from the CRUD functionality
        // but there can be added more resources
        $acl->add(new Zend_Acl_Resource('index'))
            ->add(new Zend_Acl_Resource('create'))
            ->add(new Zend_Acl_Resource('read'))
            ->add(new Zend_Acl_Resource('update'))
            ->add(new Zend_Acl_Resource('delete'));

        // define roles
        $acl->addRole(new Zend_Acl_Role('guest'))
            ->addRole(new Zend_Acl_Role('admin'));

        // define privileges
        $acl->allow('guest', array('index', 'read'))
            ->allow('admin');

        // setup acl in the registry for more
        Zend_Registry::set('acl', $acl);

        // check permissions
        if (!$acl->isAllowed('guest', $request->getActionName())) {
            $request->setControllerName('error');
            $request->setActionName('error');
        }
    }
}
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2009/06/19/zend-framework-quick-tutorial-part-3-front-controller-plugins/" rel="bookmark" title="Zend Framework &#8211; quick tutorial (part 3) &#8211; front controller plugins">Zend Framework &#8211; quick tutorial (part 3) &#8211; front controller plugins </a></li>
<li><a href="/2010/08/06/setting-up-zend-framework-with-modules/" rel="bookmark" title="Setting Up Zend Framework with Modules">Setting Up Zend Framework with Modules </a></li>
<li><a href="/2010/07/21/setting-up-global-cache-in-zend-framework/" rel="bookmark" title="Setting Up Global Cache in Zend Framework">Setting Up Global Cache in Zend Framework </a></li>
<li><a href="/2010/06/10/json-and-zend-framework-zend_json/" rel="bookmark" title="JSON and Zend Framework? &#8211; Zend_Json">JSON and Zend Framework? &#8211; Zend_Json </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/07/06/zend-framework-simple-acl-front-controller-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend Framework: Inject JavaScript Code in a Action/View</title>
		<link>/2010/06/24/zend-framework-inject-javascript-code-in-a-actionview/</link>
		<comments>/2010/06/24/zend-framework-inject-javascript-code-in-a-actionview/#comments</comments>
		<pubDate>Thu, 24 Jun 2010 14:39:49 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[Client-side JavaScript]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computer science]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[Cross-platform software]]></category>
		<category><![CDATA[Curly bracket programming languages]]></category>
		<category><![CDATA[JavaScript programming language]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Printf]]></category>
		<category><![CDATA[Scripting languages]]></category>

		<guid isPermaLink="false">/?p=1666</guid>
		<description><![CDATA[There is a view helper that can inject a head or inline script into your code. Simply by putting some JavaScript code into the view script wont do the job, because the .phtml file is grabbed and parsed by the Zend Framework and thus the code there wont be parsed as JavaScript and it wont &#8230; <a href="/2010/06/24/zend-framework-inject-javascript-code-in-a-actionview/" class="more-link">Continue reading <span class="screen-reader-text">Zend Framework: Inject JavaScript Code in a Action/View</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/06/23/bind-zend-action-with-non-default-view-part-2/" rel="bookmark" title="Bind Zend Action with Non-Default View &#8211; Part 2">Bind Zend Action with Non-Default View &#8211; Part 2 </a></li>
<li><a href="/2010/06/07/bind-zend-action-with-non-default-view/" rel="bookmark" title="Bind Zend Action with Non-Default View">Bind Zend Action with Non-Default View </a></li>
<li><a href="/2010/01/29/escaping-strings-in-a-zend-framework-view-prevent-unclosed-tags/" rel="bookmark" title="Escaping strings in a Zend Framework view. Prevent unclosed tags!">Escaping strings in a Zend Framework view. Prevent unclosed tags! </a></li>
<li><a href="/2010/05/25/download-files-with-zend-framework/" rel="bookmark" title="Download Files with Zend Framework">Download Files with Zend Framework </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>There is a view helper that can inject a head or inline script into your code.</p>
<p>Simply by putting some JavaScript code into the view script wont do the job, because the .phtml file is grabbed and parsed by the Zend Framework and thus the code there wont be parsed as JavaScript and it wont be executed as well by the browser.</p>
<h2>The Most Simple Way &#8230;</h2>
<p>is to put some place holder into the layout .phtml file and than from the controller&#8217;s action you can assign some JS code:</p>
<pre lang="php">
// layout.phtml
<?php
...
echo $this->layout()->scriptTags;
...
</pre>
<pre lang="php">
<?php

class IndexController extends Zend_Controller_Action
{
	public function indexAction() 
	{
		$this->view->layout()->scriptTags = '<script src="my.js"></script>';	
	}	
}
</pre>
<h2>Solution No. 2</h2>
<p>Although the first solution is correct it&#8217;s not the most clearest way, because you put the JavaScript code into the PHP, and this sooner or later becomes a mess. </p>
<pre lang="php">
<?php

class IndexController extends Zend_Controller_Action
{
	public function indexAction() 
	{
		$this->view->layout()->scriptTags = '<script src="my.js"></script>';
						  . '<script>alert("here\'s my" + "test")</script>';
	}	
}
</pre>
<p>The IDE is not highlighting the code, and you&#8217;ve to deal with too many quotes, which is bad! There is a better solution however &#8211; the inline script:</p>
<pre lang="php">
// script tags
/* @var $scripts Zend_View_Helper_InlineScript */
$scripts = $this->view->inlineScript();
$scripts->appendFile('my.js');
</pre>
<h2>Simply the Best</h2>
<p>Yeah, the best solution is something even better than the second one. Although the solution I&#8217;ve just mentioned is fine for including JS files, when you&#8217;ve to add some JS code you&#8217;ll have to put it again into the PHP. </p>
<pre lang="php">
$msg = 'some dynamically generated message';

// script tags
/* @var $scripts Zend_View_Helper_InlineScript */
$scripts = $this->view->inlineScript();
$scripts->appendFile('my.js');
$scripts->appendScript('alert("' . $msg . '")');
</pre>
<p>If there&#8217;s no relation between JavaScript and PHP you can simply put all this code into a separate .js file and load it from there. This is by the way the best solution because the file is cached by the browser, if the cache is enabled. But most of the time you perhaps have to send some variables from PHP to the JavaScript code.</p>
<p>It would be perfect if you had some kind of a variable placeholders &#8211; exactly as you have it in the Zend Framework&#8217;s view scripts!</p>
<h3>Another View</h3>
<p>This is completely possible &#8211; just forget about the default view &#8211; it renders the view script and it&#8217;s bind to the /views/scripts folder. You can make another, completely new, view instead! Here&#8217;s some source:</p>
<pre lang="php">
$view = new Zend_View();
$view->setBasePath('path_to_the_js_folder');

$view->msg = 'Some dynamically generated message';

// script tags
/* @var $scripts Zend_View_Helper_InlineScript */
$scripts = $this->view->inlineScript();
$scripts->appendFile('my.js');
$scripts->appendScript($view->render('inline.js'));
</pre>
<p>Thus now in the JS file you can have some placeholders!</p>
<pre lang="javascript">
// inline.js

// some js code
// ...
alert('<?php echo $this->msg ?>');
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/06/23/bind-zend-action-with-non-default-view-part-2/" rel="bookmark" title="Bind Zend Action with Non-Default View &#8211; Part 2">Bind Zend Action with Non-Default View &#8211; Part 2 </a></li>
<li><a href="/2010/06/07/bind-zend-action-with-non-default-view/" rel="bookmark" title="Bind Zend Action with Non-Default View">Bind Zend Action with Non-Default View </a></li>
<li><a href="/2010/01/29/escaping-strings-in-a-zend-framework-view-prevent-unclosed-tags/" rel="bookmark" title="Escaping strings in a Zend Framework view. Prevent unclosed tags!">Escaping strings in a Zend Framework view. Prevent unclosed tags! </a></li>
<li><a href="/2010/05/25/download-files-with-zend-framework/" rel="bookmark" title="Download Files with Zend Framework">Download Files with Zend Framework </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/06/24/zend-framework-inject-javascript-code-in-a-actionview/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Zend Examples: GET Parameters Default Value</title>
		<link>/2010/06/16/zend-examples-get-parameters-default-value/</link>
		<comments>/2010/06/16/zend-examples-get-parameters-default-value/#comments</comments>
		<pubDate>Wed, 16 Jun 2010 18:57:20 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[elegant solution]]></category>
		<category><![CDATA[Hypertext Transfer Protocol]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Uniform Resource Identifier]]></category>
		<category><![CDATA[World Wide Web]]></category>
		<category><![CDATA[Zend Technologies]]></category>

		<guid isPermaLink="false">/?p=1619</guid>
		<description><![CDATA[PHP Style As you may know in PHP you can access everything in the request uri by accessing the global $_GET array. If there is something like that in the browser&#8217;s address field: www.example.com/index.php?controller=index&#038;action=test, you can simply get the values by that: echo $_GET['controller']; // this will print "index" echo $_GET['action']; // this will print &#8230; <a href="/2010/06/16/zend-examples-get-parameters-default-value/" class="more-link">Continue reading <span class="screen-reader-text">Zend Examples: GET Parameters Default Value</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/07/07/default-error-handling-in-zend-framework/" rel="bookmark" title="Default Error Handling in Zend Framework">Default Error Handling in Zend Framework </a></li>
<li><a href="/2010/06/23/bind-zend-action-with-non-default-view-part-2/" rel="bookmark" title="Bind Zend Action with Non-Default View &#8211; Part 2">Bind Zend Action with Non-Default View &#8211; Part 2 </a></li>
<li><a href="/2010/06/07/bind-zend-action-with-non-default-view/" rel="bookmark" title="Bind Zend Action with Non-Default View">Bind Zend Action with Non-Default View </a></li>
<li><a href="/2009/12/20/redirecting-with-zend-framework-part-2/" rel="bookmark" title="Redirecting with Zend Framework &#8211; part 2">Redirecting with Zend Framework &#8211; part 2 </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>PHP Style</h2>
<p>As you may know in PHP you can access everything in the request uri by accessing the global $_GET array. If there is something like that in the browser&#8217;s address field: www.example.com/index.php?controller=index&#038;action=test, you can simply get the values by that:</p>
<pre lang="php">
echo $_GET['controller']; // this will print "index"
echo $_GET['action'];     // this will print "test"
</pre>
<h2>Zend Framework, Uri &#038; Request Params</h2>
<p>If you code with Zend Framework, you should know already, and that&#8217;s perhaps the first thing you&#8217;ve learned about Zend, that $_GET params can be accessed by calling the requrest&#8217;s getParam() method. But first of all the request uri will be different. The Zend&#8217;s convetion is to place after the domain name first the module name (which is omited when there&#8217;s only one module), than the controller&#8217;s name followed by the action and the key value pairs of all parameters. In that scheme the request uri above will look like that:</p>
<pre lang="php">
http://www.example.com/index/test
</pre>
<p>Here the keywords &#8220;controller&#8221; and &#8220;action&#8221; are omitted. This is cool &#8211; it&#8217;s more user friendly and it definitely helps the SEO.</p>
<h2>Get the $_GET</h2>
<p>Once the uri is setup like so &#8211; /index/test you can access it via the Zend way:</p>
<pre lang="php">
echo $this->getRequest()->getParam('controller'); // this will print "index"
</pre>
<p>The cool thing is that in the first case you don&#8217;t have any prevention of a missing value, while in the second case there is a second parameter or the getParam() method that does this job. What if the uri is www.example.com/index.php?controller=&#038;action=test than by printing the $_GET[&#8216;controller&#8217;] you&#8217;ll get nothing. In other hand even this:</p>
<pre lang="php">
echo $this->getRequest()->getParam('action'); 
</pre>
<p>won&#8217;t return &#8220;test&#8221; if the uri is http://www.example.com/index/</p>
<p><em><strong>Note:</strong> actually here the default &#8220;index&#8221; action will be referenced!</em></p>
<p>That&#8217;s where the power of the framework comes. In the first case the solution is:</p>
<pre lang="php">
echo (empty($_GET['controller']) ? 'default': $_GET['controller']);
</pre>
<p>while in Zend there&#8217;s more elegant solution:</p>
<pre lang="php">
echo $this->getRequest()->getParam('action', 'test');
</pre>
<p>Thus when the action param is missing the &#8220;test&#8221; value is considered as default! Very useful!</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/07/07/default-error-handling-in-zend-framework/" rel="bookmark" title="Default Error Handling in Zend Framework">Default Error Handling in Zend Framework </a></li>
<li><a href="/2010/06/23/bind-zend-action-with-non-default-view-part-2/" rel="bookmark" title="Bind Zend Action with Non-Default View &#8211; Part 2">Bind Zend Action with Non-Default View &#8211; Part 2 </a></li>
<li><a href="/2010/06/07/bind-zend-action-with-non-default-view/" rel="bookmark" title="Bind Zend Action with Non-Default View">Bind Zend Action with Non-Default View </a></li>
<li><a href="/2009/12/20/redirecting-with-zend-framework-part-2/" rel="bookmark" title="Redirecting with Zend Framework &#8211; part 2">Redirecting with Zend Framework &#8211; part 2 </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/06/16/zend-examples-get-parameters-default-value/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
