如何在 ZEND 2 中为新模块设置新布局?
How do I set up a new layout to a new module in ZEND 2?
谁能帮我设置 administration 模块的布局和 application 模块的布局? .在下图中,您可以看到我的文件夹结构:
这是我的 module.config.php 来自管理模块的内容:
<?php
return array(
'controllers' => array(
'invokables' => array(
'Administration\Controller\Admin' => 'Administration\Controller\AdminController',
),
),
// The following section is new and should be added to your file
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/administration[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Administration\Controller\Admin',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
//'base_path' => 'http://www.attila-naghi.com/',
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout2.phtml',
// 'administration/admin/index' => __DIR__ . '/../view/administration/admin/index.phtml',
// 'error/404' => __DIR__ . '/../view/error/404.phtml',
// 'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => 数组(
目录。 '/../看法',
),
),
)
?>
在这里我设置了布局。但出于某种原因,如果我访问 application 模块,它会从 administration 模块加载布局。为什么 ?
这是来自 application 模块的 module.config.php 文件的内容:
return array(
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '[:controller[/:action]][/:param1]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
),
'defaults' => array(
'action' => 'index',
'__NAMESPACE__' => 'Application\Controller',
// 'param1' => 'tralala'
)
)
)
)
),
),
),
'service_manager' => array(
'abstract_factories' => array(
'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
'Zend\Log\LoggerAbstractServiceFactory',
),
'aliases' => array(
'translator' => 'MvcTranslator',
),
),
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController',
'Application\Controller\Create' => 'Application\Controller\CreateController',
'Application\Controller\Blog' => 'Application\Controller\BlogController',
'Application\Controller\Portofolio' => 'Application\Controller\PortofolioController',
'Application\Controller\User' => 'Application\Controller\UserController',
),
),
'view_manager' => array(
'base_path' => 'http://www.attila-naghi.com/',
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
// Placeholder for console routes
'console' => array(
'router' => array(
'routes' => array(
),
),
),
);
这是 application.config.file 内容:
return array(
// This should be an array of module namespaces used in the application.
'modules' => array(
'Application',
'Administration'
),
......
您必须从控制器更改布局。只需在 ViewModel
上方指定此代码
$this->layout('administration/admin/index');
所有模块的配置合并为一个配置。最后加载的模块将覆盖第一个模块的布局。您可以使用下面的模块来设置每个模块的布局。
我最近找到了一种方法(别人的解决方案)。
将此添加到您的 module.config.php(在我的例子中,该模块称为 Album,这是基于 ZF2 的演示应用程序):
'module_layouts' => array(
'Album' => 'layout/layout.phtml'
),
需要在主 Module.php 文件中完成其他必要的更改(在其中添加此 onBootstrap 方法并根据需要进行编辑):
public function onBootstrap($e) {
$e->getApplication()
->getEventManager()
->getSharedManager()
->attach('Zend\Mvc\Controller\AbstractController', 'dispatch',
function($e) {
$controller = $e->getTarget();
$controllerClass = get_class($controller);
$moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\'));
$config = $e->getApplication()->getServiceManager()->get('config');
if (isset($config['module_layouts'][$moduleNamespace])) {
$controller->layout($config['module_layouts'][$moduleNamespace]);
}
}, 100);
}
这里是我对不同布局的要求。这不是我的主意,但由于我找不到源代码,所以我将 post 代码放在这里。如果有人知道,请在评论中添加 URL,我会将其包含在答案中。如果你阅读了我在另一个问题中给你的资源和上面的答案,我相信你会理解这里发生的事情。
Module.php
使用Zend\ModuleManager\ModuleManager;
> > public function init(ModuleManager $moduleManager)
> {
> $sharedEvents = $moduleManager->getEventManager()->getSharedManager();
> $sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) {
> // This event will only be fired when an ActionController under the MyModule namespace is dispatched.
>
> $controller = $e->getTarget();
$controller->layout('backofficeLayout');
>
> }, 100);
>
>
>
> }
module.config.php
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_path_stack' => array(
'backoffice' => __DIR__ . '/../view',
),
'template_map' => array(
'backofficeLayout' => __DIR__ . '/../view/layout/myaccount-backoffice.phtml',))
我刚刚注意到,最好为布局文件夹中的每个布局使用自定义名称。不要使用 layout.phtml,而是使用 layout-mymodulename.phtml。它与之前突出显示的要点一起工作得更好。
谁能帮我设置 administration 模块的布局和 application 模块的布局? .在下图中,您可以看到我的文件夹结构:
这是我的 module.config.php 来自管理模块的内容:
<?php
return array(
'controllers' => array(
'invokables' => array(
'Administration\Controller\Admin' => 'Administration\Controller\AdminController',
),
),
// The following section is new and should be added to your file
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/administration[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Administration\Controller\Admin',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
//'base_path' => 'http://www.attila-naghi.com/',
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout2.phtml',
// 'administration/admin/index' => __DIR__ . '/../view/administration/admin/index.phtml',
// 'error/404' => __DIR__ . '/../view/error/404.phtml',
// 'error/index' => __DIR__ . '/../view/error/index.phtml',
), 'template_path_stack' => 数组( 目录。 '/../看法', ), ), ) ?> 在这里我设置了布局。但出于某种原因,如果我访问 application 模块,它会从 administration 模块加载布局。为什么 ? 这是来自 application 模块的 module.config.php 文件的内容:
return array(
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '[:controller[/:action]][/:param1]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
),
'defaults' => array(
'action' => 'index',
'__NAMESPACE__' => 'Application\Controller',
// 'param1' => 'tralala'
)
)
)
)
),
),
),
'service_manager' => array(
'abstract_factories' => array(
'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
'Zend\Log\LoggerAbstractServiceFactory',
),
'aliases' => array(
'translator' => 'MvcTranslator',
),
),
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController',
'Application\Controller\Create' => 'Application\Controller\CreateController',
'Application\Controller\Blog' => 'Application\Controller\BlogController',
'Application\Controller\Portofolio' => 'Application\Controller\PortofolioController',
'Application\Controller\User' => 'Application\Controller\UserController',
),
),
'view_manager' => array(
'base_path' => 'http://www.attila-naghi.com/',
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
// Placeholder for console routes
'console' => array(
'router' => array(
'routes' => array(
),
),
),
);
这是 application.config.file 内容:
return array(
// This should be an array of module namespaces used in the application.
'modules' => array(
'Application',
'Administration'
),
......
您必须从控制器更改布局。只需在 ViewModel
上方指定此代码$this->layout('administration/admin/index');
所有模块的配置合并为一个配置。最后加载的模块将覆盖第一个模块的布局。您可以使用下面的模块来设置每个模块的布局。
我最近找到了一种方法(别人的解决方案)。 将此添加到您的 module.config.php(在我的例子中,该模块称为 Album,这是基于 ZF2 的演示应用程序):
'module_layouts' => array(
'Album' => 'layout/layout.phtml'
),
需要在主 Module.php 文件中完成其他必要的更改(在其中添加此 onBootstrap 方法并根据需要进行编辑):
public function onBootstrap($e) {
$e->getApplication()
->getEventManager()
->getSharedManager()
->attach('Zend\Mvc\Controller\AbstractController', 'dispatch',
function($e) {
$controller = $e->getTarget();
$controllerClass = get_class($controller);
$moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\'));
$config = $e->getApplication()->getServiceManager()->get('config');
if (isset($config['module_layouts'][$moduleNamespace])) {
$controller->layout($config['module_layouts'][$moduleNamespace]);
}
}, 100);
}
这里是我对不同布局的要求。这不是我的主意,但由于我找不到源代码,所以我将 post 代码放在这里。如果有人知道,请在评论中添加 URL,我会将其包含在答案中。如果你阅读了我在另一个问题中给你的资源和上面的答案,我相信你会理解这里发生的事情。
Module.php
使用Zend\ModuleManager\ModuleManager;
> > public function init(ModuleManager $moduleManager)
> {
> $sharedEvents = $moduleManager->getEventManager()->getSharedManager();
> $sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) {
> // This event will only be fired when an ActionController under the MyModule namespace is dispatched.
>
> $controller = $e->getTarget();
$controller->layout('backofficeLayout');
>
> }, 100);
>
>
>
> }
module.config.php
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_path_stack' => array(
'backoffice' => __DIR__ . '/../view',
),
'template_map' => array(
'backofficeLayout' => __DIR__ . '/../view/layout/myaccount-backoffice.phtml',))
我刚刚注意到,最好为布局文件夹中的每个布局使用自定义名称。不要使用 layout.phtml,而是使用 layout-mymodulename.phtml。它与之前突出显示的要点一起工作得更好。