使用 Zend Router 提供对 Seo 友好的 url

发布于 2024-12-05 17:27:44 字数 291 浏览 0 评论 0原文

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

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

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

I want to create seo friendly urls in my Zend Framework application but how is the correct syntax for this:

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

:id_:title obviously doesn't work since Zend didn't know that _ is a separator? Do i need to use the regex router for this or does it work with the normal one too?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南风起 2024-12-12 17:27:44

事实上,正则表达式路线就可以做到这一点。

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

//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 时,不要忘记构建你的 'article' 参数:

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

Indeed a regex route would do the thing.

If for any reason you don't want to use the regex route, there is a simple workaround via a front controller plugin:

//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] );
    } 
}

Of course you can also extract the title the same way if you need it.

Don't forget to build your 'article' param when you want to generate urls:

 $this->url( array( 'article' => $id.'_'.$title ) );
且行且努力 2024-12-12 17:27:44

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

https://github.com/btlagutoli/CharConvert

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

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

To avoid dealing with the links contain special characters, you can use this plugin for Zend Framework.

https://github.com/btlagutoli/CharConvert

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

this can help you deal with strings in other languages

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文