Robert Basic's blog

Posts tagged 'framework'

Loading custom module plugins

by Robert Basic on July 20, 2010.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

OK, here’s a quicky one from the office :P

I was trying to load a Front Controller plugin which resides in app/modules/my_module/controllers/plugins/ and not in the “usual” lib/My_App/Plugin/. I want this plugin to be called in every request and I want the plugin file to be under it’s “parent” module.

Here’s what I did: added the path to the plugin and it’s namespace to the Zend_Application_Module_Autoloader as a new resource type and then just register the plugin in the front controller in an other _init method.

Code is better, here’s some:

<?php
class News_Bootstrap extends Zend_Application_Module_Bootstrap
{
    /**
     * Autoloader for the "news" module
     *
     * @return Zend_Application_Module_Autoloader
     */
    public function _initNewsAutoload()
    {
        $moduleLoader = new Zend_Application_Module_Autoloader(
                                array(
                                    'namespace' => 'News',
                                    'basePath' => APPLICATION_PATH . '/modules/news'
                                )
                            );

        // adding model resources to the autoloader
        $moduleLoader->addResourceTypes(
                array(
                    'plugins' => array(
                        'path' => 'controllers/plugins',
                        'namespace' => 'Controller_Plugin'
                    )
                )
            );

        return $moduleLoader;
    }

    public function _initPlugins()
    {
        $this->bootstrap('frontcontroller');
        $fc = $this->getResource('frontcontroller');

        $fc->registerPlugin(new News_Controller_Plugin_Scheduler());
    }
}

If anyone knows a better way for doing this, please do share it with me.

Now back to work. Cheerio.

Tags: framework, loading, php, plugin, zend, zf.
Categories: Development, Programming.

Need help on your PHP projects? Let's talk!

Honeypot for Zend Framework

by Robert Basic on April 21, 2010.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

I just hacked up a little code snippet based on Matthew’s Honeypot Wordpress plugin. It’s basically just a Validator for a Zend Form element which is hidden from the user via CSS. Cause it’s hidden, users won’t see it, but spambots will, well, cause they are bots.

If the element is left empty, it’s valid, otherwise it’s not.

So, here’s the code:

<?php
class App_Validate_Honeypot extends Zend_Validate_Abstract
{
    const SPAM = 'spam';

    protected $_messageTemplates = array(
        self::SPAM => "I think you're a spambot. Sorry."
    );

    public function isValid($value, $context=null)
    {
        $value = (string)$value;
        $this->_setValue($value);

        if(is_string($value) and $value == ''){
            return true;
        }

        $this->_error(self::SPAM);
        return false;
    }
}

I add the element to the form like this:

<?php
$this->addElement(
    'text',
    'honeypot',
    array(
        'label' => 'Honeypot',
        'required' => false,
        'class' => 'honeypot',
        'decorators' => array('ViewHelper'),
        'validators' => array(
            array(
                'validator' => 'Honeypot'
            )
        )
    )
);

There. Done.

Happy hackin’!

Tags: framework, honeypot, php, validator, zend, zf.
Categories: Development, Programming.

Need help on your PHP projects? Let's talk!

Chaining routes in Zend Framework

by Robert Basic on November 27, 2009.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

On a forum, there was a question today, about adding language “support” to the routes using Zend Framework. The guy wanted routes like /en/foo/bar or /de/baz. I wrote there an example for that using Zend_Router_Routes_Chain, so just posting that example here, too :)

For what chains are for, is described in the manual, so I won’t be covering that :P

Basically, we’re prepending the language route to the other routes. This way, we have defined the route for the languages in one place only, plus, the other routes don’t have to worry about the language, too.

