我正在学习Zend 2,并且有一个我似乎无法解决的问题。我确定这很简单,但我无法确定。我要做的就是按state_id列表记录,还可以从列表中查看记录。仅显示一个观点。当我提出/blog/view/1时,我会收到一条消息,上面写着:找不到第0。这与当前链接一起显示,如果我取消/1。因此,它甚至没有查看记录号。我怀疑这可能是路由,但不确定。谢谢这就是我所拥有的:
Module.config
'router' => array(
'routes' => array(
'blog' => array(
'type' => 'Literal',
'options' => array(
'route' => '/Blog',
'defaults' => array (
'module' => 'Blog',
'controller' => 'BlogController',
'action' => 'index',
), // defaults end
), // options end
'may_terminate' => true,
'child_routes' => array(
'list' => array(
'type' => 'segment',
'options' => array(
'route' => '/list[/:state_id]',
'defaults' => array (
'action' => 'list',
), //defaults end
'constraints' => array(
'state_id' => '[0-9]+',
) // constraints end
), // options end
), // view end
'view' => array(
'type' => 'segment',
'options' => array(
'route' => '/view[/:post_id]',
'defaults' => array(
'action' => 'view',
), // defaults end
'constraints' => array(
'post_id' => '[0-9]+',
) // constraints end
), // options end
), // post end
) // child route end
), // blog end
) // routes end
) // router end
); // return array end
模型:posttable.php
public function getPosts($post_id)
{
$post_id = (int) $post_id;
$rowset = $this->tableGateway->select(array('post_id' => $post_id));
$row = $rowset->current();
if (!$row) {
throw new Exception("Could not find row $post_id");
}
return $row;
}
查看:
<h1><?php echo $this->escapeHtml($title); ?></h1>
<table class="table">
<tr>
<th>Title</th>
<th>View</th>
<th>Comments</th>
<th>Post Date</th>
</tr>
<?php foreach ($list as $posts) : ?>
<tr>
<td><?php echo $this->escapeHtml($posts->post_title);?></td>
<td><?php echo $this->escapeHtml($posts->num_views);?></td>
<td><?php echo $this->escapeHtml($posts->num_comments);?></td>
<td><?php echo $this->escapeHtml($posts->post_date);?></td>
</tr>
<?php endforeach; ?>
编辑:06/06/2016添加了查看操作。
public function viewAction()
{
return new ViewModel(array(
'list' => $this->getPostsTable()->getPosts(),
));
}
也是GetPosttable动作:
public function getPostsTable()
{
if (!$this->postsTable) {
$sm = $this->getServiceLocator();
$this->postsTable = $sm->get('BlogModelPostsTable');
}
return $this->postsTable;
}
您需要在控制器中的getPosts()
方法中提供邮政ID。以下是根据您的PostTable
的getPosts()
方法。
public function viewAction()
{
// Get the post ID from the route, /Blog/view/1
$post_id = $this->params()->fromRoute('post_id');
$list = $this->getPostsTable()->getPosts($post_id);
$view = new ViewModel(array(
'list' => $list,
));
return $view;
}