Zend_Router 省略 param-key
我有一个关于 Zend_Controller_Router 的问题。我在我的应用程序中使用模块化结构。该应用程序基于 Zend-Framework 构建。正常的路由是这样的:
/modulename/actionname/
因为我总是在模块中使用 IndexController,所以没有必要在 url 中提供它。现在我可以像这样附加参数:
/modulename/actionname/paramkey/paramvalue/paramkey/paramvalue
所以我猜这在 ZF 中是正常的。但在某些情况下,我不想在 url 中提供 paramkey。例如,我希望在网址中显示博客标题。当然,这是为了 SEO:
/blog/show/id/6/this-is-the-blog-title
在这种情况下,blog
是模块,< code>show 是操作。 id
是一个参数键,6
是我想要显示的博客文章的 ID。 this-is-the-blog-title
当然是 ID 为 6
的博客文章的标题。问题是,如果我确实使用路由器的 assemble()
方法,如下所示:
assemble(array('module' =>'blog',
'action' => 'show',
'id' => $row['blog_id'],
$row['blog_headline_de'] . '.html'));
网址结果为:
blog/show/id/6/0/this-is- the-blog-title.html
如您所见,插入了 0
作为键。但我想省略这个0。我尝试使用 blogtitle 作为键,如下所示:
assemble(array('module' =>'blog',
'action' => 'show',
'id' => $row['blog_id'],
$row['blog_headline_de'] . '.html' => ''));
这会导致:
blog/show/id/6/this-is-the-blog-title.html/
现在 0
被省略,但我在末尾有斜杠。
您是否有任何解决方案来获取没有 0
作为键且没有结尾斜杠的网址?
问候, 亚历克斯
I've got a question considering Zend_Controller_Router. I'm using a a modular-structure in my application. The application is built upon Zend-Framework. The normal Routes are like this:
/modulename/actionname/
Since I always use an IndexController within my modules, it's not necessary to provide it in the url. Now I am able to append params like this:
/modulename/actionname/paramkey/paramvalue/paramkey/paramvalue
So this is normal in ZF, I guess. But in some cases I don't want to provide a paramkey within the url. For example I want a blog-title to be shown within the url. Of course this is intended for SEO:
/blog/show/id/6/this-is-the-blog-title
In this case, blog
is the module, show
is the action. id
is a paramkey and 6
is the id of the blogpost I want to show. this-is-the-blog-title
is of course the headline of the blogpost with the id 6
. The problem is, that if I do use the assemble()
-method of the router like this:
assemble(array('module' =>'blog',
'action' => 'show',
'id' => $row['blog_id'],
$row['blog_headline_de'] . '.html'));
the url results in:
blog/show/id/6/0/this-is-the-blog-title.html
As you can see a 0
is inserted as a key. But I want this 0 to be omitted. I tried this by using the blogtitle as key, like this:
assemble(array('module' =>'blog',
'action' => 'show',
'id' => $row['blog_id'],
$row['blog_headline_de'] . '.html' => ''));
This results in:
blog/show/id/6/this-is-the-blog-title.html/
Now the 0
is omitted, but I've got the slash at the end.
Do you have any solution to get an url without 0
as key and without an ending slash?
Regards,
Alex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想为此使用自定义路由:
并使用该路由作为第二个参数来调用assemble。有关更多详细信息,请参阅文档的 Zend_Controller_Router_Route 部分(他们甚至提供汇编示例)。
或者以更一般的方式:
You might want to use a custom route for this:
And call your assemble with the route as second parameter. See the Zend_Controller_Router_Route section of the documentation for more details (they even provide examples with assemble).
Or in a more general way: