how to use $this->request->param of Kohana to get request variables

Vivek Goel picture Vivek Goel · Apr 11, 2011 · Viewed 28.7k times · Source

I have written a sample controller in kohana

    <?php

defined('SYSPATH') OR die('No direct access allowed.');

class Controller_Album extends Controller {

  public function action_index() {
    $content=$this->request->param('id','value is null');   
    $this->response->body($content);
  }

}

But when I am trying to hit url http://localhost/k/album?id=4 I am getting NULL value. how can I access request variable in kohana using request->param and without using $_GET and $_POST method ?

Answer

biakaveron picture biakaveron · Apr 12, 2011

In Kohana v3.1+ Request class has query() and post() methods. They work both as getter and setter:

// get $_POST data
$data = $this->request->post();
// returns $_GET['foo'] or NULL if not exists
$foo = $this->request->query('foo'); 

// set $_POST['foo'] value for the executing request
$request->post('foo', 'bar');
// or put array of vars. All existing data will be deleted!
$request->query(array('foo' => 'bar'));

But remember that setting GET/POST data will not overload current $_GET/$_POST values. They will be sent after request executing ($request->execute() call).