Zend Framework: Routing Wildcard Subdomains

February 11th, 2009 by KevinNuut.com

Usually, with any decent size site, you are going to run into using subdomains. Whether as static marketing pages or wildcard user domains, it is often necessary to route these subdomains to a particular script in your site.

Zend Framework uses the front controller MVC methodology, which means all pages get routed through index.php. Sure, you could rewrite the subdomain using .htaccess, but you’d lose lose the fancy subdomain URL you crave. So how can you map any subdomain to the proper module / controller / action? Despite the lack of documentation, it is rather easy, with only an update to the preDispatch plugin being required.

The Request Object of Zend Framework provides three very useful methods:

  1.  
  2. $request->setModuleName();
  3. $request->setControllerName();
  4. $request->setActionName();

With these three methods, you can route to any existing action in your site. For example, let’s say you have the site with three subdomains: about, admin, and fun. Each subdomain has its own module, with its own set of controllers and actions. To route these subdomains to the proper folders, simply do the following in your public function preDispatch of any plugin.

  1.  
  2. public function preDispatch(Zend_Controller_Request_Abstract $request)
  3. {
  4. // Split the domain name into parts, assume the structure subdomain.domain.com
  5. $urlParts = explode('.', $_SERVER['HTTP_HOST']);
  6. $subDomain = $urlParts[0];
  7. $request->setModuleName($subDomain);

See, you are already done. Of course, you’ll want to actually check that the module exists, or if you weren’t using modules, you could route subdomains to different controllers. In a user subdomain scenario, you’d probably route all subdomains to a particular controller and action, like so:

  1.  
  2. public function preDispatch(Zend_Controller_Request_Abstract $request)
  3. {
  4. // Split the domain name into parts, assume the structure subdomain.domain.com
  5. $urlParts = explode('.', $_SERVER['HTTP_HOST']);
  6. $subDomain = $urlParts[0];
  7. if (sizeof($urlParts) === 3 && $subDomain !== 'www') {
  8. $request->setControllerName('profiles');
  9. $request->setActionName('view');
  10. // Set the parameter userId to the user in the subdomain
  11. // You can get this in the view action with getParam('userId');
  12. $request->setParam('userId', $subDomain);
  13. ?>

So that basically covers it. I need to practice being less verbose, but you get the idea.

Zend Framework: Add Module Subheaders

February 9th, 2009 by KevinNuut.com

If you are developing with Zend Framework and have a huge website, it’s probably a good idea to start breaking those websites into modules.  Somtimes, each module needs its own layout, but sometimes, each module simply needs its own set of subheaders and subfooters.  After some research, and a lot of experimentation, I’ve stumbled upon a solution using plugins, preDispatch, and postDispatch:

  1. <?php
  2. class Plugin_Initialize extends Zend_Controller_Plugin_Abstract
  3. {
  4. protected $_zendAuth;
  5. protected $_frontController;
  6. protected $_reg;
  7. protected $_view;
  8. protected $_loopedOnce;
  9. public function __construct()
  10. {
  11. $this->_zendAuth        = Zend_Auth::getInstance();
  12. $this->_frontController = Zend_Controller_Front::getInstance();
  13. $this->_reg             = Zend_Registry::getInstance();
  14. $this->_view            = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->view;
  15. $this->_loopedOnce      = false;
  16. }
  17.  

This will render a header and a footer and assign them to the layout response.  This assumes you are using the MVC layout, and you are using the standard module directory layout.  Then, in your primary layout.html, you can use the following code to generate a page:

  1. <div id="mainContent">
  2. <?php echo $this->layout()->subheader; ?>
  3. <?php echo $this->layout()->content; ?>
  4. <?php echo $this->layout()->subfooter; ?>
  5. </div>
  6.  

The previous code assumes a folder structure of:

  • module
    • <moduleName>
      • layouts
      • controllers
      • views
        • scripts
          • <scriptName>

PHP and MySQLi Heads Up

January 23rd, 2009 by KevinNuut.com

Although the documentation on PHP.net lists MySQLi as having the function mysqli_result::fetch_all(), the fact that it is available with only mysqlnd isn’t too clear. I feel like I wasted 10 minutes…