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

28,966

Solution 1

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).

Solution 2

In Konana (3.0) you can't access $_GET/$_POST through the Request class. You'll have to directly use $_GET/$_POST

$this->request->param('paramname', 'defaultvalue') is for accessing params defined in the route. For route-urls like <controller>/<action>/<id> you would use $this->request->param('id') to access the portion in the route url.

edit: in Kohana 3.1 there are post and query methods for getting/setting data of the request; check the documentation at http://kohanaframework.org/3.1/guide/api/Request

Solution 3

Notice that altough it's more clear to use $this->request->param(), you can define action params as :

public function action_index($id, $seo = NULL, $something = NULL)..

and access those vars directly. You have to define these vars in the same order they're defined in the corresponding route (excluding the action and controller params, they're defined on request level anyways so there's no need to pass them to the action method).

EDIT: This functionality was deprecated in 3.1 and has been removed from 3.2, so it is best to avoid. You can read more here: http://kohanaframework.org/3.2/guide/kohana/upgrading#controller-action-parameters

Share:
28,966

Related videos on Youtube

Vivek Goel
Author by

Vivek Goel

Updated on July 20, 2020

Comments

  • Vivek Goel
    Vivek Goel almost 4 years

    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 ?

    • Vivek Goel
      Vivek Goel about 13 years
      that is option argument if value is not there . it will replace the value with that value. second value is default value if value not exist .
    • biakaveron
      biakaveron about 13 years
      Whats your Kohana version? Use $this->request->query('id') if using Ko3.1
  • biakaveron
    biakaveron about 13 years
    In 3.1 Request class has query() and post() getters/setters. For example, $request->query('var1') returns $_GET['var1'] value (if exists), while $request->post('var2', 'val2') will send var2=val2 as POST data.
  • Vivek Goel
    Vivek Goel about 13 years
    @biakaveron Can you please post your answer , as answer so that I can accept your answer
  • SpadXIII
    SpadXIII almost 13 years
    @biakaveron ah, I didn't know that (still working with ko3.0). I've updated my answer

Related