我想在我的网站中使用REST控制器映射到类似于以下内容的路由:
GET /article
GET /article/1
GET /article/create
GET /article/1/update
GET /article/1/delete
POST /article
PUT /article/1
DELETE /article/1
我的控制器是:
namespace Article\Controller;
// use Zend\Mvc\Controller\AbstractActionController;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\ViewModel;
use Article\Form\ArticleForm;
use Article\Model\Article;
class ArticleController extends AbstractRestfulController
{
protected $articleTable;
public function getList()
{
return new ViewModel(array(
'articles' => $this->getArticleTable()->fetchAll(),
));
}
public function get($id)
{
return new ViewModel(array(
'article' => $this->getArticleTable()->getArticle($id),
));
}
public function create()
{
$form = new ArticleForm();
$form->get('submit')->setValue('Add');
$request = $this->getRequest();
if ($request->isPost()) {
$article = new Article();
$form->setInputFilter($article->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$article->exchangeArray($form->getData());
$this->getArticleTable()->saveArticle($article);
// Redirect to list of articles
return $this->redirect()->toRoute('article');
}
}
return array('form' => $form);
}
public function update()
{
//...
}
public function delete()
{
//...
}
public function getArticleTable()
{
if (!$this->articleTable) {
$sm = $this->getServiceLocator();
$this->articleTable = $sm->get('Article\Model\ArticleTable');
}
return $this->articleTable;
}
}
我的module.config.php路由如下:
'router' => array(
'routes' => array(
'article' => array(
'type' => 'segment',
'options' => array(
'route' => '/article[/:id]',
'constraints' => array(
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Article\Controller\Article',
),
),
),
使用AbstractRestfulController
我可以实现前两个GET,POST和DELETE;但是,我不知道如何获得以下路线(将呈现为表格):
GET /article/create .. maps to ArticleController::create
GET /article/1/update .. maps to ArticleController::update
GET /article/1/delete .. maps to ArticleController::delete
我在文档中读到:“此外,您可以将”action“方法映射到AbstractRestfulController,就像在AbstractActionController中一样;这些方法将以”Action“为后缀,将它们与上面列出的RESTful方法区分开来。这允许您执行诸如提供用于提交到各种RESTful方法的表单或将RPC方法添加到RESTful API之类的操作。“ ..但我看不到一个例子,我尝试添加一个createAction但找不到路由。我想我的路由器配置需要修改吗?