Module göre layout tanımlamak – Zend Framework 2
Zend Framework 2’de Module bazlı layout tanımlamasını şu şekilde çözdüm elbette ki farklı yöntemlerde mevcut.
Benim kullandığım yöntem ise şu şekilde ;
module/Backend/Module.php dosyası içerisinde aşağıda vermiş olduğum kodu şu şekilde değiştirmeniz/eklemeniz gerekli ;
<?php
namespace Backend;
class Module
{
public function onBootstrap($e)
{
$e->getApplication()->getEventManager()->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) {
$controller = $e->getTarget();
$controllerClass = get_class($controller);
$moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));
$config = $e->getApplication()->getServiceManager()->get('config');
$routeMatch = $e->getRouteMatch();
$actionName = strtolower($routeMatch->getParam('action', 'not-found')); // get the action name
if (isset($config['module_layouts'][$moduleNamespace][$actionName])) {
$controller->layout($config['module_layouts'][$moduleNamespace][$actionName]);
}elseif(isset($config['module_layouts'][$moduleNamespace]['default'])) {
$controller->layout($config['module_layouts'][$moduleNamespace]['default']);
}
}, 100);
}
}
Daha sonra module/Backend/config/module.config.php dosyası içerisinde aşağıda vermiş olduğum değişikliği/eklemeyi yapmanız gerekiyor.
return array(
'controllers' => array(
'invokables' => array(
'Backend\Controller\Backend' => 'Backend\Controller\BackendController',
),
),
// The following section is new and should be added to your file
'router' => array(
'routes' => array(
'backend' => array(
'type' => 'segment',
'options' => array(
'route' => '/backend[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Backend\Controller\Backend',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/backend' => __DIR__ . '/../view/layout/layout.phtml',
'layout/backendEdit' => __DIR__ . '/../view/layout/backendEdit.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
'backend' => __DIR__ . '/../view',
),
),
'module_layouts' => array(
'Backend' => array(
'default' => 'layout/backend',
'edit' => 'layout/backendEdit',
)
),
);
