如何使用db?
中定义的参数来提供静态.phtml子页面例如,我在数据库中有条目:
table 'static_subpages' {
id: 1
url: /author
route: module=about;controller=index;action=subpage
params: page=author.phtml
layout: true
}
这是一个简单的例子。当然,我可以在module.config.php中创建maaaany路由,但我想从数据库动态管理页面(在管理面板中)。非常重要的是SEO URL:像/ about / subpage / author这样的URL并不比/ author更好。
我有什么想法可以做到吗?
答案 0 :(得分:1)
您可以使用路由配置中的URL段来提供静态页面:
在module.config.php
:
'static' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => 'about/:page_name',
'defaults' => array(
'controller' => 'About\Controller\Index',
'action' => 'subpage',
),
),
在About\Controller\IndexController
:
/**
* Render static pages
*
* @return \Zend\View\Model\ViewModel
*/
public function staticAction()
{
$pageName = $this->params('page_name');
$view = new ViewModel();
// loads views like view/static/author.phtml
$view->setTemplate('static/' . $pageName);
return $view;
}
在您的观看中,您可以使用以下内容:
<a href="<?php echo $this->url('static', array('page_name' => 'author')); ?>">
Author
</a>
这将生成如下网址:/about/author/
答案 1 :(得分:0)
除了mihaidoru的答案,你不一定需要使用'/'你可以用任何分隔符映射参数。我在这里有一个小例子,我在那里制作了一些没有'/'的友好网址:
'randomroute' => array(
'type' => 'segment',
'options' => array(
'route' => '/[:param].:id:.html',
'constraints' => array(
'id' => '[0-9]+',
'param' => '[a-zA-Z-0-9-_\/%]+',
),
'defaults' => array(
'controller' => 'Your\Name\Space',
'action' => 'index',
),
),
),
在我的示例中,任何以.html结尾的url和Id约束都将在此处映射。
your.url.application/test123.01.html or your.url.application/foobar.02.html etc.
在这种情况下[:param]
是可选的,由'[:]'表示。
这可能不是您正在寻找的内容,因为您可能需要为每个路由执行此操作,除非您为此url指定一个操作参数,这有点会导致额外的代码行。我会为每条路线创建它,因为我觉得路由配置工作得很好,开始时不应该那么麻烦。
答案 2 :(得分:0)
我找到了解决方案。感谢上面的回复,但不是这样。
首先,我使用标准控制器和index
动作创建了新模块“Subpage”
接下来,为此设置路由器:
'router' => array(
'routes' => array(
'subpage' => array(
'type' => 'segment',
'options' => array(
'route' => '/[:page[.html]]',
'constraints' => array(
'page' => '[a-zA-Z0-9]*',
),
'defaults' => array(
'controller' => 'Subpage\Controller\Subpage',
'action' => 'index',
),
),
),
),
),
但是当我在 application.config.php 中添加模块时,实际模块及其路由不可用。解决方案很简单,但花了我几个小时的实验 - 将Subpage模块移到第一个:)
在application.config.php
:
'modules' => array(
'Subpage',
'Application',
'Gallery',
),
现在,路由器跟踪是:
Subpage
控制器进行路由;此模块中的IndexAction
检查数据库,并在找到时提供静态页面Subpage -> IndexController -> IndexAction()
时:重定向到ErrorController
ErrorController
对于有ZF2经验的人来说这很简单,但不适合我。我没有找到类似的东西。