【发布时间】:2014-11-06 14:31:24
【问题描述】:
我是使用 zf2 的新手,我正在使用专辑控制器学习教程。添加相册时,浏览器不会重定向到 /album/index,而是停留在带有空白 html 页面的相册/add (url) 上。你能告诉我会发生什么吗?我在某处忘记了一个参数?重定向真的重定向吗? 这是代码:
控制器相册:
public function addAction() {
$form = new AlbumForm();
$form->get('submit')->setValue('Add');
$request = $this->getRequest();
if ($request->isPost()) {
$album = new Album();
$form->setInputFilter($album->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$album->exchangeArray($form->getData());
$this->getAlbumTable()->saveAlbum($album);
// Redirect to list of albums
return $this->redirect()->toRoute('album',array('controller'=>'album', 'action'=>'index'));
}
}
return array('form' => $form);
}
路线:
'router' => array(
'routes' => array(
'album' => array( // album is the name of the route. Tout url localhost/album...
'type' => 'segment',
'options' => array(
'route' => '/album[/][:action][/:id]', // ...matchera l'action correspondante dans le controlleur...
'constraints' => array( // ...index est l'action par defaut si pas d'action renseignée dans l'url
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Album\Controller\Album',
'action' => 'index',
),
),
),
),
),
【问题讨论】: