【发布时间】:2015-04-29 05:32:17
【问题描述】:
我很难使用 ZF2 路由和分页。 我有这个路由配置:
'router' => array(
'routes' => array(
'website' => array(
'type' => 'Hostname',
'options' => array(
'route' => '[:subdomain]foo.local',
'constraints' => array(
'subdomain' => '[www.|test.]'
),
'defaults' => array(
'controller' => 'Frontend\Controller\Index',
'action' => 'index',
'subdomain' => 'www.'
),
),
'may_terminate' => true,
'child_routes' => array(
'homepage' => array(
'type' => 'segment',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Frontend\Controller\Index',
'action' => 'index',
),
),
),
'structures' => array(
'type' => 'segment',
'options' => array(
'route' => '/structures[/:action]',
'defaults' => array(
'controller' => 'Frontend\Controller\Structures',
'action' => 'index',
),
),
),
[...]
'frontend_blog' => array(
'type' => 'segment',
'options' => array(
'route' => '/blog[/:page]',
'constraints' => array(
'page' => '[0-9]+'
),
'defaults' => array(
'controller' => 'Frontend\Controller\Blog',
'action' => 'index',
),
),
),
'frontend_single' => array(
'type' => 'segment',
'options' => array(
'route' => '/blog/view/[:id]',
'defaults' => array(
'controller' => 'Frontend\Controller\Blog',
'action' => 'view',
),
),
),
'frontend_bycategory' => array(
'type' => 'segment',
'options' => array(
'route' => '/blog/category/[:id]',
'defaults' => array(
'controller' => 'Frontend\Controller\Blog',
'action' => 'category',
),
),
),
'frontend_news' => array(
'type' => 'segment',
'options' => array(
'route' => '/news[/:page]',
'constraints' => array(
'page' => '[0-9]+'
),
'defaults' => array(
'controller' => 'Frontend\Controller\News',
'action' => 'index',
),
),
),
[...]
),
),
),
),
也就是说,我希望 foo.local 可以作为 www.foo.local、test.foo.local 或 foo.local 使用:实际上它工作正常。困难的部分带有分页视图助手:如果我这样使用它:
<?php echo $this->paginationControl($this->list, 'Sliding', 'frontend/pagination_control', array('route' => 'website/frontend_blog')); ?>
它总是输出为链接 foo.local/blog/2,而如果我访问 www.foo.local/blog 或 test.foo.local/blog,我希望它保留 www 或测试。 我做错了什么?
也许有助于解释我的移动路由配置几乎相同,这就是我使用“网站”子路由容器的方式。 谢谢。 A.
@Crisp: 这里是frontend_paginationcontrol的内容:
<?php if ($this->pageCount > 1): ?>
<nav id="paginazione">
<ul>
<?php if (isset($this->previous)): ?>
<li><a href="<?php echo $this->url($this->route, array('page' => $this->previous), array(), true); ?>">‹</a></li>
<?php else: ?>
<li><a href="#" class="disabilitato">‹</a></li>
<?php endif; ?>
<li><a href=""><?php echo $this->current; ?></a></li>
<li>di</li>
<li><a href=""><?php echo $this->pageCount ?></a></li>
<?php if (isset($this->next)): ?>
<li><a href="<?php echo $this->url($this->route, array('page' => $this->next), array(), true); ?>">›</a></li>
<?php else: ?>
<li><a href="#" class="disabilitato">›</a></li>
<?php endif; ?>
</ul>
</nav>
我按照你的建议尝试了但没有改变结果:(
更新:实际上问题似乎是由设置子域的默认值引起的,使用此配置可以正常工作(这是模块
website' => array(
'type' => 'Hostname',
'options' => array(
'route' => '[:subdomain.]foo.local',
'constraints' => array(
'subdomain' => '(www|test)'
),
'defaults' => array(
'controller' => 'Frontend\Controller\Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'homepage' => array(
'type' => 'segment',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Frontend\Controller\Index',
'action' => 'index',
),
),
),
【问题讨论】:
-
能否也包含您的 pagination_control 模板,好吗?我认为您可能需要在您的 url 视图助手调用中设置
$reuseMatchedParams标志。见:framework.zend.com/manual/2.3/en/modules/… -
嗨 Crisp,感谢您的帮助:我用编码更新了帖子,我没有使用 $reuesMatchedParams,我尝试添加它,但不幸的是没有任何改变:(
-
你找到答案了吗?
标签: php routing pagination zend-framework2