Zend Framework Tutorial: Version 1.1

It turns out that there are hardly any changes required to make the tutorial code compatible with version 0.2 of the Zend Framework! The only changes I had to make were to the index.php file.

The two changes needed were:

Zend_Config construction
You now create a Zend_Config_Ini directly:

$config = new Zend_Config_Ini('./application/config.ini''general');

is all that's needed now!

Front Controller instantiation
For the original tutorial, I needed a special route for compatibility. This is now included by default in 0.2, so the work needed to be done is less. However, I've been playing with setting the rewrite base, and may have a better solution now. The new code for this bit is now:

$router = new Zend_Controller_RewriteRouter();
$baseUrl substr($_SERVER['PHP_SELF'], 0, 
        strpos($_SERVER['PHP_SELF'], '/index.php'));
$router->setRewriteBase($baseUrl);
$controller Zend_Controller_Front::getInstance();
$controller->setRouter($router);

I think it's more likely to work on more configurations than my previous code. If anyone can see a security hole in this particular use of PHP_SELF, please shout though!

The new tutorial (version 1.1.0 - this time I'm prepared for point releases!) is available in the usual place. As always, comments and bug fixes are very welcome!

6 Responses to “Zend Framework Tutorial: Version 1.1”

  1. 1 Pádraic Brady

    Excellent stuff.

    Why not use SCRIPT_NAME or rather SCRIPT_FILENAME to find the rewrite base? Avoid using PHP_SELF at all…

  2. 2 Rob...

    Hi,

    Mainly because I wasn't sure which version of Apache using which SAPIs generate SCRIPT_NAME/SCRIPT_FILENAME. Whereas PHP_SELF is generated by php, so I was pretty sure that it'd work everywhere.

    Regards,

    Rob…

  3. 3 Pádraic Brady

    Well, I don't know which ones do either. I won't question your wisdom then ;).

    I pinged the tutorial to http://www.planet-php.net via my own blog, honestly you should register your blog there: http://www.planet-php.net/submit/. Two days is too long to wait for my irregular blog checkup :).

    Keep up the great work.

  4. 4 Waldemar Schott

    Hi,
    I have another possibility:

    $url = explode('index.php', $_SERVER['SCRIPT_NAME']);
    $baseUrl = $url[0];

    That works for my "domain.tld/index.php/controller/action/" URLs.

    Waldemar

  5. 5 Rob...

    Waldemar,

    I would still need to find out if it's possible for $_SERVER['SCRIPT_NAME'] to ever not be present (or contain the wrong information.

    Regards,

    Rob…

  6. 6 Rob...

    Thanks Pádraic!

    Regards,

    Rob…

The views expressed in these comments are not the views of the publisher. However, we believe in the rights of others to express their legitimate views and concerns. Any legitimate complaint emailed to rob@akrabat.com will be seriously considered and the post reviewed as desirable and necessary.

Leave a Reply

Pre order