0

Zend Framework アプリケーションで seo フレンドリーな URL を作成したいのですが、正しい構文は次のとおりです。

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

:id_:title は、Zend が _ がセパレーターであることを知らなかったため、明らかに機能しませんか? これには正規表現ルーターを使用する必要がありますか、それとも通常のルーターでも機能しますか?

4

2 に答える 2

2

実際、正規表現ルートはそのことを行います。

何らかの理由で正規表現ルートを使用したくない場合は、フロント コントローラー プラグインを介した簡単な回避策があります。

//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 ) );
于 2011-09-20T19:19:36.000 に答える
2

特殊文字を含むリンクを処理しないようにするには、Zend Framework 用のこのプラグインを使用できます。

https://github.com/btlagutoli/CharConvert

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

これは、他の言語で文字列を処理するのに役立ちます

于 2011-10-24T20:36:52.873 に答える