Jan 182013
 

During a code review last week, one of my clients mentioned that they were looking into alternatives to Zend_Form for their ZF1 application. They were looking for something that was easier to use than ZF1′s Zend_Form for their developers, and could easily be used with Twitter Bootstrap form markup without having to deal with decorators. Without giving it much thought, I suggested that it might be possible to take advantage of the new Zend\Form from ZF2, which would easily meet their needs, and there’s a ton of Twitter Bootstrap modules for ZF2 that easily make your forms Bootstrap-friendly.

This got me thinking… How difficult would it be to create a sort of compatibility layer enabling ZF1 applications to take advantage of ZF2 features and modules without refactoring the entire application for ZF2? Well, as it turns out, not very difficult. Allow me to introduce ZF 2-for-1.

Simply install ZF 2-for-1, and you can instantly use many of the great new ZF2 features right inside of your existing ZF1 applications. It’s even capable of loading ZF2 modules. Of course, not all ZF2 modules will “just work” with ZF1 using ZF 2-for-1, but some simple modules should work just fine. As ZF 2-for-1 improves, more and more modules will likely become compatible.

ZF2 Training at PHPNW12

 Posted by on September 10, 2012  General  No Responses »  Tagged with: ,
Sep 102012
 

I am very excited to announce that Rob Allen and I will be presenting a full day tutorial on Zend Framework 2 at PHPNW12 in Manchester, UK.

With Zend Framework 2 released, this tutorial will walk you through building a complete ZF2 MVC application from the ground up. Starting with the ZF2 skeleton application, we’ll discuss how and why it works and look at the core components used. Specifically, I’ll ensure that you understand how ZF2′s service manager, dependency injector and event manager are used with the HTTP and MVC components as the foundation of a ZF2 application. We will also look at how to use and install pre-existing modules, while also creating our own modules during the tutorial. Grab your laptop and come learn all about the new ZF2 MVC, event manage, robust module system, and more.

Head over to the tutorial page and buy a ticket now!

Why Zend Framework

 Posted by on September 7, 2012  General  30 Responses »  Tagged with: ,
Sep 072012
 

Fabien Potencier just wrote an interesting post where he outlines what he believes to be the selling points of the Symfony project. First, let me point out that all of his points are 100% valid, and there’s no doubt that Symfony is a great framework. I have great respect for Fabien and the Symfony project as a whole.

That said, my concern with Fabien’s post is that usually “selling points” for something imply that they are things that set something apart from the alternatives. Now, I’m sure Fabien had no ill-intentions when writing his post, but I do worry that some may misinterpret his post as a list of things that set Symfony apart from Zend Framework or other frameworks, which is simply not true.

Allow me to address each of Fabien’s points from Zend Framework’s perspective:

Continue reading »

Disabling the layout in Zend Framework 2

 Posted by on August 16, 2012  General  6 Responses »  Tagged with: ,
Aug 162012
 

Sometimes you need to disable the layout for a specific action. To do this, you simply set the view model that your action returns as “terminal”. This tells ZF2 not to wrap the returned view model with a layout.

<?php
 
namespace Application\Controller;
 
use Zend\Mvc\Controller\ActionController;
use Zend\View\Model\ViewModel;
 
class IndexController extends ActionController
{
    public function nolayoutAction()
    {
        // Turn off the layout, i.e. only render the view script.
        $viewModel = new ViewModel();
        $viewModel->setTerminal(true);
        return $viewModel;
    }
}

More examples can be found in Rob Allen’s ZF2TestApp.

Aug 122012
 

This post will show you how to create a simple view helper in Zend Framework 2.

In this example, our view helper will simply return the full, absolute URL of the current page/request.

<?php
// ./module/Application/src/Application/View/Helper/AbsoluteUrl.php
namespace Application\View\Helper;
 
use Zend\Http\Request;
use Zend\View\Helper\AbstractHelper;
 
class AbsoluteUrl extends AbstractHelper
{
    protected $request;
 
    public function __construct(Request $request)
    {
        $this->request = $request;
    }
 
    public function __invoke()
    {
        return $this->request->getUri()->normalize();
    }
}

You’ll notice that this particular helper has a dependency — a Zend\Http\Request object. To inject this, we’ll need to set up a factory with the initialization logic for our view helper:

<?php
// ./module/Application/Module.php
namespace Application;
 
use Application\View\Helper\AbsoluteUrl;
 
