开发者

how to pass "/home/blah/blah" as a parameter in Kohana

开发者 https://www.devze.com 2023-03-29 20:20 出处:网络
I would like to pass a parameter to a controller in Kohana... As开发者_如何转开发sume the following structure :

I would like to pass a parameter to a controller in Kohana...

As开发者_如何转开发sume the following structure :

class Controller_Configurator extends Controller {

public function action_mytask($param1){}

}

How will I send a path like "/home/blah" through $param1?

Edit : I am going to run this only in CLI.


You can use an overflow parameter in your routing configuration. Then just parse the overflow in your controller. This is how I do it in my bootstrap:

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

Then I use this helper class to get a parameter for a particular controller:

<?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;
    }

}


I ended up using this :

class Controller_fun extends Controller { 

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

} 

That does the job when called like
php index.php --uri="fun/blah" --data_folder=/path/to/wherever

Since I wanted it "only" in CLI, I could use this as a option after studying the example given in kohana's system files : system/kohana/cli.php

0

精彩评论

暂无评论...
验证码 换一张
取 消