<?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>Web application frameworks &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/web-application-frameworks/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 Overcome Zend_Cache_Frontend_Page&#8217;s Problem with Cookies</title>
		<link>/2010/11/03/how-to-overcome-zend_cache_frontend_pages-problem-with-cookies/</link>
		<comments>/2010/11/03/how-to-overcome-zend_cache_frontend_pages-problem-with-cookies/#comments</comments>
		<pubDate>Wed, 03 Nov 2010 14:19:25 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[Apache Corporation]]></category>
		<category><![CDATA[Cache]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[Computer memory]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[CPU cache]]></category>
		<category><![CDATA[Cross-platform software]]></category>
		<category><![CDATA[database server]]></category>
		<category><![CDATA[Google Inc.]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[HTTP cookie]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[MySQL Americas Inc.]]></category>
		<category><![CDATA[Page cache]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[script interpreter]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[web app]]></category>
		<category><![CDATA[web application caching]]></category>
		<category><![CDATA[Web application frameworks]]></category>
		<category><![CDATA[web server]]></category>
		<category><![CDATA[Zend Technologies]]></category>

		<guid isPermaLink="false">/?p=2024</guid>
		<description><![CDATA[Zend_Cache_Frontend_Page First of all there are several things to know about Zend Framework and caching. Whenever you work on a big web application caching is one of the mostly used mechanisms of speeding up the app and improve user performance. In general the task and the solution are pretty simple and natural. As the application &#8230; <a href="/2010/11/03/how-to-overcome-zend_cache_frontend_pages-problem-with-cookies/" class="more-link">Continue reading <span class="screen-reader-text">How to Overcome Zend_Cache_Frontend_Page&#8217;s Problem with Cookies</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2011/01/17/a-memcached-zend_cache/" rel="bookmark" title="A Memcached Zend_Cache">A Memcached Zend_Cache </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/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/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>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Zend_Cache_Frontend_Page</h2>
<p>First of all there are several things to know about Zend Framework and caching. Whenever you work on a big web application caching is one of the mostly used mechanisms of speeding up the app and improve user performance. In general the task and the solution are pretty simple and natural.</p>
<p>As the application grows up the visitors become more and more impatient about what they receive. A single page is becoming slower and slower and the result is painful. First of all every time a user hits a page the application server uses the web server, a script interpreter, a database server and potentially the file system. But that&#8217;s not all. After all this output is generated on the server, as HTML in the most cases, it is sent to the client where again CSS and JavaScript engines parse and execute them.</p>
<p>In this scenario it&#8217;s easy to imagine how many time is spent. While there are several techniques to optimize the client side by optimizing JavaScript, CSS and the static images used for the design of the site, here I&#8217;m going to talk more about the backend.</p>
<h2>Beside the Optimization</h2>
<p>Let&#8217;s assume we&#8217;ve one of the very used combination between Apache (as a webserver), PHP (as server scripting language) and MySQL (as database server). Here you can choose to optimize all three of them. However beside the optimization of them one of the most simple steps you can do is to cache the output generated by these three branches of your web app.</p>
<h2>Caching the Content</h2>
<p>In fact you can cache only single parts of the whole process. For instance you can cache only the result returned by some slow database query. Let&#8217;s imagine a query takes about 2 seconds to execute. Now you can cache the result into a file and during the cache is active, i.e. it has not expired, the application takes it from a file stored somewhere in the file system.</p>
<p>In a typical Zend Framework scenario you can first setup the frontend and backend options of the cache.</p>
<pre lang="php">
$frontendOptions = array(
	'lifetime'                => 600, // in seconds - this is 10 seconds
	'automatic_serialization' => true,
);
$backendOptions = array('cache_dir' => 'cache/');
$cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
		
$cacheKey = md5('mykey');

if (!$cache->load($cacheKey)) {
	$slowQueryResult = $article->fetchAll();
	$cache->save($slowQueryResult, $cacheKey);
} else {
	$slowQueryResult = $cache->load($cacheKey);
}
</pre>
<p>You can setup different options here by setting up the cache directory, lifetime, etc.</p>
<p><em>Note that the cache directory must exists and with write permissions and Zend Framework doesn&#8217;t create it for you and will throw error.</em></p>
<p>The problem here is that now you cache only part of the generated content and in many cases this is still too slow for most of the users. However all the work (in most of the cases) of the web server, the script interpreter and the database server result in a simple HTML output. What if you have this output generated or cached for you and when the user hit the page the server will return this pre-generated code?</p>
<p>This is indeed very fast, because it&#8217;s similar to return a text file, as the HTML is simply formatted text.</p>
<h2>Caching the Entire Page</h2>
<p>Before I proceed, I&#8217;d like to say that I work with Zend Framework 1.9.x. Now in the latest versions of ZF there are new mechanisms of caching the output even Zend_Cache_Frontend_Page works fine on them.</p>
<p>You can simply setup the page cache within few simple lines of code:</p>
<pre lang="php">
$fo = array(
    'lifetime' => 600,
    'regexps' => array(
        '^/' => array(
	'cache' => true,
         'cache_with_cookie_variables' => true,
        ),
    )
);

$bo = array(
    'cache_dir' => 'cache/'
);

$cache = Zend_Cache::factory('Page', 'File', $fo, $bo);
$cache->start();
</pre>
<p>However my advise is to place this code as high as possible, because this will cache everything generated as output. It&#8217;s a good practice if you place this even in the bootstrap before you make the connection with the database. Actually you don&#8217;t need a database connection when you&#8217;ve to return a simple text(html) file.</p>
<p>This will improve your app&#8217;s performance a lot!</p>
<p>However there are few things to know. When you setup the cache to work even with cookie variables, you can see that hitting the page with different browsers Zend Framework will generated different cache pages. This is quite useless because than you don&#8217;t have any benefit of caching the content.</p>
<p>First of all let me say that THIS IS NOT A BUG! of ZF. Simply the framework will use the cookie variables to generate the cache key. It&#8217;s obvious that different browsers, even more different users, will have different cookie set and the framework will generate different cache keys for them.</p>
<p>Thus you&#8217;ve to change the setting to generate the cache key from cookie variable by explicitly set this option to false:</p>
<pre lang="php">
$fo = array(
    'lifetime' => 600,
    'regexps' => array(
        '^/' => array(
		 'cache' => true,
         'cache_with_cookie_variables' => true,
         'make_id_with_cookie_variables' => false,
        ),
    )
);

$bo = array(
    'cache_dir' => 'cache/'
);

$cache = Zend_Cache::factory('Page', 'File', $fo, $bo);
$cache->start();
</pre>
<p>Note that in this example we cache every single page generated by the framework explained in the regexps. This is not so good especially when the users have the possibility to login and to see customized content for them, so you can be careful what you cache.</p>
<p>A typical problem that this solution solves is when your application uses Google Analytics. As you may know Google Analytics sets up a cookie every time when an user hits the page, so every time the framework will generate a different cache for him and in result he won&#8217;t see any benefit and performance improvement from your site.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2011/01/17/a-memcached-zend_cache/" rel="bookmark" title="A Memcached Zend_Cache">A Memcached Zend_Cache </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/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/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>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/11/03/how-to-overcome-zend_cache_frontend_pages-problem-with-cookies/feed/</wfw:commentRss>
		<slash:comments>3</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>Zend_Validate_Db_RecordExists in Zend Framework 1.10+</title>
		<link>/2010/07/22/zend_validate_db_recordexists-in-zend-framework-1-10/</link>
		<comments>/2010/07/22/zend_validate_db_recordexists-in-zend-framework-1-10/#respond</comments>
		<pubDate>Thu, 22 Jul 2010 11:24:46 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[Column]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Data modeling]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Row]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[Web 2.0]]></category>
		<category><![CDATA[Web application frameworks]]></category>

		<guid isPermaLink="false">/?p=1809</guid>
		<description><![CDATA[Zend_Validate_Db_RecodExists is an extremely useful validator in Zend Framework when you&#8217;d like to be sure that a give row exists. Now it seems to be even better. Before you could check for a specific row by only comparing a value to the specified column: $validator = new Zend_Validate_Db_RecordExists('db_table_name', 'column_name'); if ($validator->isValid(122)) { ... } which &#8230; <a href="/2010/07/22/zend_validate_db_recordexists-in-zend-framework-1-10/" class="more-link">Continue reading <span class="screen-reader-text">Zend_Validate_Db_RecordExists in Zend Framework 1.10+</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/01/25/using-zend-framework-validators-zend_validate_db_recordexists/" rel="bookmark" title="Using Zend Framework validators &#8211; Zend_Validate_Db_RecordExists">Using Zend Framework validators &#8211; Zend_Validate_Db_RecordExists </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>
<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>
<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>Zend_Validate_Db_RecodExists is an extremely useful validator in Zend Framework when you&#8217;d like to be sure that a give row exists. Now it seems to be even better. Before you could check for a specific row by only comparing a value to the specified column:</p>
<pre lang="php">
$validator = new Zend_Validate_Db_RecordExists('db_table_name', 'column_name');
if ($validator->isValid(122)) {
   ...
}
</pre>
<p>which made it useless when you&#8217;d like to compare by more than one column. Now this is changed and you can even exclude given rows by adding an exclude clause. </p>
<pre lang="php">
$validator = new Zend_Validate_Db_NoRecordExists(
    array(
        'table' => 'users',
        'field' => 'username',
        'exclude' => array(
            'field' => 'id',
            'value' => $user_id
        )
    )
);
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/01/25/using-zend-framework-validators-zend_validate_db_recordexists/" rel="bookmark" title="Using Zend Framework validators &#8211; Zend_Validate_Db_RecordExists">Using Zend Framework validators &#8211; Zend_Validate_Db_RecordExists </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>
<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>
<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/07/22/zend_validate_db_recordexists-in-zend-framework-1-10/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>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>Bind Zend Action with Non-Default View &#8211; Part 2</title>
		<link>/2010/06/23/bind-zend-action-with-non-default-view-part-2/</link>
		<comments>/2010/06/23/bind-zend-action-with-non-default-view-part-2/#comments</comments>
		<pubDate>Wed, 23 Jun 2010 14:26:41 +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[html]]></category>
		<category><![CDATA[HTML element]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Web 2.0]]></category>
		<category><![CDATA[Web application frameworks]]></category>
		<category><![CDATA[World Wide Web]]></category>
		<category><![CDATA[zend]]></category>
		<category><![CDATA[zend view renderer]]></category>

		<guid isPermaLink="false">/?p=1664</guid>
		<description><![CDATA[Typical Setup Typically Zend Framework is setup to bind every controller&#8217;s action to a specific view. The names of the action and the view script must be the same, or at least must be similar &#8211; following the ZF&#8217;s convention. Thus if you name an action, let&#8217;s say, indexAction(), you&#8217;ve to create an index.phtml file &#8230; <a href="/2010/06/23/bind-zend-action-with-non-default-view-part-2/" class="more-link">Continue reading <span class="screen-reader-text">Bind Zend Action with Non-Default View &#8211; Part 2</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<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/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/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/04/27/inline-scripts-with-zend_view_helper_inlinescript/" rel="bookmark" title="Inline Scripts with Zend_View_Helper_InlineScript">Inline Scripts with Zend_View_Helper_InlineScript </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Typical Setup</h2>
<p>Typically <a href="http://framework.zend.com/">Zend Framework</a> is setup to bind every controller&#8217;s action to a specific view. The names of the action and the view script must be the same, or at least must be similar &#8211; following the ZF&#8217;s convention.</p>
<p>Thus if you name an action, let&#8217;s say, indexAction(), you&#8217;ve to create an index.phtml file into the views/scripts/index/ folder. Let me remind you that this is what is called a typical setup, but this may vary from one installation to another.</p>
<p>Thus everything&#8217;s fine and the MVC does its best. After the user requests a specific uri, the framework parses it and finds the controller, than the action and than the view script &#8211; and there is a view script for every single action.</p>
<p><a href="/wp-content/uploads/2010/06/typical-zend-framework-setup.jpg"><img class="alignleft size-medium wp-image-1675" title="typical-zend-framework-setup" src="/wp-content/uploads/2010/06/typical-zend-framework-setup-300x162.jpg" alt="typical zend framework setup" width="300" height="162" srcset="/wp-content/uploads/2010/06/typical-zend-framework-setup-300x162.jpg 300w, /wp-content/uploads/2010/06/typical-zend-framework-setup.jpg 545w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>When there are two or more actions &#8211; there are two or more .phtml (view scripts) files.</p>
<p><a href="/wp-content/uploads/2010/06/typical-zend-framework-setup2.jpg"><img class="alignleft size-medium wp-image-1676" title="typical-zend-framework-setup2" src="/wp-content/uploads/2010/06/typical-zend-framework-setup2-300x203.jpg" alt="typical zend framework setup for two or more actions" width="300" height="203" srcset="/wp-content/uploads/2010/06/typical-zend-framework-setup2-300x203.jpg 300w, /wp-content/uploads/2010/06/typical-zend-framework-setup2.jpg 630w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<h2>Some Exceptions</h2>
<p>Sometimes you have very similar logic, but very different markup between two actions. As the logic goes to the controller/action and the markup into the view, the question is can you simply have one action in the controller, but with two different .phtml file as a view scripts?  Of course switching between those two (or more) with some conditionals.</p>
<h3>Solution No. 1</h3>
<p>However once I posted a simple, and working, <a href="/2010/06/07/bind-zend-action-with-non-default-view/">solution</a>. There you have again two actions and two scripts:</p>
<pre lang="php">
<?php

class IndexController extends Zend_Controller_Action
{	
	public function firstAction()
	{
		// place all the logic you need here
		// and every view placeholder assignment
		$this->view->title = "My Title";
		
		// if the logic requires a different markup
		// simply redirect to another view
		if (some_expression) {
			$this->_forward('second');	
		}	
	}
	
	public function secondAction()
	{}
}

// in the application/views/scripts/index
// 1. first.phtml
<h1><?php echo $this->title ?></h1>

// 2. second.phtml
<h2><?php echo $this->title ?></h2>
</pre>
<p>As you can see in the second action there&#8217;s no logic &#8211; it&#8217;s only serving the role of a bridge between the views. Everything is setup into the first action, but the second action is doing the relation with the second.phtml view script.</p>
<h3>Solution No. 2</h3>
<p>However there&#8217;s another solution with only one action and two scripts which makes something like that shown on the diagram bellow:</p>
<p><a href="/wp-content/uploads/2010/06/zend-framework-one-action-multiple-views.jpg"><img class="alignleft size-medium wp-image-1677" title="zend-framework-one-action-multiple-views" src="/wp-content/uploads/2010/06/zend-framework-one-action-multiple-views-300x203.jpg" alt="zend framework one action multiple=" height="203" srcset="/wp-content/uploads/2010/06/zend-framework-one-action-multiple-views-300x203.jpg 300w, /wp-content/uploads/2010/06/zend-framework-one-action-multiple-views.jpg 630w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>The only thing to do is to add some code to your action:</p>
<pre lang="php">
<?php

class IndexController extends Zend_Controller_Action
{	
	public function firstAction()
	{
		// place all the logic you need here
		// and every view placeholder assignment
		$this->view->title = "My Title";
		
		// if the logic requires a different markup
		// simply redirect to another view
		if (some_expression) {
			echo $this->view->render('index/second.phtml');
			$this->_helper->viewRenderer->setNoRender(true);
		}	
	}
}

// in the application/views/scripts/index
// 1. first.phtml
<h1><?php echo $this->title ?></h1>

// 2. second.phtml
<h2><?php echo $this->title ?></h2>
</pre>
<p>Note that you&#8217;ve to setNoRender on the view renderer once you render the other script!</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<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/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/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/04/27/inline-scripts-with-zend_view_helper_inlinescript/" rel="bookmark" title="Inline Scripts with Zend_View_Helper_InlineScript">Inline Scripts with Zend_View_Helper_InlineScript </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/06/23/bind-zend-action-with-non-default-view-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Redirect with Zend Framework</title>
		<link>/2010/06/21/redirect-with-zend-framework/</link>
		<comments>/2010/06/21/redirect-with-zend-framework/#comments</comments>
		<pubDate>Mon, 21 Jun 2010 14:31:10 +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[PHP programming language]]></category>
		<category><![CDATA[redirect]]></category>
		<category><![CDATA[referer]]></category>
		<category><![CDATA[URL]]></category>
		<category><![CDATA[Web application frameworks]]></category>
		<category><![CDATA[World Wide Web]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">/?p=1652</guid>
		<description><![CDATA[Once I wrote about redirecting with Zend Framework, but what I missed back than was a common mistake. Although the code from my post is working, to be completely correct after redirecting I&#8217;d to add an exit() statement. This is important because the header is changed and if something goes after the redirect there will &#8230; <a href="/2010/06/21/redirect-with-zend-framework/" class="more-link">Continue reading <span class="screen-reader-text">Redirect with Zend Framework</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2009/11/27/redirecting-with-zend-framework/" rel="bookmark" title="Redirecting with Zend Framework">Redirecting with Zend Framework </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="/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>
<li><a href="/2010/07/22/zend_validate_db_recordexists-in-zend-framework-1-10/" rel="bookmark" title="Zend_Validate_Db_RecordExists in Zend Framework 1.10+">Zend_Validate_Db_RecordExists in Zend Framework 1.10+ </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>Once I <a title="_redirect with Zend Framework" href="/2010/04/21/setting-a-zend-framework-_redirect-referer/" target="_blank">wrote</a> about redirecting with Zend Framework, but what I missed back than was a common mistake. Although the code from my post is working, to be completely correct after redirecting I&#8217;d to add an exit() statement.</p>
<p>This is important because the header is changed and if something goes after the redirect there will be some problems. Finally the correct snippet is:</p>
<pre lang="php">
public function() {
	$this->_redirect('some_url');
	exit();	
}
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2009/11/27/redirecting-with-zend-framework/" rel="bookmark" title="Redirecting with Zend Framework">Redirecting with Zend Framework </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="/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>
<li><a href="/2010/07/22/zend_validate_db_recordexists-in-zend-framework-1-10/" rel="bookmark" title="Zend_Validate_Db_RecordExists in Zend Framework 1.10+">Zend_Validate_Db_RecordExists in Zend Framework 1.10+ </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/06/21/redirect-with-zend-framework/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JSON and Zend Framework? &#8211; Zend_Json</title>
		<link>/2010/06/10/json-and-zend-framework-zend_json/</link>
		<comments>/2010/06/10/json-and-zend-framework-zend_json/#respond</comments>
		<pubDate>Thu, 10 Jun 2010 14:08: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[C]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Curly bracket programming languages]]></category>
		<category><![CDATA[Human Interest]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Procedural programming languages]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[Web 2.0]]></category>
		<category><![CDATA[Web application frameworks]]></category>
		<category><![CDATA[Whitespace]]></category>

		<guid isPermaLink="false">/?p=1603</guid>
		<description><![CDATA[That&#8217;s really a good Zend Framework&#8217;s class that help you do the encode/decode job very easily. First of all it escapes everything for you and second it prints a correct/valid code. Note that sometimes if you have a trailing whitespace after the closing PHP tag &#8211; ?&#62; that will result in an error. Here&#8217;s some &#8230; <a href="/2010/06/10/json-and-zend-framework-zend_json/" class="more-link">Continue reading <span class="screen-reader-text">JSON and Zend Framework? &#8211; Zend_Json</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/08/13/returning-json-in-a-zend-controllers-action/" rel="bookmark" title="Returning JSON in a Zend Controller&#8217;s Action">Returning JSON in a Zend Controller&#8217;s Action </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>
<li><a href="/2010/04/09/secure-forms-with-zend-framework/" rel="bookmark" title="Secure Forms with Zend Framework">Secure Forms with Zend Framework </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>That&#8217;s really a good Zend Framework&#8217;s class that help you do the encode/decode job very easily. First of all it escapes everything for you and second it prints a correct/valid code. Note that sometimes if you have a trailing whitespace after the closing PHP tag &#8211; ?&gt; that will result in an error.</p>
<p>Here&#8217;s some code:</p>
<pre lang="php">
public function jsonAction()
{
	$data = array(3,4,'test', 'my-name' => 3,4);

	echo Zend_Json::encode($data);

	$this->_helper->viewRenderer->setNoRender(true);
	$this->view->layout()->disableLayout();
}
</pre>
<p>and the result is:</p>
<pre lang="html4strict">
{"0":3,"1":4,"2":"test","my-name":3,"3":4}
</pre>
<p>Note that all integers are printed without double quotes &#8211; which saves some space!</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/08/13/returning-json-in-a-zend-controllers-action/" rel="bookmark" title="Returning JSON in a Zend Controller&#8217;s Action">Returning JSON in a Zend Controller&#8217;s Action </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>
<li><a href="/2010/04/09/secure-forms-with-zend-framework/" rel="bookmark" title="Secure Forms with Zend Framework">Secure Forms with Zend Framework </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/06/10/json-and-zend-framework-zend_json/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Download Files with Zend Framework</title>
		<link>/2010/05/25/download-files-with-zend-framework/</link>
		<comments>/2010/05/25/download-files-with-zend-framework/#comments</comments>
		<pubDate>Tue, 25 May 2010 14:06:13 +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 file formats]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Graphics file formats]]></category>
		<category><![CDATA[jpeg]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Social Issues]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[Web application frameworks]]></category>

		<guid isPermaLink="false">/?p=1571</guid>
		<description><![CDATA[Download a File The title may sound unclear enough, but the task is simple. You&#8217;ve to make a file download process within a Zend Framework&#8217;s controller. Let&#8217;s assume we&#8217;ve the DownloadController setup:<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/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>
<li><a href="/2010/04/07/upload-a-file-with-zend-framework/" rel="bookmark" title="Upload a File with Zend Framework">Upload a File with Zend Framework </a></li>
<li><a href="/2011/01/18/download-images-with-php/" rel="bookmark" title="Download Images with PHP">Download Images with PHP </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Download a File</h2>
<p>The title may sound unclear enough, but the task is simple. You&#8217;ve to make a file download process within a Zend Framework&#8217;s controller. Let&#8217;s assume we&#8217;ve the DownloadController setup:</p>
<pre lang="php">
<?php

class DownloadController extends Zend_Controller_Action
{
	public function indexAction()
	{}	
}
</pre>
<p>In PHP there are at least three simple lines of code that will do the job of downloading a file.</p>
<pre lang="php">
header('Content-Type: image/jpeg');
header('Content-Disposition: attachment; filename="logo.jpg"');
readfile('images/logo.jpg');
</pre>
<p>Note that here there is a content-type header, which is important cause the browsers understands what kind of file is supposed to be downloaded. The second line suggests a name of the downloaded file and the third one returns the file to the client.</p>
<h2>Download a File ... within Zend Framework</h2>
<p>Those three lines wont work alone in a ZF application, because there's no view, but even if you create a .phtml (view) file for this action it won't work, because the header of the returned file is modified.</p>
<p>The question is how to possibly return the file for download, perhaps write some statistics to the database and if there's a problem (some permission issues for instance) return a message to the user.</p>
<h2>The Basic Solution</h2>
<p>The solution is simple. First make it work by disabling the view and possibly the layout for this action:</p>
<pre lang="php">
public function indexAction()
{
	header('Content-Type: image/jpeg');
	header('Content-Disposition: attachment; filename="logo.jpg"');
	readfile('images/logo.jpg');
	
	// disable the view ... and perhaps the layout
	$this->view->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
}
</pre>
<p>Than add some code where you can check the permissions. Just because there's no view for this action you can redirect to another - errorAction():</p>
<pre lang="php">
public function indexAction()
{
    if (userHasNoPermissions) {
        $this->view->msg = 'This file cannot be downloaded!';
        $this->_forward('error', 'download');
    }

    header('Content-Type: image/jpeg');
    header('Content-Disposition: attachment; filename="logo.jpg"');
    readfile('images/logo.jpg');

    // disable layout and view
    $this->view->layout()->disableLayout();
    $this->_helper->viewRenderer->setNoRender(true);
}
</pre>
<p>But that still will prompt you a file to download, so there should be a return statement that will return false:</p>
<pre lang="php">
public function indexAction()
{
    if (userHasNoPermissions) {
        $this->view->msg = 'This file cannot be downloaded!';
        $this->_forward('error', 'download');
        return FALSE;
    }

    header('Content-Type: image/jpeg');
    header('Content-Disposition: attachment; filename="logo.jpg"');
    readfile('images/logo.jpg');

    // disable layout and view
    $this->view->layout()->disableLayout();
    $this->_helper->viewRenderer->setNoRender(true);
}
</pre>
<p>So here's the complete source of DownloadController.php:</p>
<pre lang="php">
<?php

class DownloadController extends Zend_Controller_Action
{
	public function indexAction()
	{
	    if (userHasNoPermissions) {
	        $this->view->msg = 'This file cannot be downloaded!';
	        $this->_forward('error', 'download');
	        return FALSE;
	    }
	
	    header('Content-Type: image/jpeg');
	    header('Content-Disposition: attachment; filename="logo.jpg"');
	    readfile('images/logo.jpg');
	
	    // disable layout and view
	    $this->view->layout()->disableLayout();
	    $this->_helper->viewRenderer->setNoRender(true);
	}	
	
	public function errorAction()
	{}
}
</pre>
<p>and the error.phtml:</p>
<pre lang="php">
<?php echo $this->msg ?>
</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/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>
<li><a href="/2010/04/07/upload-a-file-with-zend-framework/" rel="bookmark" title="Upload a File with Zend Framework">Upload a File with Zend Framework </a></li>
<li><a href="/2011/01/18/download-images-with-php/" rel="bookmark" title="Download Images with PHP">Download Images with PHP </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/05/25/download-files-with-zend-framework/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Check YouTube Video Existence with Zend_Gdata_YouTube</title>
		<link>/2010/05/21/check-youtube-video-existence-with-zend_gdata_youtube/</link>
		<comments>/2010/05/21/check-youtube-video-existence-with-zend_gdata_youtube/#respond</comments>
		<pubDate>Fri, 21 May 2010 07:43:57 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[e->]]></category>
		<category><![CDATA[Entertainment/Culture]]></category>
		<category><![CDATA[GData]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[Hypertext Transfer Protocol]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[Web 2.0]]></category>
		<category><![CDATA[Web application frameworks]]></category>
		<category><![CDATA[World Wide Web]]></category>
		<category><![CDATA[YouTube]]></category>
		<category><![CDATA[YouTube Inc]]></category>

		<guid isPermaLink="false">/?p=1531</guid>
		<description><![CDATA[The Video Has Been Removed?! Have you ever seen the famous message on YouTube: &#8220;This video has been removed by the user.&#8221;, but what if you&#8217;re trying to grab that video via the Zend_Gdata_YouTube? That can happen when you&#8217;re reading the feed for a channel or a user&#8217;s list of videos. In the feed everything&#8217;s &#8230; <a href="/2010/05/21/check-youtube-video-existence-with-zend_gdata_youtube/" class="more-link">Continue reading <span class="screen-reader-text">Check YouTube Video Existence with Zend_Gdata_YouTube</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/03/16/retrieving-youtube-channels-videos-with-zend_gdata_youtube/" rel="bookmark" title="Retrieving YouTube Channel&#8217;s Videos with Zend_Gdata_YouTube">Retrieving YouTube Channel&#8217;s Videos with Zend_Gdata_YouTube </a></li>
<li><a href="/2010/04/22/iterate-over-youtube-channel-with-zend_gdata_youtube/" rel="bookmark" title="Iterate over YouTube Channel with Zend_Gdata_YouTube">Iterate over YouTube Channel with Zend_Gdata_YouTube </a></li>
<li><a href="/2010/01/23/youtube-and-vimeo-goes-html5-with-video-tag-usage-catch-them-up/" rel="bookmark" title="YouTube and Vimeo goes HTML5 with video tag usage! Catch them up!">YouTube and Vimeo goes HTML5 with video tag usage! Catch them up! </a></li>
<li><a href="/2011/04/07/use-fopen-to-check-file-availability/" rel="bookmark" title="Use fopen() to Check File Availability?">Use fopen() to Check File Availability? </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>The Video Has Been Removed?!</h2>
<p>Have you ever seen the famous message on <a href="http://youtube.com/">YouTube</a>: &#8220;This video has been removed by the user.&#8221;, but what if you&#8217;re trying to grab that video via the <a href="http://framework.zend.com/manual/en/zend.gdata.youtube.html" target="_blank">Zend_Gdata_YouTube</a>?</p>
<p>That can happen when you&#8217;re reading the feed for a channel or a user&#8217;s list of videos. In the feed everything&#8217;s OK &#8211; once the video is uploaded it appears into the feed. After that the user removes the video and visiting the link from the feed you&#8217;ll get the message above.</p>
<p>Actually what happens when you try to read this &#8220;video entry&#8221; within a Zend Framework&#8217;s application. There must be some way to catch this message?</p>
<h2>Trying to Catch</h2>
<p>The answer is pretty simple! Zend_Gdata_YouTube&#8217;s throwing an exception and it can be easily cached. Here&#8217;s some source code:</p>
<pre lang="php">
$apiKey = 'your_api_key';
$client = new Zend_Http_Client();
$gdata = new Zend_Gdata_YouTube($client, 'my-app', null, $apiKey);
try {
       $gdata->getVideoEntry($video_id);
} catch(Zend_Gdata_App_Exception $e) {
       echo $e->getMessage();
}
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/03/16/retrieving-youtube-channels-videos-with-zend_gdata_youtube/" rel="bookmark" title="Retrieving YouTube Channel&#8217;s Videos with Zend_Gdata_YouTube">Retrieving YouTube Channel&#8217;s Videos with Zend_Gdata_YouTube </a></li>
<li><a href="/2010/04/22/iterate-over-youtube-channel-with-zend_gdata_youtube/" rel="bookmark" title="Iterate over YouTube Channel with Zend_Gdata_YouTube">Iterate over YouTube Channel with Zend_Gdata_YouTube </a></li>
<li><a href="/2010/01/23/youtube-and-vimeo-goes-html5-with-video-tag-usage-catch-them-up/" rel="bookmark" title="YouTube and Vimeo goes HTML5 with video tag usage! Catch them up!">YouTube and Vimeo goes HTML5 with video tag usage! Catch them up! </a></li>
<li><a href="/2011/04/07/use-fopen-to-check-file-availability/" rel="bookmark" title="Use fopen() to Check File Availability?">Use fopen() to Check File Availability? </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/05/21/check-youtube-video-existence-with-zend_gdata_youtube/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
