3

ご存知のように、Zend Framework (v1.10) は、スラッシュで区切られたパラメーターに基づくルーティングを使用します。

[server]/controllerName/actionName/param1/value1/param2/value2/

問題は次のとおりです: Zend Framework に、標準の PHP クエリ文字列を使用してアクションとコントローラー名を取得させる方法、この場合:

[server]?controller=controllerName&action=actionName&param1=value1&param2=value2

私はもう試した:

protected function _initRequest()
{
    // Ensure the front controller is initialized
    $this->bootstrap('FrontController');

    // Retrieve the front controller from the bootstrap registry
    $front = $this->getResource('FrontController');

    $request = new Zend_Controller_Request_Http();
    $request->setControllerName($_GET['controller']);
    $request->setActionName($_GET['action']);
    $front->setRequest($request);

    // Ensure the request is stored in the bootstrap registry
    return $request;
}

しかし、それは私にはうまくいきませんでした。

4

2 に答える 2

3
$front->setRequest($request);

この行は、Requestオブジェクトインスタンスのみを設定します。frontControllerは引き続きルーターを介してリクエストを実行し、そこで呼び出すコントローラー/アクションが割り当てられます。

独自のルーターを作成する必要があります。

class My_Router implements Zend_Controller_Router_Interface
{
    public function route(Zend_Controller_Request_Abstract $request)
    {
        $controller = 'index';
        if(isset($_GET['controller'])) {
            $controller = $_GET['controller'];
        }

        $request->setControllerName($controller);

        $action = 'index';
        if(isset($_GET['action'])) {
            $action = $_GET['action'];
        }

        $request->setActionName($action);
    }
}}

次に、ブートストラップで:

protected function _initRouter()
{
    $this->bootstrap('frontController');
    $frontController = $this->getResource('frontController');

    $frontController->setRouter(new My_Router());
}
于 2010-03-05T23:12:00.127 に答える
1

試しましたか: $router->removeDefaultRoutes()、その後、$request->getParams()または$request->getServer()?

于 2010-03-06T08:15:10.987 に答える