Category Archives: zend framework

Extending the art & spirit of PHP, Zend Framework is based on simplicity, object-oriented best practices, corporate friendly licensing, and a rigorously tested agile codebase. Zend Framework is focused on building more secure, reliable, and modern Web 2.0 applications & web services, and consuming widely available APIs from leading vendors like Google, Amazon, Yahoo!, Flickr, as well as API providers and cataloguers like StrikeIron and ProgrammableWeb.

HTTP_REFERRER in Zend Framework

What is HTTP_REFERRER?

If you need to know how called a PHP script, it is normally setup in the $_SERVER variable of HTTP_REFERRER, or simply

$_SERVER['HTTP_REFERRER'];

but in the same time this does not work the same way in Zend Framework, just because everything is passing by the index.php or a custom bootstrap!

Unfortunately there’s no so many resources online, however there is a solution, which seems to be pretty natural. You can just use this chuck of code:

$this->getRequest()->getHeader('REFERER');

and that’s all!

Strict types in Zend Framework and in PHP

As you may know you can add strict types for a function parameters in PHP i.e.

public function func1(string $test)
{}

and this may be some simple function in some Zend Framework standard model! Now the problem comes with the instance of this function. If you call it like this:

func1('foo bar');

in a ZF controller, you simply will get an error.

It is strange …

isn’t it! But in PHP you can use strict types only for an array, which is built in type. In other words if some object implements the __toString() method than you can use string strict type.

Zend_Layout : using placeholders

Supposing that you’re familiar with Zend_Layout and why and where you should use it, here’s a little tutorial how to use placeholders.

If there’s a layout file called main.phtml where beside the part of:

<?php echo $this->layout()->content; ?>

there’s a need for a placeholder.

The placeholder is the place in your layout file, in this case in main.phtml, where you’d like to inject a part of your view script.

In a standard way you’ve some script file like index.phtml of the index controller. In Zend as you may know this is the default view of the default controller, and in that controller contains the simple code like this:

$this->_helper->layout()->title = 'browser title';

and than you can be sure this browser title can appear in the layout file with adding something really similar to the line above.

Just add this:

<title><?php echo $this->layout()->title; ?></title>

And now that’s enough to setup a variable in a Zend controller and to call that variable in the layout script.

Zend Framework – quick tutorial (part 3) – front controller plugins

Why writing a front controller plugin?

Almost every application uses a database connection and acl module. Why doing this in the bootstrap and to mantain many lines of code there, instead of making it clear and mantainable. Of course you can have all these lines of code in your bootstrap, but you know for serious applications that recently will become an obstacle. That’s why Zend Framework allows you to use Front_Controller plugin.

First in you bootstrap add those lines of code

/*
 * add a simple plugin to the controller
 */
 
$front->registerPlugin(new Zend_Controller_Plugin_Init())
      ->registerPlugin(new Zend_Controller_Plugin_Acl());

These two classes (Zend_Controller_Plugin_Init and Zend_Controller_Plugin_Acl) should be placed in two different files with the same names in Controller/Plugins directory under Zend folder, and garantee you that both classes will be instanciated before starting the front controller. Continue reading Zend Framework – quick tutorial (part 3) – front controller plugins