Gre*_*een 5 php zend-framework-mvc zend-view http-status-code-404 zend-framework2
默认情况下,页面在Applicationmodule.config数组中设置如下:
'template_map' => array(
'error/404' => __DIR__ . '/../view/error/404.phtml'
Run Code Online (Sandbox Code Playgroud)
我想改变页面.我想要新ViewModel的变量充满变数.这意味着仅仅更改模板是不够的:
'error/404' => __DIR__ . '/../view/error/my_new_404_template.phtml'
Run Code Online (Sandbox Code Playgroud)
但我无法理解如何制作它.我无法看到请求的方式'error/404'.
如何ViewModel为它创造新的?
如何将变量附加到它?
如何通过路线来'error/404'改变它?
例如,我有这样的'error/404'页面方法:
public function pageNotFoundAction() {
$view = new ViewModel();
$view->setTemplate('error/404'); // set my template
$sm = $this->getServiceLocator()->get('SessionManager');
$cont = new Container('SomeNamespace', $sm);
$view->var1 = $cont->offsetGet('someValue1'); // the "error/404" template
$view->var2 = $cont->offsetGet('someValue2'); // is full of variables
$view->var3 = $cont->offsetGet('someValue3');
$view->var4 = "One more view variable";
// And now I return it somewhere and want it to be called
// in case of "the page is not found"
return $view;
}
Run Code Online (Sandbox Code Playgroud)
如何做出这样的改变?我无法得到他们创建的系统来处理类似的事情'error/404'.请帮忙.
UPD 1
更复杂的任务.怎么有几'error/404'页?想问一下创建框架的人是否知道'not_found_template'不能有一个'error/404'页面数组 .它怎么样?如果我像这样设置此选项:
'template_map' => array(
'error/404' => __DIR__ . '/../view/error/404.phtml',
'my-page/one_more_error404' => __DIR__ . '/../view/my-page/my-page/one_more_error404.phtml',
'other_page/second_404' => __DIR__ . '/../view/other-page/one_more_error404.phtml',
'not_found_template' => array(
'error/404',
'my-page/one_more_error404',
'other-page/second_404',
);
Run Code Online (Sandbox Code Playgroud)
它会抛出一个错误.'not_found_template'强迫你只有一个'error/404'模板?
还有另一种方式.赶上EVENT_DISPATCH_ERROR并完全重建viewModel.洞穴是布局 - 是根viewModel,默认情况下附加到布局的内容是另一个viewModel(子).官方文档中没有明确说明这些要点.
以下是它在您的样子中的样子Module.php:
public function onBootstrap(MvcEvent $event)
{
$app = $event->getParam( 'application' );
$eventManager = $app->getEventManager();
/** attach Front layout for 404 errors */
$eventManager->attach( MvcEvent::EVENT_DISPATCH_ERROR, function( MvcEvent $event ){
/** here you can retrieve anything from your serviceManager */
$serviceManager = $event->getApplication()->getServiceManager();
$someVar = $serviceManager->get( 'Some\Factory' )->getSomeValue();
/** here you redefine layout used to publish an error */
$layout = $serviceManager->get( 'viewManager' )->getViewModel();
$layout->setTemplate( 'layout/start' );
/** here you redefine template used to the error exactly and pass custom variable into ViewModel */
$viewModel = $event->getViewModel();
$viewModel->setVariables( array( 'someVar' => $someVar ) )
->setTemplate( 'error/404' );
});
}
Run Code Online (Sandbox Code Playgroud)
我不确定我是否遵循您想要实现的目标,您能否给出一个明确的例子来说明您想要做什么?
如果您只是尝试将变量添加到传递的视图模型中,您甚至可以在控制器中执行此操作,请查看AbstractActionController
/**
* Action called if matched action does not exist
*
* @return array
*/
public function notFoundAction()
{
$response = $this->response;
$event = $this->getEvent();
$routeMatch = $event->getRouteMatch();
$routeMatch->setParam('action', 'not-found');
if ($response instanceof HttpResponse) {
return $this->createHttpNotFoundModel($response);
}
return $this->createConsoleNotFoundModel($response);
}
/**
* Create an HTTP view model representing a "not found" page
*
* @param HttpResponse $response
* @return ViewModel
*/
protected function createHttpNotFoundModel(HttpResponse $response)
{
$response->setStatusCode(404);
// Add in extra stuff from your ServiceLocator here...
// $viewModel->setTemplate(..);
return new ViewModel(array(
'content' => 'Page not found',
));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12059 次 |
| 最近记录: |