// this goes in the bootstrap class
<?php
public function _initRoutes()
{
    $this->bootstrap('FrontController');
    $this->_frontController = $this->getResource('FrontController');
    $router = $this->_frontController->getRouter();

    $langRoute = new Zend_Controller_Router_Route(
        ':lang/',
        array(
            'lang' => 'en'
        )
    );
    $contactRoute = new Zend_Controller_Router_Route_Static(
        'contact',
        array('controller'=>'index', 'action'=>'contact')
    );
    $defaultRoute = new Zend_Controller_Router_Route(
        ':controller/:action',
        array(
            'module'=>'default',
            'controller'=>'index',
            'action'=>'index'
        )
    );

    $contactRoute = $langRoute->chain($contactRoute);
    $defaultRoute = $langRoute->chain($defaultRoute);

    $router->addRoute('langRoute', $langRoute);
    $router->addRoute('defaultRoute', $defaultRoute);
    $router->addRoute('contactRoute', $contactRoute);
}

Assuming that we have an Index controller, with actions index and contact and a Foo controller with actions index and bar, paired with the routes from the above example, we could do requests like:

/ => /index/index/lang/en
/de => /index/index/lang/de
/sr/contact => /index/contact/lang/sr
/en/foo => /foo/index/lang/en
/fr/foo/bar => /foo/bar/lang/fr

Requesting a page like, e.g. /de/baz, would give us a 404 page, cause we don’t have a Baz controller.

HTH :)

Happy hacking!

Tags: example, framework, php, route, routing, zend, zf.
Categories: Development, Programming.

Need help on your PHP projects? Let's talk!

Zend Framework bug hunt days

by Robert Basic on November 22, 2009.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

On the 19th and 20th of this month, the third Zend Framework Bug Hunt days were held. I joined the party for the first time and I say, it was a jolly good one!

It was announced on Zend DevZone after which Pádraic wrote a nice and detailed Guide To Zend Framework Bug Hunt Days (I think I read on IRC that there’ll be a Bug Hunt Day FAQ, too). I decided to try and give back to the community as much as I can. OK, it wasn’t much - submitted a patch for an issue and closed another which was lacking information - but hey! I think it was pretty good for a noob bug hunter like me :P The only downside for me is it that it’s held on Thursdays and Fridays, which means I can join up only after work. Blah.

All in all - more than 130 closed issues - w00t!

So yep, see you all in December on the fourth Bug Hunt days ;)

Happy hacking!

Tags: bug, framework, hunt, zend, zf.
Categories: Development, Programming.

Need help on your PHP projects? Let's talk!

Zend Framework 1.8 Web Application Development book review

by Robert Basic on November 17, 2009.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

A few days ago I finished reading Keith Pope’s book titled “Zend Framework 1.8 Web Application Development”, so, after letting it “rest” in my mind for a while, here are my thoughts on it…

First, I must point out the “language” of the book - I was expecting a text that’s hard to follow, that’s full of words and sentences requiring at least two dictionaries by my side to help me out (hey, English is not my first language!), but, it was quite an easy and, if I may add, an enjoyable read.

If you think, that you’re just gonna sit down, read the book and know all about Zend Framework, boy you’re wrong! Yes, the book explains a lot, but you’ll still need to follow the example codes along the way and play with them to get really familiar with ZF.

The book starts off with a basic application (yep, “Hello world!”), explains the bootstrapping, configuring, working with action controllers, views and handling errors… The second chapter continues with explaining the MVC architecture, the front controller, router, dispatcher… It even has a nice flowchart about the whole dispatch process, great stuff.

From chapter 3 to chapter 12, the author is taking you through a process of building a web application – from creating the basic directory structure, over the hardcore programming stuff to the optimizing/testing part. Chapter 4 gives a rather good explanation on the “Fat Model Skinny Controller” concept; chapter 8 deals with authentication and authorization; chapter 11 takes care of the optimization.

At last, my favourite part of the book is when the author has several “ways out of a problem”, he tells the good and the bad sides of each, picks out the best one and explains why did he choose that particular one. I hate it when an author just simply says: “This is the right way, trust me.”, without caring to explain why.

So, would I recommend this book to a friend who wants to start working with ZF? Absolutely.

Also, be sure to check out what Jani, Raphael, Rob and Sudheer have to say about this book (Jani’s and Rob’s reviews are not up yet, so I’m linking to their feeds!), too.

Happy reading! :)

Edit 2009., November 23rd: Added a link Sudheer’s post :)

Tags: book, framework, php, review, zend, zf.
Categories: Development, Programming.

Need help on your PHP projects? Let's talk!