I had to take a little hiatus the last few weeks, however I’m hoping to get back into a weekly posting schedule.
This video is an introduction in how to effectively use Zend Controller Plugins. The Zend Documentation refers to Zend Controller plugins being part of the Action Stack which is a simple data structure that can be filled with a bunch of plugins which will run in succession.
We’ll create a simple asset protection script using the Zend Framework’s built in Zend Controller Plugin architecture.
Grab a copy of the project or browse the repository.

+10!!! awesome! very good screencasts!! thx
Absolutly Great!
Thanks!! great screencasts
AssetGrabber – genius!
To make it work with Zend_Navigation I used this code:
frontController = Zend_Controller_Front::getInstance();
$router = $this->frontController->getRouter();
$language = $request->getParam(‘language’,”);
if($language !== ‘en’ && $language !== ‘nl’ && $language !== ‘de’ && $language !== ‘fr’ && $language !== ‘be’)
{
$request->setParam(‘language’,'en’);
}
$language = $request->getParam(‘language’);
switch($language)
{
case ‘en’:
$locale = ‘en_US’;
break;
case ‘nl’:
$locale = ‘nl_NL’;
break;
case ‘de’:
$locale = ‘de_DE’;
break;
case ‘fr’:
$locale = ‘fr_FR’;
break;
case ‘be’:
$locale = ‘nl_BE’;
break;
}
$setLanguage = new Zend_Locale();
$setLanguage->setLocale($locale);
Zend_Registry::set(‘Zend_Locale’, $setLanguage);
$translate = new Zend_Translate(‘gettext’, APPLICATION_PATH . ‘/languages/’. $locale . ‘.mo’ , $locale);
Zend_Registry::set(‘Zend_Translate’, $translate);
$router->removeDefaultRoutes();
$router->addRoute(
‘languagecontroller’,
new Zend_Controller_Router_Route(‘/:language/:controller/:action’,
array(‘language’ => $language,
‘controller’ => ‘index’,
‘action’ => ‘index’
)
)
);
}
}
I couldn’t get the parameter in the boostrap, I figured if i set the parameter again in the bootstrap it would be nicer, but getParam(‘language’) keeps returning NULL.
Completly sorry! Posted on the wrong cast!!
Awesome lessons! They are all very helpful, clear and easy to understand even though I’m not good at English
Hi, Jon.
I am wondering how you recommend to handle different content types with this approach? The problem here is that you always get the text/html content type unless you set your headers manually which you cannot do since the actual rewritten assets may be of any type.
Also a note on using an exit statement in the plugin – it is better not to use exit and use $this->getResponse()->sendResponse() instead. This will allow you to unit test your plugins normally without breaking the whole test suite.
@Dmitry
Thank you for mentioning the exit; statement. Quite right that $this->getResponse()->sendResponse() is more testable. Hopefully, the web service or filesystem you’re using to distribute the content will provide the necessary headers.
@Jon:
>Hopefully, the web service or filesystem you’re using to distribute the content will
> provide the necessary headers.
Unfortunately in doesn’t. So in search for a solution I did small method that does mime type detection with PHP 5.3 FIleinfo extension if available or otherwise falls back to now depricated mime_content_type() method. So here it goes:
/**
* Get mime-type for a file
* Attempts to get mime type for a given file if it’s possible with
* the required functions are available and file exists.
*
* @param string $filePath
* @return string
*/
public function getMimeType($filePath)
{
//Default value
$mimeType = null;
//If Fileinfo is available
if(function_exists(‘finfo_file’) && function_exists(‘finfo_open’))
{
$handle = finfo_open(FILEINFO_MIME_TYPE);
$mimeType = finfo_file($handle, $filePath);
finfo_close($handle);
}
//Fall back to depricated mime_content_type
elseif(function_exists(mime_content_type))
$mimeType = mime_content_type($filePath);
return strtolower($mimeType);
}
Autoloblbblbl … hilarious. Love your screencasts. Very nice lecturing. Good and easy to understand presentations.
thanks alot.
How might one go about uploading an image via a form to the /assets/ folder? Would there need to be anything special during the upload?
Hi!
Nice approach, not bad.
I did just do it a bit differently. Imagine you have a lot of images/photos, let’s say you have a gallery where all your images have to be protected or verified for group/user rights.
If you load the ZF for every photo this has a negative impact on the performance.
But nevertheless the approach using a plugin like this good.
Nice cast.
Christian
I’m learning a lot from you. Thanks for everything Jon!!!