0

デフォルトの /module/controller/action/value ルーティング構造を維持しながら、バニティ URL の「すべてをキャッチ」ルートを作成する方法はありますか?

ありがとう!

4

2 に答える 2

3

たとえば、ブートストラップでカスタム ルートを設定した方がよいでしょう。

protected function _initRoutes() {
     $this->bootstrap('frontController');
     $front = $this->getResource('frontController');
     $router = $front->getRouter();

     $router->addRoute(
         'neat_url',
         new Zend_Controller_Router_Route(
              'profile/:username',
              array(
                   'controller' => 'profiles',
                   'action' => 'view_profile'
              )
         )
     );
}

このようにして、デフォルト ルートを保持し、/profile/jhon.doe の下のすべてをリダイレクトするカスタム ルートを保持し、コントローラーの下で $this->_getParam('username'); を使用してパラメーターを取得できます。

于 2009-08-19T16:30:59.333 に答える
2

フロントコントローラープラグインでPreDispatch()フックを使用できます。そのようです:

ブートストラップで

<?php
...
$frontController = Zend_Controller_Front::getInstance();
// Set our Front Controller Plugin
$frontController->registerPlugin(new Mystuff_Frontplugin());

?>

次に、Mystuff/Frontplugin.php内

<?php

class Mystuff_Frontplugin extends Zend_Controller_Plugin_Abstract
{
    ....

    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        ....

        $controllerFile = $this->_GetRequestedControllerFile();

        if (!is_file($controllerFile)) {
            // Controller does not exist
            // re-route to another location
            $request->setModuleName('customHandler');
            $request->setControllerName('index'); 
            $request->setActionName('index');

        }
    }

    ....
}

また、preDispatch()は、アプリケーション全体の認証を処理するための便利な場所です。

于 2009-08-19T03:53:49.643 に答える