2012-12-05 93 views
2

我是Zend框架的新手,並使用框架2.0。我需要在PHP Zend框架中對路由進行一些澄清。 1)Bootstrap.php在路由中的作用是什麼。 2)我們如何才能使用index.php進行路由。 3)需要編寫什麼代碼才能實現自定義路由。Zend框架中的自定義路由

此外,我寫了某些代碼,但沒有得到所需的輸出。

bootstrap.php中:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 
{ 
    protected function _initRouter() 
    { 
    $frontController = Zend_Controller_Front::getInstance(); 
     $router = $frontController->getRouter(); 
     $route = new Zend_Controller_Router_Route(':userid', 
              array('controller' => 'user',    'action'=>  'index', 'userid'=>null)); 
     $router->addRoute('default', $route); 
    } 
} 

的index.php

error_reporting(E_ALL); 
ini_set('display_errors', 1); 

ini_set('display_errors', true); 

//Define Base Url 
define("BASEURL","http://".$_SERVER['SERVER_NAME']."/sampleapp/public/"); 

// directory setup and class loading 
set_include_path('.' . PATH_SEPARATOR . 'library/' 
    . PATH_SEPARATOR . 'application/models' 
    . PATH_SEPARATOR . 'application/config' 
    . PATH_SEPARATOR . get_include_path() 
); 

include ('Zend/Loader/Autoloader.php'); 
$loader = Zend_Loader_Autoloader::getInstance(); 

// setup controller 
$frontController = Zend_Controller_Front::getInstance(); 
$frontController->throwExceptions(true); 
$frontController->setControllerDirectory('application/controllers'); 

// run! 
$frontController->dispatch(); 

我試圖讓路由值,就像我的網址是:

localhost:8080/sampleapp/user/index/1. 

我應該怎麼獲得1從路線。這裏1是用戶標識。

+1

您所提供的所有代碼是使用Zend框架1,因此,我會重新打此爲您更好的幫助。 – Sam

回答

2

我建議做到以下幾點:

  • 上的index.php,你不應該碰任何東西。當你在projectHomeDirectory /application/Bootstrap.php創建項目

  • 離開它,因爲它是由Zend的默認包含此:

    protected function _initRoutes() 
    { 
         $router = Zend_Controller_Front::getInstance()->getRouter(); 
         include APPLICATION_PATH . "/configs/routes.php"; 
    } 
    
  • 下創建一個routes.php文件文件projectHomeDirectory /應用程序/ CONFIGS /,並添加有你想要的所有路線,例如:

    $router->addRoute('sample-module/user', new Zend_Controller_Router_Route(
         'user/:action/:id/', 
         array('module' => 'sample', 'controller' => 'user', 'id'=>'\w+') 
    )); 
    

當然你需要創建UserController User模型的示例模塊和視圖。

這些是一些有用的鏈接:

Zend routing, throws resource not found

http://framework.zend.com/manual/1.12/en/zend.controller.router.html#zend.controller.router.routes.standard