Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can implement sub-modules with relatively little effort in ZF. Let's say you have directory structure such as:</p> <pre><code>application/ modules/ admin/ cms/ controllers/ views/ controllers/ views/ </code></pre> <p>You'd register the modules like this in your bootstrap (sub-modules use _ to separate the sub-module from the main module):</p> <pre><code>$frontController-&gt;setControllerDirectory(array( 'default' =&gt; APPLICATION_PATH . '/modules/default/controllers', 'admin' =&gt; APPLICATION_PATH . '/modules/admin/controllers', 'admin_cms' =&gt; APPLICATION_PATH . '/modules/admin/cms/controllers' )); </code></pre> <p>The issue with this is that it would actually use an underline in the URL instead of a slash, so eg: "admin_cms/conteroller/action" instead of "admin/cms/controller/action". While this "works", it's not pretty. One way to solve the issue is to provide your own route for the default route. Since the default Zend_Controller_Router_Route_Module does it <em>almost</em> right, you can simply extend from it and add the wanted behavior:</p> <pre><code>&lt;?php class App_Router_Route_Module extends Zend_Controller_Router_Route_Module { public function __construct() { $frontController = Zend_Controller_Front::getInstance(); $dispatcher = $frontController-&gt;getDispatcher(); $request = $frontController-&gt;getRequest(); parent::__construct(array(), $dispatcher, $request); } public function match($path) { // Get front controller instance $frontController = Zend_Controller_Front::getInstance(); // Parse path parts $parts = explode('/', $path); // Get all registered modules $modules = $frontController-&gt;getControllerDirectory(); // Check if we're in default module if (count($parts) == 0 || !isset($modules[$parts[0]])) array_unshift($parts, $frontController-&gt;getDefaultModule()); // Module name $module = $parts[0]; // While there are more parts to parse while (isset($parts[1])) { // Construct new module name $module .= '_' . $parts[1]; // If module doesn't exist, stop processing if (!isset($modules[$module])) break; // Replace the parts with the new module name array_splice($parts, 0, 2, $module); } // Put path back together $path = implode('/', $parts); // Let Zend's module router deal with the rest return parent::match($path); } } </code></pre> <p>And in your bootstrap:</p> <pre><code>$router = Zend_Controller_Front::getInstance()-&gt;getRouter(); $router-&gt;addRoute('default', new App_Router_Route_Module); </code></pre> <p>What this does is traverse the path as long as it finds a module, and transparently rewrites the path so that the default Zend_Controller_Router_Route_Module can do the real work. For example the following path: "/admin/cms/article/edit" will be transformed into "/admin_cms/article/edit", which allows the standard convention of the ZF's ":module/:controller/:action" do the magic.</p> <p>This allows you to have nice modular structure with self-contained modules, while still use pretty, logical URLs. One thing you want to make note of is that if you use Zend_Navigation and specify the navigation items using module/controller/action parameters, you need to tell ZF how to correctly build the URL using "/" instead of "_" in module names (by default ZF uses the :module/:controller/:action spec when it builds the URLs). You can do this by implementing your own Zend_Controller_Action_Helper_Url, like this:</p> <pre><code>&lt;?php class App_Router_Helper_Url extends Zend_Controller_Action_Helper_Url { public function url($urlOptions = array(), $name = null, $reset = false, $encode = false) { // Replace the _ with / in the module name $urlOptions['module'] = str_replace('_', '/', $urlOptions['module']); // Let the router do rest of the work return $this-&gt;getFrontController()-&gt;getRouter()-&gt;assemble($urlOptions, $name, $reset, $encode); } } </code></pre> <p>And in your bootstrap:</p> <pre><code>Zend_Controller_Action_HelperBroker::addHelper(new App_Router_Helper_Url); </code></pre> <p>Now Zend_Navigation works nicely with your sub-module support as well.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload