2011-08-23 32 views
1

一個參數,我想傳遞一個參數到Kohana的控制器...
假設以下結構:
class Controller_Configurator extends Controller {
public function action_mytask($param1){}
}
如何通過「/首頁/胡說/嗒嗒」在Kohana中

怎麼會我通過$ param1發送一個像「/ home/blah」的路徑?

編輯:我打算只在CLI中運行它。

+0

嘗試使用url_encoding它。 –

回答

1

我結束了使用此:

class Controller_fun extends Controller { 

    public function action_blah() 
    { 
    $data_folder = CLI::options('data_folder'); 
    echo $data_folder['data_folder']; 
    } 

} 

這樣做就像
php index.php --uri="fun/blah" --data_folder=/path/to/wherever

由於我只在CLI中需要它,因此我可以在研究kohana系統文件中給出的示例之後使用它作爲選項:system/kohana/cli.php

1

您可以在路由配置中使用溢出參數。然後解析控制器中的溢出。這就是我要做的事在我的引導:

Route::set('default', '(<controller>(/<action>(/<overflow>)))', array('overflow' => '.*?')) 
    ->defaults(array(
     'controller' => 'widget', 
     'action'  => 'index', 
    )); 

然後我用這個輔助類來獲得一個參數爲特定的控制器:

<?php defined('SYSPATH') or die('No direct script access.'); 

class UrlParam { 

    static public function get($controller, $name) { 
     $output = $controller->request->param($name); 
     if ($output) return $output; 

     if (isset($_GET[$name])) return $_GET[$name]; 

     $overflow = $controller->request->param("overflow"); 
     if (!$overflow) return null; 

     $exploded = explode("/", $overflow); 
     for ($i = 0; $i < count($exploded); $i += 2) { 
      $n = $exploded[$i]; 
      if ($n == $name && $i < count($exploded) - 1) return $exploded[$i + 1]; 
     } 

     return null; 
    } 


    static public function getArray($controller) { 
     $overflow = $controller->request->param("overflow"); 
     if (!$overflow) return array(); 

     $output = array(); 
     $exploded = explode("/", $overflow); 

     for ($i = 0; $i < count($exploded); $i += 2) { 
      $n = $exploded[$i]; 
      $output[$n] = $exploded[$i + 1]; 
     } 

     return $output; 
    } 

} 
+0

喜歡的方法,+1 – Shrinath

+0

我會利用我的代碼在其他地方:) – Shrinath