Module configuration and layout configuration in z

2019-01-22 08:44发布

I got some codes from other articles for configuring module and layout in zend framework. I tried with in my local. i didn't get different layout for default and admin module. Here is my code for configuring module and layout for zend framework.

configs/application.ini

[production]

# Debug output
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0

# Include path
includePaths.library = APPLICATION_PATH "/../library"

# Bootstrap
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"

admin.bootstrap.path = APPLICATION_PATH "/modules/admin/Bootstrap.php"
admin.bootstrap.class = "admin_Bootstrap"

# Front Controller
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.env = APPLICATION_ENV

# Session
resources.session.name = "ZendSession"
resources.session.save_path = APPLICATION_PATH "/../data/session"
resources.session.remember_me_seconds = 86400

# Layout
resources.layout.layout = "layout"
resources.layout.layoutPath = APPLICATION_PATH "/layouts"
admin.resources.layout.layout = "admin"
admin.resources.layout.layoutPath = APPLICATION_PATH "/modules/admin/layouts"

# Views
resources.view.encoding = "UTF-8"
resources.view.basePath = APPLICATION_PATH "/views/"
resources.view[] =

resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] =
resources.view[] =
admin.resources.view[] = 

[staging : production]

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

application/Bootstrap.php

<?php

/**
* Ensure all communications are managed by sessions.
*/
require_once ('Zend/Session.php');
Zend_Session::start();

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

     protected function _initDoctype() {
      $this->bootstrap( 'view' );
      $view = $this->getResource( 'view' );
      $view->navigation = array();
      $view->subnavigation = array();
      $view->headTitle( 'Module One' );
      $view->headLink()->appendStylesheet('/css/clear.css');
      $view->headLink()->appendStylesheet('/css/main.css');
      $view->headScript()->appendFile('/js/jquery.js');
      $view->doctype( 'XHTML1_STRICT' );
      //$view->navigation = $this->buildMenu();
   }

    /*protected function _initAppAutoLoad()
    {
   $autoloader = new Zend_Application_Module_Autoloader(array(
       'namespace' => 'default',
       'basePath' => APPLICATION_PATH
        ));
   return $autoloader;
    }*/

    protected function _initLayoutHelper()
    {
        $this->bootstrap('frontController');
        $layout = Zend_Controller_Action_HelperBroker::addHelper(
            new ModuleLayoutLoader());
    }


   public function _initControllers()
   {
       $front = Zend_Controller_Front::getInstance();
      $front->addModuleDirectory(APPLICATION_PATH . '/modules/admin/', 'admin');
    }

    protected function _initAutoLoadModuleAdmin() {
        $autoloader = new Zend_Application_module_Autoloader(array(
            'namespace' => 'Admin',
            'basePath' => APPLICATION_PATH . '/modules/admin'
        ));

        return $autoloader;
    }

    protected function _initModuleutoload() {
      $autoloader = new Zend_Application_Module_Autoloader ( array ('namespace' => '', 'basePath' => APPLICATION_PATH ) );
      return $autoloader;
   }

}

class ModuleLayoutLoader extends Zend_Controller_Action_Helper_Abstract
// looks up layout by module in application.ini
{
    public function preDispatch()
    {
        $bootstrap = $this->getActionController()
                          ->getInvokeArg('bootstrap');
        $config = $bootstrap->getOptions();
        echo $module = $this->getRequest()->getModuleName();
        /*echo "Configs : <pre>";
        print_r($config[$module]);*/
        if (isset($config[$module]['resources']['layout']['layout'])) {
            $layoutScript = $config[$module]['resources']['layout']['layout'];
            $this->getActionController()
            ->getHelper('layout')
            ->setLayout($layoutScript);
        }
    }
}

application/modules/admin/Bootstrap.php

<?php
class Admin_Bootstrap extends Zend_Application_Module_Bootstrap
{
/*protected function _initAppAutoload()
{
    $autoloader = new Zend_Application_Module_Autoloader(array(
        'namespace' => 'admin',
        'basePath' => APPLICATION_PATH . '/modules/admin/'
    ));
    return $autoloader;
}*/
   protected function _initDoctype() {
      $this->bootstrap( 'view' );
      $view = $this->getResource( 'view' );
      $view->navigation = array();
      $view->subnavigation = array();
      $view->headTitle( 'Module One' );
      $view->headLink()->appendStylesheet('/css/clear.css');
      $view->headLink()->appendStylesheet('/css/main.css');
      $view->headScript()->appendFile('/js/jquery.js');
      $view->doctype( 'XHTML1_STRICT' );
      //$view->navigation = $this->buildMenu();
   }
}

