0

コントローラからの変数はビューに表示されず、ビューヘルパーは機能しません。たとえば、コントローラで変数を定義します。

$this->view->title = "Title";

ビューに印刷します。

echo $this->title;

変数「titles」の内容はページに表示されません。

これが私のブートストラップファイルです:

use ZFBootstrap\View\Helper\Navigation\Menu;

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initAutoload() {
    require_once '../application/views/helpers/LoggedInAs.php';//need to include a view-helper
    require_once '../application/plugins/AuthPlugin.php';
    require_once '../application/configs/configAcl.php';
    $frontController = Zend_Controller_Front::getInstance();
    $frontController->registerPlugin($auth = new AuthPlugin());
    $frontController->getRouter()->addDefaultRoutes();
    $autoloader = Zend_Loader_Autoloader::getInstance();
    $autoloader->setFallbackAutoloader(true);

    $this->bootstrap('layout');
    $layout=$this->getResource('layout');
    $view=$layout->getView();
    $config = new Zend_Config_Xml(APPLICATION_PATH. '/configs/navigation.xml', 'nav');
    $container = new Zend_Navigation($config);
    $view->registerHelper(new Menu(), 'menu');
    $view->navigation($container);   
    Zend_View_Helper_Navigation_HelperAbstract::setDefaultAcl($acl);
    $auth = Zend_Auth::getInstance();
    $identity = $auth->getIdentity();
     $role = ($auth->hasIdentity() && !empty($auth->getIdentity()->role))
            ? $auth->getIdentity()->role : 'guest';
   Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole($role);
    $view->navigation()->menu()->render();
    $view->navigation()->menu()->setMinDepth(1)->setUlClass('nav');
} 

代わりにこのコードを使用する場合

use ZFBootstrap\View\Helper\Navigation\Menu;

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initAutoload() {
    require_once '../application/plugins/AuthPlugin.php';
    require_once '../application/configs/configAcl.php';
    $frontController = Zend_Controller_Front::getInstance();
    $frontController->registerPlugin($auth = new AuthPlugin());
    $frontController->getRouter()->addDefaultRoutes();
    $autoloader = Zend_Loader_Autoloader::getInstance();
    $autoloader->setFallbackAutoloader(true);

    /*$this->bootstrap('layout');
    $layout=$this->getResource('layout');
    $view=$layout->getView();*/
    $config = new Zend_Config_Xml(APPLICATION_PATH. '/configs/navigation.xml', 'nav');
    $container = new Zend_Navigation($config);
    /*$view = Zend_Layout::startMvc()->getView();
    $view->registerHelper(new Menu(), 'menu');
    $view->navigation($container);//->setAcl($acl)->setRole($auth->getStorage()->read()->role);     
    */Zend_View_Helper_Navigation_HelperAbstract::setDefaultAcl($acl);
    $auth = Zend_Auth::getInstance();
    $identity = $auth->getIdentity();
     $role = ($auth->hasIdentity() && !empty($auth->getIdentity()->role))
            ? $auth->getIdentity()->role : 'guest';
   Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole($role);
    /*$view->navigation()->menu()->render();
    $view->navigation()->menu()->setMinDepth(1)->setUlClass('nav');*/
} 

ナビゲーションを除いてすべてが機能します。問題は、ナビゲーションが機能しない理由と、それを修正するにはどうすればよいかということです。

4

1 に答える 1

1

そもそもあなたBootstrap.phpは完全に混乱しています。いくつかの基本的な組織から始めます。

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

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

ブートストラップでビューオブジェクトを操作する場合は、必ず新しいビューを返す必要があります。

   /**
     * initialize the view and return it
     * @return \Zend_View
     */
    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 head meta data
        $view->headMeta()->setCharset(Zend_Registry::get('config')->resources->view->charset);
        //set css includes
        $view->headlink()->setStylesheet('/bootstrap/css/bootstrap.min.css');
        $view->headLink()->appendStylesheet('/css/main.css');
        //add javascript files
        $view->headScript()->setFile('/bootstrap/js/jquery.min.js');
        $view->headScript()->appendFile('/bootstrap/js/bootstrap.min.js');
        //add the view 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;
    }

ナビゲーションに関する限り。ブートストラップでナビゲーションコンテナを初期化できますが、実際のビューコンポーネントは通常、レイアウトでセットアップを賭けます。

   /**
     * The bootstrap part as example, yours will likely be similar but different
     */
    protected function _initNavigation() {

        $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/site.xml');
        $container = new Zend_Navigation($config);
        $registry = Zend_Registry::getInstance();
        $registry->set('Zend_Navigation', $container);
    }

次に、レイアウトファイルで、次のようなナビゲーションを実装できます。

<div id="nav">
    <?php echo $this->navigation()->menu()
               ->renderMenu(null, array(
                   'minDepth' => null,
                   'maxDepth' => 1,
                   'onlyActiveBranch' => TRUE
               )) ?>
</div>

覚えておくべき1つのこと:メソッドを呼び出すときに取得するオプションは、application.iniファイルから取得$this->getOptions()されます。多くの場合、ここで多数のオプションが初期化されます。_initRegistry()

//application.ini as example your settings will be somewhat different
[production]
;-------------------------------------------------------------------------------
;PHP
;-------------------------------------------------------------------------------

phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0

;-------------------------------------------------------------------------------
;Paths and Namespaces
;-------------------------------------------------------------------------------

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

;-------------------------------------------------------------------------------
;Front Controller
;-------------------------------------------------------------------------------

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

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

resources.view[]=
resources.view.charset = "UTF-8"
resources.view.encoding = "UTF-8"
resources.view.doctype = "HTML5"
resources.view.language = "en"

;-------------------------------------------------------------------------------
;Database Settings
;-------------------------------------------------------------------------------

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

;-------------------------------------------------------------------------------
;Layouts
;-------------------------------------------------------------------------------

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


[staging : production]


[testing : production]

phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]

phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1

あなたのページが空白で、そうすべきではないと思うなら。環境を「開発」に設定するか、「本番」セクションでエラー表示値を「0」から「1」に変更します。これを行うと、エラーが表示されます。

幸運を

于 2013-03-28T10:38:51.197 に答える