2009-10-23 8 views
14

Ich habe einige Codes von anderen Artikeln für die Konfiguration von Modul und Layout in Zend Framework. Ich habe es in meinem Lokal versucht. Ich habe kein anderes Layout für Standard und Admin Modul bekommen. Hier ist mein Code zum Konfigurieren von Modul und Layout für zend Framework.Modul Konfiguration und Layout Konfiguration in 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(); 
    } 
} 

Bitte durch sie gehen und lassen Sie mich wissen, jeder weiß, wie konfigurieren Sie Modul und das Layout in der richtigen Art und Weise ..

Danke und Gruß,

Prasanth P

+0

Ich verstehe nicht .. warum Sie Jungs für diesen Beitrag abstimmen ?. Dieser Beitrag ist fast auf Google-Suche getroffen sowie von den meisten Menschen überprüft !! .. –

+0

Wenn die Antworten falsch sind, bitte stimmen Sie nur für Antworten und nicht für die Post. Danke .. –

Antwort

2

Sie müssen verwenden ein Controller-Plugin, um dies zu erreichen, da das Layout auf der Basis des Anfrageeintrags festgelegt wurde und auf dem Bootstrap die Anwendung nicht ausgelöst wurde. Daher müssen Sie ein Controller-Plugin verwenden, um mit preDispatch zu arbeiten, um Layouts zu wechseln.

8

aus dem Code:

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

Sie your_app/modules/admin/Layout/admin.phtml als Admin-Modul-Layout verwenden, und ich denke, es your_app/Layout/layout.phtml ersetzt. Überprüfen Sie einen Weg, um zwischen den Modulen zu wechseln, und versuchen Sie etwas site.ressources.layout anstelle von resources.layout.layout. Ich bin ein Neuling, um zu zend. überprüfen, wie zum Einrichten Sie bei http://www.survivethedeepend.com/

das gleiche Problem und Lösung wurde hier betont Bootstrap: http://blog.astrumfutura.com/archives/415-Self-Contained-Reusable-Zend-Framework-Modules-With-Standardised-Configurators.html

24

Ich benutze Plugin Ansatz mit diesem Code habe ich geschrieben:

in Haupt Bootstrap:

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

in der Plugin-Verzeichnis:

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

Hoffe es hilft.

+0

Wo lege ich diese Datei ab? –

+0

Sie müssen auch Zend_Layout :: startMvc() 'in Ihrem Bootstrap aufrufen, bevor Sie diese Datei einbinden – HorusKol

+0

Bitte nicht, dass Sie den Pfad zum Modulverzeichnis mit' Zend_Controller_Front :: getInstance() -> getModuleDirectory() ' – Kirzilla

2

Ich denke, der einfachste Weg ist, überprüfen Sie den URI_String. Siehe unten:

Ich habe ein Modul namens "admin". Unter Layout Ordner habe ich 2 Verzeichnisse. "site" und "admin"

\ application \ layout \ site \ layout.phtml und \ application \ layout \ admin \ layout.phtml

diesen Codeblock auf Bootstrap.php hinzufügen Es ist einfach Ändern Sie den Layoutverzeichnispfad.

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

Das hat nicht für mich funktioniert –

+1

Es wird eine schlechte Idee sein, wenn Sie einige benutzerfreundliche Umschreibungen, z. www.mysite.com/user-admin.html –

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

} 
+0

Konnten Sie eine kleine Erklärung anbieten? – cereallarceny

1

Ihre Fragen beantwortet meine Frage, das ist richtig, ich habe versucht, herauszufinden, warum es nicht in meiner Bootstrap-Module funktionierten, in der Konfigurationsdatei zu sehen, dass Sie die Zeile

hinzufügen müssen

administrador.resources.view [] =

Valew Partner!

6

In meiner Anwendung habe ich diesen Weg konfiguriert. Es hat perfekt funktioniert.

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

-Layout und Modul nicht auf ein neu zend Projekt freigegeben (in ZF-Version 1). Es muss aktiviert werden und Sie müssen es funktionieren lassen.

Layout funktioniert für die gemeinsame Kopf-und Fußzeile für die Arbeit zend Projekt, auf der anderen Seite Modul kann für die verschiedenen Arten von Zugriff verwendet werden, d. H. Modul für Benutzer, Modul für Admin, Modul für Besucher und so weiter.

Für eine kurze Referenz können Sie eine vollständige Erklärung mit einem vollständigen Projekt finden, um die Grundidee von hier auf meiner Website zu erhalten. . http://www.getallthing.com/how-to-use-layout-and-module-in-zend-framework/

Viel Glück und Prost!

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

Ich versuchte ein paar andere Lösungen von SOF und dieses funktionierte großartig. Nur benötigt, um den layoutPath auf den Ordner der tatsächlichen Layouts zu zeigen

Verwandte Themen