Blog

Zend and ExpressionEngine Working Together

Have you ever tried to make two technologies working together? Has it worked well for you? Was it easy, out of the box type of task that took you about an hour to complete? Or was a week long thing, and you got out of it with less hair? If you fall in the last case, then youre in the right place, I will give you a few tips about getting ExpressionEngine to work nicely (sort of) with Zend.

In this post I am going to assume that you know what ExpressionEngine, Zend, CMSes, and MVC frameworks are. The challenge that well be trying to overcome is getting Zend to serve up static pages built in ExpressionEngine. Most people would probably have two applications and try to establish a connection between the two, where one application would be the custom built Zend application and the other would be the CMS loading static pages with ExpressionEngine. Or some sort complicated Apache RewriteRule filled solution. Not in this case.

Note: We are using ExpressionEngine 1.6.7, ZendFramework 1.7.4 and PHP 5.3

The first thing we did after installing EE and setting up our Zend directories (for more information on this check out the respective sites for EE and Zend), was to copy the index.php file into our public folder and named it ee.php, we also added a symlink in the public folder that points to the system folder of the EE code base. We copied and renamed the index.php file because we needed to rewrite some of the logic that deals with routing and where everything is found. We did also have to deal with some bugs and warnings, so you might have to do some of that yourself, I will only mention the changes that we made that are concerned with working with Zend.

Introducing EE to Zend

Since Zend is now responsible for all the routing for the application, we need to tell Zend how to behave when we want to display EE content. Since Zend is an MVC framework, the first place to start is in the controllers. We added a new controller called EEController which will take over the responsibilities of displayed EE content. In the controller we added the following function:

public function __call($method, $args){$this->_helper->viewRenderer->setNoRender();$this->_helper->layout->setLayout(“site”);

global $root;require_once $root . “/public/ee.php”;}

In this function “site” represents the general site layout that is defined in our Zend code which allows us to encapsulate the content in EE with our navigation menus. This also means that our work does not have to be duplicated in two places by creating the layouts in both Zend and EE (keeping with the DRY principal). And $root is a global variable defined in our application that points to the directory that contains our app folder which is where the controllers and the models and the views reside, and the public folder and other stuff such as the testing directory and other custom code. We use that $root variable to include the custom ee.php file that we talked about earlier. This is all that Zend needs for it to figure out that when someone goes to http://domain/ee/something that it needs to let EE figure out what to do with the something part and display that appropriate content. Now that this part is done, we need to configure EE to deal with the changes that weve made.

**The Secret Handshake **

Remember that ee.php file? Well heres what we added to make sure everything works nicely. At the top of the file theres a variable called $qtype, and the comments above it instruct you to set it to either 0,1, or 2. This variable controls a switch statement in the code that determines what EE should do with the url coming in (you know, that something part). Well we dont like any of the options, so we set the variable to 3:

$qtype = 3;

and then added another case in the switch statement:

case 3 :$uri = str_replace(“ee.php”, “”, ltrim($_SERVER[“REQUEST_URI”], “/”));$uri = str_replace(array(“ee/”, “admin_ee/”), “”, $uri);break;

the first line comes from the default behavior in the switch statement but now we have ee.php in there instead of index.php. As for the second part, well we want to take away the controller name from the uri that we added in there to get Zend to cooperate. We use an array because we could have multiple controllers in Zend that reroute to EE, in our case we have the EEController, so we neep to strip “ee” out of the uri, you could also have and admin EE controller name Admin_EEController, and you would use the second example “admin_ee”. This is necessary because EE uses relative paths and constructive urls to figure out what to do and we dont want to confuse it with our controller stuff, it is PHP after all.

There is one more thing that needs to be done. I just said that EE uses relative paths, well that means that links for anything that is produced by EE (like comments …) would need some tweaking. We need to have the name of our controller in the links for Zend not to choke, but we remove it once we get to the EE level for EE not to choke, so we need to tell EE to add the controller name in its links. We do that by specifying in the admin interface of EE (under CP Home/Admin/System Preferences/General Configuration ) that the name of the sites index page is the name of the EE controller, so we changed that field to `ee/.

…and voila, everything works nicely. Good luck and happy hunting!