2013-05-15 85 views
0

我設置的提取以下獲取的參數了URI

  1. 控制器
  2. 行動
  3. 參數

級路由器{

private $uri; 
private $controller; 
private $method; 
private $params; 

public function __construct($uri) { 
    $this->uri = $uri; 
    $this->method = 'index'; 
    $this->params = array(); 
} 

public function map() { 
    $uri = explode('/', $this->uri); 
    if (empty($uri[0])) { 
     $c = new Config('app'); 
     $this->controller = $c->default_controller; 
    } else { 
     if (!empty($uri[1])) 
      $this->method = $uri[1]; 
     // how about the parameters?? 
    } 
} 

一個簡單的路由器類}

這個簡單的$router->map()可以給我正確的控制器,動作和此URI http://domain.com/users/edit/2

這是非常好的一個參數,但如果我需要存儲在URL這樣的多個參數: http://domain.com/controller/action/param/param2/param3

如果我不知道將傳遞多少個參數,我如何將它們推送到$parms

回答

2

你知道數組的第1兩個值控制器行動,一切後,將是一個PARAM

所以你可以使用array_shift($uri)來獲得第2個,其餘的$uri將是你的參數。

public function map() { 
    $uri = explode('/', $this->uri); 

    // shift element off beginning of array. 

    $controller = array_shift($uri); 
    $action = array_shift($uri); 

    // your $uri variable will not only contain the params. 

    if (empty($uri[0])) { 
     $c = new Config('app'); 
     $this->controller = $c->default_controller; 
    } else { 
     if (!empty($uri[1])) 
      $this->method = $uri[1]; 
     // how about the parameters?? 
    } 
} 
+0

您在知道'$ uri [0]'是否爲空之前轉移了嗎?那會好嗎? –

+1

您可以檢查$ uri [0]是否爲空,然後運行您的默認控制器代碼。這是你的框架,我會把這個決定留給你。 –