模块配置和布局配置在Zend框架(Module configuration and layout c

2019-08-31 18:36发布

我从其他文章的一些代码在Zend Framework的配置模块和布局。 我在当地与尝试。 我没有得到违约和管理模块不同的布局。 这是我为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

应用程序/ 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);
        }
    }
}

应用/模块/管理/ 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();
   }
}

请通过它,让我知道任何知道如何做正确的方式配置模块和布局..

感谢致敬,

prasanth P

Answer 1:

我使用插件的方法与此代码我写:

主引导程序:

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

在插件目录:

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");
        }
}

希望能帮助到你。



Answer 2:

从您的代码:

# 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"

您正在使用your_app /模块/管理/布局/ admin.phtml作为管理模块的布局,我想它取代your_app /布局/ layout.phtml。 检查的方式和模块之间切换尝试一些site.ressources.layout代替resources.layout.layout。 我是Zend的一个新手。 看看如何设置你引导在http://www.survivethedeepend.com/

同样的问题和解决方案已此间强调: http://blog.astrumfutura.com/archives/415-Self-Contained-Reusable-Zend-Framework-Modules-With-Standardised-Configurators.html



Answer 3:

在我的应用程序配置这种方式。 它完美地工作。

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);
}


Answer 4:

你需要使用一个控制器插件来实现,因为布局是基于请求项设置,并在引导应用已经没有分派,所以你需要使用控制器插件工作在preDispatch切换布局。



Answer 5:

我认为,最简单的方法就是检查URI_String。 请看下面:

我有一个命名为“管理”模块。 在布局文件夹我有2个目录。 “网站”和“admin”

\程序\设计\网站\ layout.phtml和\程序\设计\ ADMIN \ layout.phtml

添加的代码块上的bootstrap.php它只是改变了布局目录路径。

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);
}


Answer 6:

您的问题回答我的问题,这是正确的,我试图找出为什么没有在我的引导模块,其配置文件中看到,您需要添加行工作

administrador.resources.view [] =

Valew的合作伙伴!



Answer 7:

<?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);
}

}


Answer 8:

布局和模块(ZF版本1)新的Zend项目启用。 它需要启用,你需要使它工作。

布局适用于常见的页眉和页脚的工作Zend的项目,另一方面模块可用于不同类型的用户访问即模块,模块管理,模块为游客等等。

对于一个快速参考,你可以找到一个完整的项目,从这里得到的基本思想,我的网站上一个完整的解释。 。 http://www.getallthing.com/how-to-use-layout-and-module-in-zend-framework/

祝你好运和欢呼声!



Answer 9:

$options = array(
         'layout'     => 'layout',
         'layoutPath' => APPLICATION_PATH."/modules/".$layout_dir."/views/layouts"
  );
Zend_Layout::startMvc($options);

试图从SOF一些其他的解决方案,而这一次伟大的工作。 只需要到layoutPath指向实际布局的文件夹



文章来源: Module configuration and layout configuration in zend framework