2009-08-19 26 views

回答

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()方法是一個方便的位置來處理應用廣泛的認證。

+0

謝謝蘭斯! 你在哪裏設置插件目錄?我正在使用Zend_Application與application.ini文件,我得到 致命錯誤:Class'ControllerPlugins_Vanityurl'找不到C:\ ***** \ library \ Zend \ Application \ Resource \ Frontcontroller.php在90行(我已經在庫文件夾中有ControllerPlugins目錄。 謝謝! – arendn 2009-08-26 22:46:54

+0

謝謝蘭斯我現在正在工作:-) – arendn 2009-09-10 08:02:05

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' 
      ) 
     ) 
    ); 
} 

這樣,你仍然可以有默認路由,並有將重定向下/資料/ JHON一切的自定義路線。 doe然後在你的控制器下使用$ this - > _ getParam('username')獲取參數;