Zend: Clean urls when relying on a generic Page controller?
An application I'm working with has the page structure setup such so that when you create a new page and view it, the URI is:
http://site.com/page/open/title/Contact Us
A client has requested that we need shortened urls, so for example the prior URI should be something like http://site.com/contact-us/
The reason why there's a generic page controller is because these pages have editable content regions in an admin area and it's much easier controlling them with this type of structure.
In regards to the request of shortening them, how should I go about this? Should I, for example, create controllers for each of the 20 pages and somehow reroute them, or setup some method in my initial page controller as a shortcut to open/title/
such as page/name/Contact Us
? I would think that relying on custom rewrite rules is a dirty solution and I shouldn't need 开发者_如何学编程to use any, perhaps I'm wrong?
By the way, some of these pages will also have custom dynamic content such as forms, so they're not all purely html/static. Any advice is appreciated.
you must create "slug" for page title. it'll be like 'contact-us' and save it
then use standart router functionality
routes.showarticles.type = "Zend_Controller_Router_Route_Regex"
routes.showarticles.route = "articles(?:/([a-zA-Z\-]+))?"
routes.showarticles.reverse = "articles/%s"
routes.showarticles.defaults.controller = "posts"
routes.showarticles.defaults.action = "show-articles"
routes.showarticles.map.1 = "slug"
routes.showarticles.defaults.slug = slug
something like this
or u can do like stackoverflow including page_id in url and optional slug part like
http://zagon.org/articles/ku-ka-re-ku/56/edet-kujvashev-na-velosipede-7
http://zagon.org/articles/ku-ka-re-ku/56
EDIT
protected function _initRoutes()
{
if ($this->hasOption('routes')) {
$this->bootstrap('frontController');
$frontController = $this->getResource('frontController');
/* @var $router Zend_Controller_Router_Rewrite */
$router = $frontController->getRouter();
$router->addConfig(new Zend_Config($this->getOption('routes')));
}
}
You can approach this with zend routing or mod_rewrite. My logic would be
- If URL exists, route to the matched one
- If URL does not exist, check if space is present
- Replace space with '-' (or other delimiter) then do the checking again.
- If no space is present, display a 404 or redirect to front page.
精彩评论