[英]Zend 2: How to configure errors with custom controller?
我的問題是關於自定義Zend 2中如何處理錯誤。
假設我想自定義布局,以便在控制器中的一個操作中做到這一點:
$layout = $this->layout();
$myNav = new ViewModel(array('nav' => $this->getNav());
$myNav->setTemplate('layout/nav');
$layout->addChild($myNav, 'navigation');
當我將其放入控制器進行常規(即非404)查看時,效果很好。 現在,我已經自定義布局,以便可以執行<?php echo $this->navigation; ?>
<?php echo $this->navigation; ?>
然后啟動layout/nav.phtml
,一切都正常進行。
現在,假設出現錯誤時我想做完全相同的事情。 我需要能夠在錯誤處理程序將其自身的ViewModel(...)
到error/404.phtml
模板之前以某種方式注入上述代碼。
你是怎樣做的?
我懷疑這就像在module.config.php
為服務管理器設置正確的類:
'service_manager' => array(
'services' => array(
'error_handler' => 'MyModule\Controller\MyCustomErrorController'
//and so on...
我該怎么做呢?
更新:
在我的Module.php
我附加了MvcEvent::EVENT_DISPATCH_ERROR
的方法。 變體A有效,而變體B不起作用。 因此,您不能在此處使用局部變量? 我錯過了一些基本的東西嗎?
變體A
public function onDispatchError(MvcEvent $event)
{
$sm = $event->getApplication()->getServiceManager();
$vm = $event->getViewModel();
$vm->setVariable('nav', '<h1>test do i work?</h1>');
//Works
}
變體B
public function onDispatchError(MvcEvent $event)
{
$sm = $event->getApplication()->getServiceManager();
$vm = $event->getViewModel();
$nav = new ViewModel(array('test'=>'hello there'));
$nav->setTemplate('layout/simpletest');//contents: <?php echo $this->test; ?>
$vm->addChild($nav, 'nav');
//In the template, <?php echo $this->nav; ?> has nothing...
}
Zf2使用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_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',
),
這應該處理4xx客戶端錯誤和5xx服務器錯誤。
對於特定模塊中的自定義錯誤頁面。
namespace ModuleName;
use Zend\ModuleManager\Feature\BootstrapListenerInterface;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\Mvc\MvcEvent;
class Module implements
BootstrapListenerInterface,
AutoloaderProviderInterface,
ConfigProviderInterface
{
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$eventManager->attach('dispatch', array($this, 'loadConfiguration' ), 100);
}
public function loadConfiguration(MvcEvent $e)
{
$sm = $e->getApplication()->getServiceManager();
$controller = $e->getRouteMatch()->getParam('controller');
if (0 !== strpos($controller, __NAMESPACE__, 0)) {
//if not this module
return;
}
//if this module
$exceptionstrategy = $sm->get('ViewManager')->getExceptionStrategy();
$exceptionstrategy->setExceptionTemplate('error/errorcustom');
}
public function getAutoloaderConfig(){ /* common code */ }
public function getConfig(){ /* common code */}
}
該解決方案由http://samsonasik.wordpress.com/2012/09/19/zend-framework-2-create-custom-error-page/中的 “ samsonasik”提供
您可以附加一個偶數來處理觸發404時發生的情況:
Module.php
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
/**
* Log any Uncaught Errors
*/
$sharedManager = $e->getApplication()->getEventManager()->getSharedManager();
$sm = $e->getApplication()->getServiceManager();
$sharedManager->attach('Zend\Mvc\Application', 'dispatch.error',
function($e) use ($sm) {
/**
* Decide what to do now we've got a problem...
* Log the issue etc..
* You could forward to some custom controller if you wanted..
*/
//$sm->get('Zend\Log\Logger')->crit('an error occurred... bla');
$controller = $e->getTarget();
//$routeMatch = $e->getRouteMatch();
$controller->layout('somelayout'); // possibly change the layout..
}
);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.