【问题标题】:Seo friendly urls with the Zend Router使用 Zend 路由器的 SEO 友好 URL
【发布时间】:2012-09-12 23:23:50
【问题描述】:

我想在我的 Zend Framework 应用程序中创建对 seo 友好的 url,但是正确的语法如何:

$newsroute = new Zend_Controller_Router_Route(
   'news/:action/:id_:title',
    array( 'controller' => 'news' ));

:id_:title 显然不起作用,因为 Zend 不知道 _ 是分隔符?我需要为此使用正则表达式路由器还是它也可以与普通路由器一起使用?

【问题讨论】:

    标签: php zend-framework zend-controller-router


    【解决方案1】:

    确实,正则表达式路由可以做到这一点。

    如果出于某种原因您不想使用正则表达式路由,可以通过前端控制器插件提供一个简单的解决方法:

    //replace the :id and :title params with a single one, mapping them both
    $newsroute = new Zend_Controller_Router_Route(
            'news/:action/:article',
             array( 'controller' => 'news' )
       );
    
    // in a front controller plugin, you extract the Id form the article param
    function function dispatchLoopStartup( Zend_Controller_Request_Abstract $request ) {
    
        if( $request->getParam( 'article', false ) ){
    
            $slug = $request->getParam( 'article' );
            $parts = array();
            preg_match( '/^(\d+)/', $slug, $parts );
    
            // add the extracted id to the request as if there where an :id param
            $request->setParam( 'id', $parts[0] );
        } 
    }
    

    当然你也可以根据需要用同样的方法提取标题。

    当您想要生成 url 时,不要忘记构建您的“文章”参数:

     $this->url( array( 'article' => $id.'_'.$title ) );
    

    【讨论】:

      【解决方案2】:

      为避免处理包含特殊字符的链接,您可以将此插件用于 Zend Framework。

      https://github.com/btlagutoli/CharConvert

      $filter2 = new Zag_Filter_CharConvert(array(
                     'onlyAlnum' => true,
                     'replaceWhiteSpace' => '-'
                 ));
      echo $filter2->filter('éééé ááááá ? 90 :');//eeee-aaaaa-90
      

      这可以帮助你处理其他语言的字符串

      【讨论】:

        猜你喜欢
        • 2012-03-20
        • 2012-01-17
        • 2012-10-06
        • 2011-12-24
        • 2011-04-06
        • 2012-10-24
        • 1970-01-01
        • 1970-01-01
        • 2011-11-21
        相关资源
        最近更新 更多