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

4

10

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 ?

Tomtit answered 11/4, 2011 at 13:4 Comment(3)
Why are you passing second argument ,'value is null'?Panic
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 .Tomtit
Whats your Kohana version? Use $this->request->query('id') if using Ko3.1Faletti
F
24

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

Faletti answered 12/4, 2011 at 7:4 Comment(0)
R
4

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

Roussel answered 11/4, 2011 at 13:22 Comment(3)
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.Faletti
@Faletti Can you please post your answer , as answer so that I can accept your answerTomtit
@Faletti ah, I didn't know that (still working with ko3.0). I've updated my answerRoussel
R
1

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

Raleigh answered 11/4, 2011 at 20:9 Comment(0)
I
0

If I remember well, if you have not changed the default routes, you can try using the url http://localhost/k/album/4 with that controller.

As the default route is in the form: /<controller>/<action>/<id>

Hope it helps.

Irruptive answered 11/4, 2011 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.