I want create user-friendly URL like this:
mysite.com/flat-sale/london/1-room/1
When parts of URL is parameters:
flat-sale is post/list/type/1
london is city/123
1-room is rooms/1
and 1 id page/1
For doing this I created table in database - url_alias. This table has three column: aid,url,alis
I inserted in this table next rows:
1 post/list/type/1 flat-sale
2 city/123 lonodon
3 1-room rooms/1
I am using Controller_Plugin for parsing URL:
class My_Controller_Plugin_UrlAlias extends Zend_Controller_Plugin_Abstract {
public function routeStartup(Zend_Controller_Request_Abstract $request) {
$alias = substr($request->getRequestUri(), 1);
$pattern = "([^/]+)";
//this model for CRUD from tables url_alias
$resources = new Admin_Resource_Materialalias();
$match = array();
if (preg_match_all($pattern, $alias, $match)) {
$url = array();
foreach($match['0'] as $m) {
//this is page
if (preg_match("#^[\d]+$#", $m)) {
$url[] = "page/$m";
} else {
$url[] = $resources->getUrl($m);
}
}
$url = implode("/", $url);
//echo $url;
}
if (isset($url) && strlen($url)) {
$request->setRequestUri($url);
}
}
}
This plugin is work perfectly.
But I am else needing create url like this: mysite.com/flat-sale/london/1-room/1. For this purpose, I created new View_Helper:
class My_View_Helper_Alias extends Zend_View_Helper_Url {
public function alias(array $urlOptions = array(), $name = null, $reset = false, $encode = true) {
$url = $this->url($urlOptions, $name, $reset, $encode);
$pattern = "#([^/]+)\/([^/]+)\/([-a-zA-Z0-9_/.]+)#";
$params_pattern = "#([^/]+\/[\d]+)#";
if (preg_match($pattern, $url, $match)) {
$resources = new Admin_Resource_Materialalias();
if (preg_match_all($params_pattern, $match[3],$params)) {
$p_alias = array();
foreach($params[0] as $p) {
//add controller, action and first params
if (empty($p_alias)) {
$p = "/".$match[1].'/'.$match[2]."/".$p;
}
//this is page
if (preg_match("#page\/([\d]+)#", $p, $page)) {
$p_alias[] = $page[1];
continue;
}
//this model for CRUD from tables url_alias
$part = $resources->getAlias($p);
$p_alias[] = strlen($part)?$part:$p;
}
$alias = implode("/",$p_alias);
}
}
$alias = strlen($alias)? $alias : $url;
return $alias;
}
}
This view helper also work, but i think it is not optimal. Can anybody comments this code or maybe has same task? Thank you.
The problem is solved so, thanks for KA_lin:
routes.flat_sale_city_rooms.route = /:type/:city/:rooms/:page
routes.flat_sale_city_rooms.defaults.module = main
routes.flat_sale_city_rooms.defaults.controller = post
routes.flat_sale_city_rooms.defaults.action = list
routes.flat_sale_city_rooms.reqs.type = [^/]+
routes.flat_sale_city_rooms.reqs.city = [^/]+
routes.flat_sale_city_rooms.reqs.rooms = [^/]+
routes.flat_sale_city_rooms.defaults.page = 1
routes.flat_sale_city_rooms.reqs.page = \d+
routes.flat_sale_city.route = /:type/:city/:page
routes.flat_sale_city.defaults.module = main
routes.flat_sale_city.defaults.controller = post
routes.flat_sale_city.defaults.action = list
routes.flat_sale_city.reqs.type = [^/]+
routes.flat_sale_city.reqs.city = [^/]+
routes.flat_sale_city.defaults.page = 1
routes.flat_sale_city.reqs.page = \d+