0

请为我的查询提供适当的解决方案。我试图解决它,但没有得到任何适当的解决方案。请给我正确的解决方案。

如果我从 application.ini 文件中删除以下行,那么它适用于前端应用程序

resources.modules[] =

删除它后,我无法在具有正确布局的模块文件夹中获取我创建的模块(管理员)。我只有一个模块。在模块引导文件中,我定义了以下函数(project/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 _initPlugins()
    {
        $bootstrap = $this->getApplication();
        if ($bootstrap instanceof Zend_Application) {
            $bootstrap = $this;
        }
        $bootstrap->bootstrap('FrontController');
        $front = $bootstrap->getResource('FrontController');

        $plugin = new Admin_Plugin_Layout();
    //    $plugin->setBootstrap($this);
        $front->registerPlugin($plugin);
    }

    protected function _initAuthPlugin()
    {
       $checkAuth = Zend_Controller_Front::getInstance();
       $checkAuth->registerPlugin(new Admin_Plugin_CheckAuth(Zend_Auth::getInstance()));
    }

    protected function _initDoctype()
    {
      global $adminModuleCssPath;
      global $adminModuleJsPath;
      $this->bootstrap( 'view' );
      $view = $this->getResource( 'view' );

      $view->headTitle('Jyotish - Ek Gyan');
      $view->headScript()->appendFile($adminModuleJsPath.'jquery-1.7.2.js');
      $view->headScript()->appendFile($adminModuleJsPath.'jquery-ui.js');     
      $view->headScript()->appendFile($adminModuleJsPath.'tinybox.js');
      $view->headScript()->appendFile($adminModuleJsPath.'common.js');
      $view->headLink()->appendStylesheet($adminModuleCssPath.'jquery-ui.css');
      $view->headLink()->appendStylesheet($adminModuleCssPath.'style.css');
      $view->headLink()->appendStylesheet($adminModuleCssPath.'theme.css');
      $view->headLink()->appendStylesheet($adminModuleCssPath.'tinybox.css');
      $view->doctype( 'XHTML1_STRICT' );
      //$view->navigation = $this->buildMenu();
    }

    protected function _initLayoutPlugin()
    {
         $layout = Zend_Controller_Front::getInstance();
         $layout->registerPlugin(new Admin_Plugin_AdminLayout());

    }

    protected function _initRouter()
    {
        $frontController = Zend_Controller_Front::getInstance();
        $router = $frontController->getRouter();
        $route = new Zend_Controller_Router_Route(
                 ':module/:controller/:action/*',
                 array('module' => 'admin')
              );
        $router->addRoute('default', $route);

        $usersRoute = new Zend_Controller_Router_Route_Regex(
                      ':module/:controller/:action/(?:/page/(\d+)/?)?',
                      array(
                            'module' => 'admin',
                            'controller' => 'users',
                            'action' => 'index',
                            'page' => 1,
                            ),
                      array(
                              'page' => 1,
                            )
                    );

        $router->addRoute('users-index', $usersRoute);

    }

    protected function _initActionHelpers()
    { 
        Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH . "/modules/admin/views/helpers");
        Zend_Controller_Action_HelperBroker::addPrefix('Admin_View_Helper');
    }

}

在模块文件夹中,我创建了以下插件布局

class Admin_Plugin_Layout extends Zend_Controller_Plugin_Abstract
{
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
       if ('admin' != $request->getModuleName()) {
            // If not in this module, return early
            return;
        }
        // Change layout
        Zend_Layout::getMvcInstance()->setLayout('admin');
    }
}

在前端引导文件中,我定义了以下函数(project/application/Bootstrap.php)

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initAppAutoload()
    {
        $autoloader = new Zend_Application_Module_Autoloader(array(
            'namespace' => 'default',
            'basePath' => dirname(__FILE__),
        ));
        return $autoloader;
    }
        protected function _initLayoutHelper()
        {
            $this->bootstrap('frontController');
            $layout = Zend_Controller_Action_HelperBroker::addHelper(
                new Application_View_Helper_LayoutLoader());
        }
    }

我在(project/application/view/helper/LayoutLoader.php)中创建了以下帮助文件

<?php
class Application_View_Helper_LayoutLoader extends Zend_Controller_Action_Helper_Abstract
{

    public function preDispatch()
    {
        $bootstrap = $this->getActionController()
                         ->getInvokeArg('bootstrap');
        $config = $bootstrap->getOptions();
        $module = $this->getRequest()->getModuleName();
        if (isset($config[$module]['resources']['layout']['layout'])) {
            $layoutScript =
                 $config[$module]['resources']['layout']['layout'];
            $this->getActionController()
                 ->getHelper('layout')
                 ->setLayout($layoutScript);
        }

    }

}

从最近两天开始,我试图为两者创建单独的布局,但我无法获得正确的解决方案。当我在浏览器中运行管理模块时,它运行良好,但是当我运行前端应用程序文件夹时,它显示管理布局的异常错误。

请给我正确的解决方案....

谢谢

4

2 回答 2

1

进行布局切换的标准方法是使用前端控制器插件。你不需要带钩子的LayoutLoader助手。preDispatch

一个简单的布局切换器插件可以实现如下。

将您的各种布局文件放在 中application/layouts/scripts/,与您的模块命名相同:default.phtmladmin.phtml等。

在文件中application/plugins/Layout.php

class Application_Plugin_Layout extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        Zend_Layout::getMvcInstance()->setLayout($request->getModuleName());
    }
}

启用插件application/configs/application.ini使用:

resources.frontController.plugins.layout = "Application_Plugin_Layout"

或通过在Bootstrap.

此外,请确保您application.ini启用模块并确定您的布局位置:

resources.modules[]=
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
于 2013-03-30T08:56:57.017 回答
0

