0

我想像这样创建用户友好的 URL:

mysite.com/flat-sale/london/1-room/1

当 URL 的一部分是参数时:

 flat-sale is post/list/type/1
 london is city/123
 1-room is rooms/1
 and 1 id page/1

为此,我在数据库中创建了表 - url_alias。该表有三列:aid,url,alis

我在此表中插入了下一行:

1    post/list/type/1  flat-sale
2    city/123          lonodon
3    1-room            rooms/1

我正在使用 Controller_Plugin 来解析 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);      
       }
  }
}

这个插件很好用。

但我还需要像这样创建网址:mysite.com/flat-sale/london/1-room/1。为此,我创建了新的 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;      
    }        
  }

这个视图助手也可以工作,但我认为它不是最佳的。任何人都可以评论此代码或可能有相同的任务吗?谢谢你。

问题就这样解决了,感谢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+
4

3 回答 3

1

尝试在 Bootstrap.php 中添加如下内容:

 $route = new Zend_Controller_Router_Route (
    'user/summary/:id/connection',
    array('controller' => 'user',
          'action'     => 'get-summary',
          'id'         => FALSE,
        )
    );
 $router->addRoute('equity', $route);

这会创建您可以调用的自定义 url:

user/my-personal-url, 
user/my-personal-url/connection

假设使用 Zend 1

于 2013-10-24T13:19:28.040 回答
0

为什么不使用 Zend Framework 路由?看看这个例子: http: //framework.zend.com/manual/2.0/en/user-guide/routing-and-controllers.html

于 2013-10-24T13:08:13.637 回答
0

您可以将路由用于用户友好的 url

看看例如

http://framework.zend.com/manual/1.5/en/zend.controller.router.html

于 2013-10-24T13:15:55.103 回答