2013-05-01 55 views
0

我有一個控制器可以保存一些數據。查看模塊異常策略

$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'); 
} 

在每一個我都有這樣的看法confi。

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', 
    ), 
), 

);

的事情是,當我在PaymentAttemptsTable類做

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

我從控制器獲得的模板,並不會形成表類,是有辦法解決這一問題?

回答

0

如果你看看 http://framework.zend.com/manual/2.0/en/modules/zend.view.quick-start.html

控制器和視圖模型部分它表明你如何加載不同的視圖模板,你需要做的是視圖模板更改爲需要加載的一個在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; 
    } 
} 
+0

例子,我認爲這是罰款正常視圖模板,但我需要的是一個異常的策略,即當異常平米使用某些模板。 – gastoncs 2013-05-01 22:30:07

+0

啊,我明白了,我很抱歉,我無法幫助你。儘管如果你在try catch塊中做了它,你可以在catch中應用模板更改。我知道的不是一個很整潔的方式,但我甚至不確定有內置的方法來實現這一點。 – mic 2013-05-02 07:17:51