Please go through it and let me know any knows how do configure module and layout in right way..

Thanks and regards,

Prasanth P

9条回答
贼婆χ
2楼-- · 2019-01-22 09:15

In my application I configured this way. It worked perfectly.

protected function _initLayout(){
    $layout = explode('/', $_SERVER['REQUEST_URI']);

    if(in_array('admin', $layout)){
        $layout_dir = 'admin';
    }else if(in_array('default', $layout)){
        $layout_dir = 'default';
    }else{
        $layout_dir = 'default';
    }
      $options = array(
             'layout'     => 'layout',
             'layoutPath' => APPLICATION_PATH."/modules/".$layout_dir."/views/layouts"
      );
    Zend_Layout::startMvc($options);
}
查看更多
孤傲高冷的网名
3楼-- · 2019-01-22 09:18

I use plugin approach with this code I have written:

in main Bootstrap:

protected function _initPlugins()
{
        // Access plugin
        $front = Zend_Controller_Front::getInstance(); 
        $front->registerPlugin(new MyApp_Plugin_Module());
}

In plugin directory:

class MyApp_Plugin_Module extends Zend_Controller_Plugin_Abstract
{

    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {

        $module = $request->getModuleName();
        $layout = Zend_Layout::getMvcInstance();

        // check module and automatically set layout
        $layoutsDir = $layout->getLayoutPath();
        // check if module layout exists else use default
        if(file_exists($layoutsDir . DIRECTORY_SEPARATOR . $module . ".phtml")) {
            $layout->setLayout($module);
        } else {
            $layout->setLayout("default");
        }
}

Hope it helps.

查看更多
唯我独甜
4楼-- · 2019-01-22 09:18
<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

   public function _initAutoload() {
            $autoloader = Zend_Loader_Autoloader::getInstance();
            $moduleLoader = new Zend_Application_Module_Autoloader(
                    array(
                            'namespace' => '',
                             'basePath' => APPLICATION_PATH . '/modules'

                    )
            );

             return $moduleLoader;
    } 


protected function _initViewhelpers()
{
    $this->bootstrap('layout');
    $layout = $this->getResource('layout');
    $view = $layout->getView();
    $view->doctype('XHTML1_STRICT');
    $view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
}


protected function _initNavigation()
{
    $this->bootstrap('layout');
    $layout = $this->getResource('layout');
    $view = $layout->getView();
    $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml','nav');

    $navigation = new Zend_Navigation($config);
    $view->navigation($navigation);
}

}
查看更多
爷的心禁止访问
5楼-- · 2019-01-22 09:19

I think the easiest way is check the URI_String. Please see below:

I have a module named as "admin". Under layout folder I have 2 directories. "site" and "admin"

\application\layout\site\layout.phtml and \application\layout\admin\layout.phtml

Add this block of code on Bootstrap.php It just change the layout directory path.

protected function _initLayout(){
    $layout = explode('/', $_SERVER['REQUEST_URI']);

    if(in_array('admin', $layout)){
        $layout_dir = 'admin';
    }else{
        $layout_dir = 'site';
    }

      $options = array(
             'layout'     => 'layout',
             'layoutPath' => APPLICATION_PATH . "/layouts/scripts/".$layout_dir,
      );

    Zend_Layout::startMvc($options);
}
查看更多
Fickle 薄情
6楼-- · 2019-01-22 09:20

You need to use a Controller Plugin to achieve that, because the layout is set based on the request entry, and on the bootstrap the application hasn't been dispatched, so you need to use a controller plugin to work on the preDispatch to switch layouts.

查看更多
混吃等死
7楼-- · 2019-01-22 09:23

Layout and module in not enabled on a newly zend project (in ZF version 1). It needs to be enabled and you need to make it work.

Layout works for the common header and footer for the working zend project, on the other hand module can be used for the different kind of access i.e module for user, module for admin, module for visitor and so on.

For a quick reference you can find a complete explanation with a complete project to get the basic idea from here, on my site. . http://www.getallthing.com/how-to-use-layout-and-module-in-zend-framework/

Good luck and cheers!

查看更多
登录 后发表回答