Category Archives: Zend

All right, guys. In response to the Zend 2 For Beginners post, I thought I’d carry on and show more on how not to be afraid of Zend 2.

If you find yourself frustrated with frameworks as I was a couple of years ago, don’t lose hope. A lot of the frameworks out there have evolved to the point where they’re very manageable. If you’re like me though, and want to do pure PHP on your own projects, no one is forcing you to do Zend. And no, using Zend doesn’t guarantee that everyone who will use it will be a better coder, or that your code will be cleaner, or faster. In fact, it takes 10 times more of the processing power to render out a zend call than a pure php call.

But, if you’re involved in working with others on Zend, I’m writing this to show you that anyone with a bit of programming knowledge can pick up Zend 2 and go with it. Simple.

Let’s start!

Here’s the structure you will need for the basic module. The bare essentials to just render out some text.

Don’t be scared. this is only 4 files if you look at it carefully. Yes, it’s a lot of folders, but it’s not a lot of code. Yes, clean PHP could do it in one file, but there is a lot more to worry about as far as keeping track of all of the including, etc.

So, now, let’s look at the first file: The Module.php off of the module root folder:

1234567891011121314151617181920212223242526

// /module/Articles/Module.php// This line defines our namespace. The place where our module resides. Keep this the same name as our module.namespace Articles;

And that’s it. Basically, this file tells us to load our module configuration and autoload any controller files we may have.

The next step is to look at our config file.:

12345678910111213141516171819202122232425262728293031323334

// /module/Articles/config/module.config.phpreturnarray('router'=>array('routes'=>array(// this is going to be what we call our route. It's a good idea to keep it the same name as our "link"'articles'=>array(// Yes, this time, were passing in an exact match for the route. No parameters, no trailing slashes, done.'type'=>'Literal','options'=>array(// Finally, this is what the URL should say to access this route.'route'=>'/articles','defaults'=>array(// And of course, we define our default controller and action.'controller'=>'Articles\Controller\Index','action'=>'index'))))),// Specify our controller shortcuts (from the controller above)'controllers'=>array('invokables'=>array('Articles\Controller\Index'=>'Articles\Controller\IndexController')),// and, finally, where to look for our main file when the /articles link is hit.'view_manager'=>array('template_map'=>array('articles/index/index'=> __DIR__ .'/../view/articles/index/index.phtml'))

);

Simply put, this code above will direct any calls that come in to the right logic in the code and the right template file to use when rendering.

That’s it. There really isn’t much to it. This view file will produce a comment of the location and some text that mentions that this is my article index file.

If you could resist through this tutorial and the previous one, you can pretty much build your own Zend 2 modules. Yes, there’s a lot more to learn about Zend, which I’ll slowly be covering in other posts, but for now, to get you started to understand how everything is pulled into Zend to render out some simple pages, this is all you need.

I’m also going to teach you how to load scripts and css files into the header. That’s going to be fun. And later than that, I will be covering AJAX calls and partials.