Zend framework2 视图模块异常策略

Zend framework2 视图模块异常策略,zend-framework2,Zend Framework2,我有一个保存一些数据的控制器 $pat = $sm->get('Tables\PaymentAttemptsTable'); $pat->save($post); 模块配置具有以下配置: public function onBootstrap(EventInterface $e) { $em = $e->getApplication()->getEventManager(); $em->attach('dispatch', array($this

我有一个保存一些数据的控制器

$pat = $sm->get('Tables\PaymentAttemptsTable');
$pat->save($post);
模块配置具有以下配置:

public function onBootstrap(EventInterface $e)
{
    $em  = $e->getApplication()->getEventManager();
    $em->attach('dispatch', array($this, 'loadConfiguration' ), 100);
}

public function loadConfiguration(EventInterface $e)
{
    $sm  = $e->getApplication()->getServiceManager();

    //if this module
    $exceptionstrategy = $sm->get('ViewManager')->getExceptionStrategy();
    $exceptionstrategy->setExceptionTemplate('error/inserterror');
}
关于PaymentAttemptsTable模块confi,我有类似的策略

public function onBootstrap(EventInterface $e)
{
    $eventManager        = $e->getApplication()->getEventManager();
    $eventManager->attach('dispatch', array($this, 'loadConfiguration' ), 100);
}

public function loadConfiguration(EventInterface $e)
{
    $sm  = $e->getApplication()->getServiceManager();

    //if this module
    $exceptionstrategy = $sm->get('ViewManager')->getExceptionStrategy();
    $exceptionstrategy->setExceptionTemplate('error/saveerror');
}
return array(

'view_manager' => array(
    'exception_template'       => 'error/index',
    'template_map' => array(
        'error/index'             => __DIR__ . '/../view/error/index.phtml',
    ),
    'template_path_stack' => array(
        __DIR__ . '/../view',
    ),
),
在每一张照片上,我都有这样一张照片

public function onBootstrap(EventInterface $e)
{
    $eventManager        = $e->getApplication()->getEventManager();
    $eventManager->attach('dispatch', array($this, 'loadConfiguration' ), 100);
}

public function loadConfiguration(EventInterface $e)
{
    $sm  = $e->getApplication()->getServiceManager();

    //if this module
    $exceptionstrategy = $sm->get('ViewManager')->getExceptionStrategy();
    $exceptionstrategy->setExceptionTemplate('error/saveerror');
}
return array(

'view_manager' => array(
    'exception_template'       => 'error/index',
    'template_map' => array(
        'error/index'             => __DIR__ . '/../view/error/index.phtml',
    ),
    'template_path_stack' => array(
        __DIR__ . '/../view',
    ),
),
))

问题是当我这样做时

throw new SaveError('Table must be a string or instance of TableIdentifier.');

在PaymentAttemptsTable类中,我从控制器获得模板,而不是表格类,是否有办法解决此问题?

如果查看

它向您展示了如何加载不同的视图模板,您需要做的是将视图模板更改为需要在PaymentAttemptsTable类中加载的模板。这将需要在调用控制器中完成

来自Zend.com的示例

namespace Foo\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class BazBatController extends AbstractActionController
{
    public function doSomethingCrazyAction()
    {
        $view = new ViewModel(array(
            'message' => 'Hello world',
        ));
        $view->setTemplate('foo/baz-bat/do-something-crazy');
        return $view;
    }
}

嗨,我认为这对于普通的视图模板来说很好,但我需要的是一个异常策略,当一个异常使用某个模板时。啊,我明白了,很抱歉我不能帮你。尽管如果在try-catch块中执行此操作,您可以在catch中应用模板更改。我知道这不是一个很好的方法,但我甚至不确定是否有一个内在的方法来实现这一点。