Zend 2 module.config 具有约束 model/controller 未检索记录
Zend 2 module.config with constraints with model/controller not retrieveing record
我正在学习 Zend 2 并且有一个问题我似乎无法解决。我确定这很简单,但我无法确定它。我想做的是按 state_id 列出记录,并查看列表中的记录。仅显示其中一个视图。当我调出 /Blog/view/1 时,我收到一条消息:找不到第 0 行。这会显示当前的 link 并且如果我关闭 /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; ?>
编辑:2016 年 6 月 6 日添加了查看操作。
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('Blog\Model\PostsTable');
}
return $this->postsTable;
}
您需要将 post ID 提供给控制器中的 getPosts()
方法。以下是根据你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;
}
我正在学习 Zend 2 并且有一个问题我似乎无法解决。我确定这很简单,但我无法确定它。我想做的是按 state_id 列出记录,并查看列表中的记录。仅显示其中一个视图。当我调出 /Blog/view/1 时,我收到一条消息:找不到第 0 行。这会显示当前的 link 并且如果我关闭 /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; ?>
编辑:2016 年 6 月 6 日添加了查看操作。
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('Blog\Model\PostsTable');
}
return $this->postsTable;
}
您需要将 post ID 提供给控制器中的 getPosts()
方法。以下是根据你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;
}