2009-10-23 32 views
14

Tôi nhận được một số mã từ các bài viết khác để định cấu hình mô-đun và bố cục trong khung công tác zend. Tôi đã thử với địa phương của tôi. tôi đã không nhận được bố trí khác nhau cho module mặc định và admin. Đây là mã của tôi để cấu hình mô-đun và bố cục cho khung công tác zend.Cấu hình mô-đun và cấu hình bố cục trong khung công tác zend

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

hãy đi qua nó và cho tôi biết bất kỳ biết làm thế nào để cấu hình mô-đun và bố trí đúng cách ..

Trân trọng cảm ơn,

Prasanth P

+0

Tôi không hiểu .. tại sao các bạn lại bỏ phiếu cho bài đăng này ?. Bài đăng này gần như được nhấn vào tìm kiếm của google cũng như được xem xét bởi hầu hết mọi người !! .. –

+0

Nếu câu trả lời sai, vui lòng bỏ phiếu để chỉ trả lời và không cho bài đăng. Cảm ơn .. –

Trả lời

2

Bạn cần phải sử dụng một Controller Plugin để đạt được điều đó, bởi vì layout được thiết lập dựa trên mục yêu cầu, và trên bootstrap ứng dụng chưa được gửi đi, vì vậy bạn cần sử dụng một plugin điều khiển để làm việc trên preDispatch để chuyển bố trí.

8

Từ mã của bạn:

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

bạn đang sử dụng your_app/modules/admin/layouts/admin.phtml như bố trí mô-đun admin, và tôi đoán nó thay thế your_app/layouts/layout.phtml. Kiểm tra một cách để chuyển đổi giữa các mô-đun và thử một cái gì đó site.ressources.layout thay vì resources.layout.layout. tôi là một newbie để zend. kiểm tra làm thế nào để thiết lập bạn bootstrap tại http://www.survivethedeepend.com/

cùng một vấn đề và giải pháp đã được nhấn mạnh ở đây: http://blog.astrumfutura.com/archives/415-Self-Contained-Reusable-Zend-Framework-Modules-With-Standardised-Configurators.html

24

tôi sử dụng cách tiếp cận plugin với mã này tôi đã viết:

trong Bootstrap chính:

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

Trong thư mục plugin:

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

Hy vọng điều đó sẽ hữu ích.

+0

Tôi nên đặt tệp này ở đâu? –

+0

Bạn cũng cần phải gọi 'Zend_Layout :: startMvc()' trong bootstrap của bạn trước khi bao gồm tệp này – HorusKol

+0

Xin vui lòng không phải là bạn có thể lấy đường dẫn đến thư mục của mô-đun bằng cách sử dụng 'Zend_Controller_Front :: getInstance() -> getModuleDirectory()' – Kirzilla

2

Tôi nghĩ cách dễ nhất là kiểm tra URI_String. Vui lòng xem bên dưới:

Tôi có một mô-đun có tên là "quản trị viên". Trong thư mục bố cục, tôi có 2 thư mục. "trang web" và "admin"

\ ứng dụng \ layout \ site \ layout.phtml và \ ứng dụng \ layout \ admin \ layout.phtml

Thêm khối mã này trên Bootstrap.php Nó chỉ thay đổi đường dẫn thư mục layout.

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

Điều đó không hiệu quả đối với tôi –

+1

Sẽ là ý tưởng tồi nếu bạn muốn thêm một số ghi đè thân thiện với người dùng, ví dụ: 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

Bạn có thể giải thích một chút không? – cereallarceny

1

câu hỏi của bạn đã trả lời câu hỏi của tôi, đúng vậy, tôi đã cố gắng để tìm hiểu tại sao nó không làm việc trong các module bootstrap của tôi, nhìn thấy trong tập tin cấu hình của nó mà bạn cần phải thêm dòng

administrador.resources.view [] =

Đối tác valew!

6

Trong ứng dụng của tôi, tôi đã định cấu hình theo cách này. Nó hoạt động hoàn hảo.

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 và module trong không được kích hoạt trên một dự án mới Zend (trong ZF phiên bản 1). Nó cần phải được kích hoạt và bạn cần phải làm cho nó hoạt động.

Bố cục bố cục cho chân đầu trang và chân trang chung cho dự án zend đang hoạt động, có thể sử dụng mô-đun khác cho loại truy cập khác nhau cho người dùng, mô-đun cho quản trị viên, mô-đun cho khách truy cập v.v.

Để tham khảo nhanh, bạn có thể tìm thấy giải thích đầy đủ với dự án hoàn chỉnh để lấy ý tưởng cơ bản từ đây, trên trang web của tôi. . http://www.getallthing.com/how-to-use-layout-and-module-in-zend-framework/

Chúc bạn may mắn và chúc mừng!

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

Đã thử một vài giải pháp khác từ SOF và công cụ này hoạt động tốt. Chỉ cần để trỏ layoutPath vào thư mục của bố cục thực tế

Các vấn đề liên quan