2011-08-01 34 views
0

我使用Zend Framework開發的一個應用程序需要通過website.com/username訪問用戶的配置文件頁面,而其他頁面則應通過website.com/controller_name/action_name訪問Zend Framework website.com/username

我不太確定如何實現,但是,我覺得這可以通過在.htaccess文件中進行一些調整來完成。

有人可以幫我嗎?

提前

+0

我認爲你可以做的最好的將是website.com/user/username。我認爲你不能只擁有website.com/username。 – Marcin

+0

@Marcin:好吧,OP可能會按照他的建議(所有的「單層」URL傳遞給執行用戶查找並呈現用戶配置文件的控制器/操作),但是(正如你所暗示的那樣)它是一個相當可疑的人網址方案。你建議的方案要好得多。 –

回答

2

正如前面所說,你可以使用自定義路徑將路由單級請求。但是,這也會覆蓋默認路由。如果你使用模塊,這將不再起作用example.com/<module>

我已經做過,但只對靜態頁面。我想這一點:

example.com/about 

,而不是這樣的:

example.com/<some-id>/about 

,同時保持默認路由所以這仍然工作

example.com/<module> 
example.com/<controller> 

我這樣做的方式是使用插件來測試我的請求可能被派遣。如果請求無法使用默認路由分派,那麼我會將請求更改爲適當的模塊以加載我的頁面。這裏是一個示例插件:

class My_Controller_Plugin_UsernameRoute extends Zend_Controller_Plugin_Abstract 
{ 
    public function preDispatch(Zend_Controller_Request_Abstract $request) 
    { 
     $dispatcher = Zend_Controller_Front::getInstance()->getDispatcher(); 

     if (!$dispatcher->isDispatchable($request)) { 

      $username = $request->getControllerName(); 

      $request->setModuleName('users'); 
      $request->setControllerName('dashboard'); 
      $request->setActionName('index'); 
      $request->setParam('username', $username); 

      /** Prevents infinite loop if you make a mistake in the new request **/ 
      if ($dispatcher->isDispatchable($request)) { 
       $request->setDispatched(false); 
      } 

     } 

    } 
} 
+0

HI brady.vitrano 你能告訴我如何以及在哪裏可以這個自定義插件? 另外,我使用基於模塊的應用程序。 非常感謝 – user845990

+0

這是關於註冊插件的問題。 http://stackoverflow.com/questions/938930/zend-framework-1-8-recommended-way-to-register-a-plugin –