需要设置更清晰的链接 - 当前链接太长 - Zend 框架
我正在为我的项目使用 zend 框架 - 我尝试使用干净的链接进行设置 - 但是我注意到我的链接似乎变得非常非常长,想知道此时是否有一种方法可以合并我可以使用较小链接的结构。
下面是一个例子,我的网站有一个目录控制器,它有一个带有 id 参数的人员操作 - 因此,查看某个 id 的人员的链接如下所示:
www.mywebsite.com/directory/person/id/1809
看起来不错,但我现在正在努力使链接更具可读性和搜索引擎友好 - 我想要类似的东西 - 假设用户 ID 1809 名为 John Smith:
www.mywebsite.com/directory/person/John-Smith-1809
像下面这样的非常基本的东西会更好:
www.mywebsite.com/John-Smith-1809
虽然我不知道如何在不提及控制器或的情况下管理它行动...任何想法都会真正有帮助...
I'm using the zend framework for my project - I've tried to set up using clean links - however I've noticed that my links seem to be getting really really long and wonder if at this point there would be a way to incorporate a structure where I could user smaller links.
Heres an example my website has a directory controller, which has a person action which takes an id parameter - so then a link to see a person of a certin id looks like:
www.mywebsite.com/directory/person/id/1809
It seems ok but I'm working now to make links more readable and search engine friendly - I would like to have something like - assuming the user ID 1809 is named John Smith:
www.mywebsite.com/directory/person/John-Smith-1809
Something really basic like the following would be even better:
www.mywebsite.com/John-Smith-1809
Although I don't know how I could manage that without mentioning a controller or action...any ideas guys would really help...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看看 ZF关于如何使用路由的手册。
基本上,您需要这样的东西才能使其工作:
注意:不要指望它开箱即用。您可能需要修改它。不过应该让你进入正确的方向。
Have a look at the ZF Manual on how to use Routes.
Basically, you need something like this to make it work:
Note: don't expect this to run out of the box. You will likely have to modify it. Should get you into the right direction though.
就像之前的答案所说,您可以使用正则表达式路线,但对于初学者来说,使用“常规”路线可能会更容易,如下所示:
http://yoursite.com/1234/John-Smith/
在你的引导程序中,你会做:
这会向路由器添加一个名为“user”的路由,其形式为/用户id/用户名,所以/1234/John-Smith/。它将在用户模块中查找用户控制器,然后执行读取操作。如果您愿意,您当然可以将其更改为另一个模块/控制器/操作。
然后,您将在 User 模块中拥有 User_UserController,该模块具有 readAction();在该操作中,您可以通过以下方式获取 id:
类似地,如果您需要它,您可以检索名称。
在视图脚本中,您可以使用以下命令生成该路由的 url:
注意“user”参数,它告诉路由器使用我们之前定义的“user”路由。
Like a previous answer said you could use a Regex route, but for starters it would probably be easier to use a "regular" route that looks like:
http://yoursite.com/1234/John-Smith/
In your bootstrap you'd do:
This adds a route called "user" to the router, that has the form /user-id/user-name, so /1234/John-Smith/. It will look for a usercontroller in a user module, and go to the read action. You can of course change that to another module/controller/action if you wish.
You'd then have that User_UserController in the User module, that has a readAction(); In that action you could get the id with:
Similary, should you need it, you could retrieve the name.
In a view script you could generate an url to that route with:
Note the 'user' parameter, which tells the router to use the "user" route we defined earlier.
您可以使用 apache 模块 mod_rewrite 重写 URL 的
http://httpd.apache .org/docs/1.3/mod/mod_rewrite.html
You can use the apache module mod_rewrite to rewrite your URL's
http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html