2010-03-05 126 views
3

如您所知,Zend Framework(v1.10)使用基于斜杠分隔参数的路由,例如。基于标准PHP查询字符串的路由

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

Queston是:如何强制Zend框架,使用标准的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; 
} 

但它不适合我。

回答

3
$front->setRequest($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()); 
} 
+0

它几乎工作,我不得不从Zend_Controller_Router_Rewrite扩展My_Router或实现其余的接口方法。第一种解决方案更快;)现在它工作得很好。谢谢! – singles 2010-03-06 15:14:24

+0

根据手册,你应该只需要实现该方法。该API似乎说不然,所以我很困惑。你也可以尝试扩展Zend_Controller_Router_Abstract而不是Zend_Controller_Router_Rewrite。他们在手册中需要一个更好的例子。 – smack0007 2010-03-06 16:13:35

1

您是否尝试过:$router->removeDefaultRoutes(),然后$request->getParams()$request->getServer()