class Module
{
    public function getViewHelperConfig()
    {
        return array(
            'factories' => array(
                // the array key here is the name you will call the view helper by in your view scripts
                'absoluteUrl' => function($sm) {
                    $locator = $sm->getServiceLocator(); // $sm is the view helper manager, so we need to fetch the main service manager
                    return new AbsoluteUrl($locator->get('Request'));
                },
            ),
        );
    }
 
     // If copy/pasting this example, you'll also need the getAutoloaderConfig() method; I've omitted it for the sake of brevity.
}

That’s it! Now you can call your helper in your view scripts:

The full URL to the current page is: <?php echo $this->absoluteUrl(); ?>

Note: I’ll be covering the details of the getViewHelperConfig() method and how ZF2 uses the ServiceManager to handle view helpers, controller plugins, etc in a later post.

Jul 182012
 

This post is intended to familiarize you with the various features of the new Zend Framework 2 ServiceManager component along with some simple examples.

So, what is the ServiceManager? Basically it’s a registry, or container (the proper term is service locator) to hold various objects needed by your application, allowing you to easily practice Inversion of Control. The service manager holds just the information needed to lazily instantiate these objects as they’re needed. So if you were thinking ‘services’ such as those composing a service layer, you might be better off thinking of the service manager more as an “object manager” or “instance manager”.

Continue reading »

Jul 182012
 

So you’re all excited to try out ZF2. You clone the skeleton, install some modules, maybe even follow Rob Allen’s excellent ZF2 tutorial, and finally, start building your application. Now, if you’re a former ZF1 user or refugee from another framework, you might be troubled at this point by the fact that, at first glance, ZF2 doesn’t appear to take into consideration environment-specific configuration values (e.g., development, testing, staging, production). Luckily, this is not the case!

Continue reading »

Jun 292012
 

The new Zend\Db in Zend Framework 2 has a handy feature which allows you to specify your own entity/model class to represent rows in your database tables. This means you can tell Zend\Db to return each row as a populated instance of your own custom objects. Keep in mind that this is simply a convenience feature, and not meant to serve as a fully-featured ORM. If you’re looking for a full-blown ORM, have a look at Doctrine 2.

For this demonstration, let’s assume the following table:

CREATE TABLE `book` (
   `isbn10` VARCHAR(10) NOT NULL,
   `isbn13` VARCHAR(13) NOT NULL,
   `author` VARCHAR(50) NOT NULL,
   `title` VARCHAR(255) NOT NULL,
   `year` INT(4) NOT NULL
);

Continue reading »

May 112012
 

First, I should point out that the title of this post is a bit of an intentional misnomer. There’s really no such thing as “module-specific” anything in ZF2, so what we’re really talking about is the topmost namespace of the controller being dispatched. So in the case of MyModule\Controller\SomeController, the topmost namespace would be MyModule. In most cases, this will be the name of a given module.

UPDATE: The information in this post is still correct and applicable, but I’ve made a very simple module called EdpModuleLayouts to make it even easier.

Here’s how you can easily switch the layout (or perform any other arbitrary logic) for a specific module in Zend Framework 2.0 (as of d0b1dbc92):

<?php
namespace MyModule;
 
use Zend\ModuleManager\ModuleManager;
 
class Module
{
    public function init(ModuleManager $moduleManager)
    {
        $sharedEvents = $moduleManager->getEventManager()->getSharedManager();
        $sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) {
            // This event will only be fired when an ActionController under the MyModule namespace is dispatched.
            $controller = $e->getTarget();
            $controller->layout('layout/alternativelayout');
        }, 100);
    }
}

This event listener will only be triggered if an ActionController under the MyModule namespace is dispatched, so you do not need to perform any additional logic to check which “module” or namespace the controller being dispatched is under.

Keep in mind, as of writing this, ZF2 is still in beta. There are plans to add a convenience layer to the framework before the GA release which will likely make common tasks like this much simpler.

See also: Rob Allen’s post on module specific bootstrapping in ZF2

Apr 262012
 

NOTE: This has now been updated as of ZF2 beta5.

With the new modular infrastructure in Zend Framework 2, one of the most common questions will indoubitably be how to share a database connection across modules. Here’s a quick explanation of how to set up and share your database connection across multiple modules in a way that can even allow you to use a single connection between Zend\Db, Doctrine2, and possibly even other database libraries / ORMs.

Continue reading »