Skip navigation

Sign up | Lost password? | Help

Hello World

Practical programming... and stuff...

Building a CMS with the Zend Framework

, ,

I've been working on a Content Management System (CMS) for my personal use and for experimenting. Since I like the Zend Framework so much, using it as a base for the CMS was a natural choice.

While at first it might seem that using ZF adds many restrictions, it actually isn't like that. Thanks to the modularity of the framework, you can easily replace certain functinality with classes of your own to change the way it works.

There are also many other things to consider when making a CMS.


First, you obviously need to know what you want your CMS to be able to do. I wanted my CMS to be easily extensible, so it needs to support multiple kinds of pages such as "content" pages which would be normal pages with text, blog pages which could be blogs and things like that.

Here's a list of things I want my CMS to do at the moment
  • Pages - The CMS needs to store the pages in a database so that they are easily editable. The database will also be used to generate the main navigation menu and things like that.
  • The CMS needs to have good user access control - At least the ability to allow certain users to modify only certain pages
  • Support for different page types - You must be able to add new kinds of pages that can be created/modified from within the CMS' admin panel


So the stuff is pretty common CMS functionality. One might ask why I'm not just using some ready-made CMS... No, it's much more fun to make your own :wink:


Initial problems


Considering the way the Zend Framework works, there is a few initial problems that need to be solved.


By default, the initial request is routed to the IndexController's indexAction. Now, if you want to have multiple page types this has a certain problem:

You need to have a controller that has certain functionality, which depends on the page type. So what if the page you want to show as the home page is not of the same type that your IndexController is?
Another thing is that you need to access the database to figure out the homepage to begin with.

So to solve this, we need to think of a way to route requests to correct controller dynamically and not based on the controller variable in the URL (mypage.com/controller/action).


Another related problem is the design of the admin page. You need to be able to add and edit different page types and the different page types need to have different looking add/edit pages. An image gallery page's edit mode can't look the same as blog's now can it?


Solving the routing problem


So first is the routing problem. We can solve this by modifying the behavior of the Zend_Controller_Router_Rewrite and changing the default route to a custom route based on Zend_Controller_Router_Route_Module. I will not go into much details about them on this post, but I will talk about the implementation I have for them and leave the details for a later post.

To be able to figure out the required page types and such, we need to store each page's type in the database. The page types must have a name and a controller which parses them. We can use a parameter in the route to determine the controller where we need to route the request to.

I'm currently using something like this: mypage.com/controller/pagename/action
The pagename variable controls the name of the page which is used to determine what content to load from the database. Now, I am planning on changing this to mypage.com/pagename/action, so that the controller doesn't appear in the URL since it can be queried from the database.

The pagename is stored as a parameter in the request in the custom route. If you look at the source of Zend_Controller_Router_Route_Module, it looks at the URL and parses the module, controller and action from it. I simply made my custom route do the same, except it also parses the pagename from the URL and sets it as a parameter in the request.

The custom router is needed for determining the home page. This is much easier to do than the route part: We simply extend Zend_Controller_Router_Rewrite and override the route() method. In this method, we call the parent route method and then look at the request we get from it to see if the request is going to the index controller's index action. If so, we simply query the database for the default page, its type and controller, and change the request's parameters to those.

So it's pretty easy to change the behavior of the routing process. I will go into details about the code in a future post, so look forward to it.


The admin panel


The admin panel's add/edit page is quite simple to add if you have the routing sorted out.
I decided to put all the admin pages to the admin module while the other pages are in the default module. To support different admin pages for each page type, you can simply create an admin controller for each page type and use the page type to figure out which one you need to use. I created a base admin controller which defines some abstract methods that you just need to create in your page admin controller so it's pretty easy to create new page types.





At the moment my CMS supports the page types and the custom admin panel thing I mentioned. Only the user access control is missing, at the moment it's done by a simple ACL class and only the admin account can access the admin panel. I will probably create a customized version of the Zend_Acl class which uses the database to determine the specific access rights to a page and such.

There are also some modifications I've done to Smarty and the ViewRenderer helper so that I can use layouts instead of always including headers and footers in my templates. I will also post about this later.

Here's my newer post about ViewRenderer and layouts

Fulltext MySQL tag searchUsing layouts with Zend ViewRenderer helper

Comments

zomg 19. September 2007, 15:13

See my post here for a better explanation and example code on the customized routing I described in this post.

Anonymous 11. January 2008, 11:19

Forrest Lyman writes:

if you are interested in playing with a zf based cms i just put up a pre release of my cms. It is covered under the new BSD license.

http://code.google.com/p/digitalus-site-manager/downloads/list

Anonymous 6. May 2008, 11:29

alitrix writes:

Hi, this is exactly what I'm looking for too understand ZF.

But there is one problem, I can't find any database dump, so I can test it locally.

Can you please tell me where I can find it or maybe u can post it?

Greetz,

alitrix

Anonymous 6. May 2008, 11:30

alitrix writes:

Sorry, found it :ko:

appliciation/data/*.sql

Anonymous 30. July 2008, 23:26

Anonymous writes:

seems to have stalled in development?

Anonymous 6. February 2009, 12:05

Anonymous writes:

:)

Anonymous 17. June 2009, 20:12

Anonymous writes:

This article shows clearly that you have absolutely no idea what you're doing. Learn to use Zend_Layout.

Anonymous 18. July 2009, 00:33

Anonymous writes:

Anonymous #17, you obviously have no idea what you are doing. Zend_Layout was not available when this post was written.

Next time consider keeping your mouth shut so everyone doesn't know you are an idiot.

How to use Quote function:

  1. Select some text
  2. Click on the Quote link

Write a comment

Comment
(BBcode and HTML is turned off for anonymous user comments.)

If you can't read the words, press the small reload icon.


Smilies