你似乎想多了。我将把它简化为基本的,你可以在你认为合适的时候添加回来。

ZF1 中的引导系统是个坏笑话。引导文件之一中存在的任何内容都将在运行时对所有模块可用,并将随每个请求一起运行。所以保持简单并保持轻便。我通常将所有 _init 方法放在主引导程序中,并将模块引导程序留空。

//simplify your bootstrap to minimum
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    //your app autoloader is the standard autoloader so is not needed here, it better just work.

   /**
    * initialize the session
    */
    protected function _initsession()
    {
        Zend_Session::start();
    }

    /**
     * initialize the registry and assign application.ini to config namespace
     */
    protected function _initRegistry()
    {
        $config = new Zend_Config($this->getOptions());
        Zend_Registry::set('config', $config);
    }

    protected function _initView()
    {
        //Initialize view
        $view = new Zend_View();
        //add custom view helper path
        $view->addHelperPath('/../library/My/View/Helper');
        //set doctype for default layout
        $view->doctype(Zend_Registry::get('config')->resources->view->doctype);
        //set default title
        $view->headTitle('Jyotish - Ek Gyan');

        //set css includes
        $view->headlink()->setStylesheet('/bootstrap/css/bootstrap.min.css');
        //add javascript files
        $view->headScript()->setFile('/bootstrap/js/jquery.min.js');
        $view->headScript()->appendFile('/bootstrap/js/bootstrap.min.js');
        //add it to the view renderer
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
                'ViewRenderer');
        $viewRenderer->setView($view);
        //Return it, so that it can be stored by the bootstrap
        return $view;
    }


     /**
      * Not sure if this is really required as it seems as though your routes are pretty standard.
      */
     protected function _initRouter()
    {
        $frontController = Zend_Controller_Front::getInstance();
        $router = $frontController->getRouter();
        $route = new Zend_Controller_Router_Route(
                 ':module/:controller/:action/*',
                 array('module' => 'admin')
              );
        $router->addRoute('default', $route);

        $usersRoute = new Zend_Controller_Router_Route_Regex(
                  ':module/:controller/:action/(?:/page/(\d+)/?)?',
                  array(
                        'module' => 'admin',
                        'controller' => 'users',
                        'action' => 'index',
                        'page' => 1,
                        ),
                  array(
                          'page' => 1,
                        )
                );

        $router->addRoute('users-index', $usersRoute);

    }
    protected function _initAuthPlugin()
    {
       $checkAuth = Zend_Controller_Front::getInstance();
       $checkAuth->registerPlugin(new Admin_Plugin_CheckAuth(Zend_Auth::getInstance()));
    }
}

application.ini 是应用程序配置中非常重要的一部分:

//truncated for example
[production]
;-------------------------------------------------------------------------------
;//PHP
;-------------------------------------------------------------------------------

phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0

;-------------------------------------------------------------------------------
;//Paths and Namespaces, paths for application resources and library
;-------------------------------------------------------------------------------

includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
autoloaderNamespaces[] = "My_"

;-------------------------------------------------------------------------------
;//Front Controller, default settings for controllers and modules
;-------------------------------------------------------------------------------

resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.frontController.moduleControllerDirectoryName = "controllers"
resources.frontController.params.prefixDefaultModule = ""
resources.modules = ""
resources.frontController.baseurl = http://example.com
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"

;-------------------------------------------------------------------------------
;//plugins, default paths for resource plugins and action helpers, can also be 
;         //accomplished in the bootstrap
;-------------------------------------------------------------------------------

pluginPaths.My_Application_Resource = APPLICATION_PATH "/../library/My/Resource"
resources.frontController.actionhelperpaths.My_Controller_Action_Helper = APPLICATION_PATH "/../library/My/Controller/Action/Helper"

;-------------------------------------------------------------------------------
;//View Settings, view settings to be shared with the view object in the bootstrap
;//               allows for easier editing.
;-------------------------------------------------------------------------------

resources.view[]=
resources.view.charset = "UTF-8"
resources.view.encoding = "UTF-8"
resources.view.doctype = "HTML5"
resources.view.language = "en"
resources.view.contentType = "text/html; charset=UTF-8"

;-------------------------------------------------------------------------------
;//Database Settings, default database adapter settings
;-------------------------------------------------------------------------------

resources.db.adapter = "pdo_Mysql"
resources.db.params.username = "user"
resources.db.params.password = "xxxx"
resources.db.params.dbname = "dbname"
resources.db.params.charset = "utf8"
resources.db.isDefaultTableAdapter = true
resources.db.params.profiler = true

;-------------------------------------------------------------------------------
;//Layouts, default path to layout files and default layout name
;-------------------------------------------------------------------------------

resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.layout.layout = layout

现在完成布局切换的最简单方法是在每个控制器的基础上切换布局,使用默认布局,除非您明确更改它:

//some controller that needs the admin layout
//note: preDispatch() and routeShutdown() are essentially the same point in the loop
public function preDispatch() {
        //switch to new layout
        $this->_helper->layout->setLayout('admin');

        //you can also manipulate the css and js similarly to the bootstrap
        $this->view->headScript()->appendFile(
        '/javascript/mediaelement/build/mediaelement-and-player.min.js');

        $this->view->inlineScript()->setScript(
            "$('audio').mediaelementplayer();");
    }

我发现如果我只需要一种或两种不同的布局,这很好用而且很简单。不是真正的DRY解决方案,但如果需要DRY解决方案,它确实可以帮助开发插件的参数。

我希望这能提供一些帮助,要记住的重要一点是模块引导程序基本上只是主引导程序的扩展,并且提供功能分离。

PS我会提供一个插件演示,但我真的很讨厌插件,对不起。

于 2013-03-30T07:09:08.080 回答