简体   繁体   中英

Zend routes & SEO

I was wondering how i would go about implementing my site's URLs like http://mysite.com/ArticleTitleHere using zend framework.

The way i see it is to implement a regex route, but this way zf will not be able to tell say an article URL from a regular module/controller/action or controller/action routes. How would i go about implementing such a thing?

Thanks

you can use placeholders for that in your route:

routes.test.route = "/:article"
routes.test.defaults.module = default
routes.test.defaults.controller = test
routes.test.defaults.action = test
routes.test.defaults.article = ""

You can retrieve the article parameter in your controller using $this->_getParam("article") . The rest (matching the database entry by the article's name) is your part.

But you should add something like an url-prefix and/or article id, otherwise other routes may not be found. So use something like this is much better:

routes.test.route = "/article/:id/:title"

routes.test.defaults.module = default
routes.test.defaults.controller = test
routes.test.defaults.action = test
routes.test.defaults.article = ""
routes.test.defaults.id = ""

You can use the url-Viewhelper this way to create a link:

<a href="<?php echo $this->url(array("id" => $article->id, "title" => $article->title), "test"); ?>"><?php echo $this->escape($article->title); ?></a>

And in your controller for a redirect:

$this->_helper->redirector->gotoRoute(array("id" => $article->id, "title" => $article->title), "test");

Keep in mind escaping or filtering the $article->title for invalid characters (or non-ascii-characters).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM