I need to capture several parameters in a controller regardless of whether the were posted or they are in the url.
Does $this->_request->getParam('parameter') work regardless?
To make life easier and shorter code, you can use the _getParam function in your controllers:
$page = $this->_getParam('page', 1);
Note that the second function variable is the default value if the request didn't include that specific variable.
Short answer, yes.
If you are in the controller, you can access any POST of GET parameter by accessing the getParam() method like you said.
$this->getRequest()->getParam("foo") will get the parameter foo, if it is present in the URL via a get param, or in a POST. It will also get any user set parameters.
The
$this->getRequest()->getParams();
Will get several parameters regardless of the action type being sent (get or post).
$this->getRequest()->getParam('foo');
Will get you individual requested parameter.
i prefer always use short function:
$parameter = $this->_getParam('parameter');
Related
In CakePHP, it is possible to get the called function string using the
$this->action
syntax. It returns the literal string of whatever is called, so if the URL is /do_this, it returns do_this, and if it's doThis it'll return doThis. Regardless of the called method's real name.
What I am looking for, on the other hand, is the called method's actual name, no matter the URL syntax.
Is there a way to find it out?
I'd preferably be able to do this in the beforeFilter method.
You should use the request object.
CakePHP 3.3 and below
$this->request->params['action'];
Since 3.4
$this->request->getParam('action');
I think this should contain the real method name that was called. CakePHPs router resolves the string URL to a controller / action pair and other args, all of that ends up in the request object. Read the documentation and do debug($this->request); in your beforeFilter() to see what else is there.
In CakePHP 2 you can use $this->action, in CakePHP 3 you must use $this->request->params['action']
The params array (CakePHP >= 3.4) is deprecated The correct way to get the current action within a controller is :
$currentAction = $this->request->getParam('action');
Have you taken a look at this?
Retrieving the name of the current function in php
This obviously will not work in the beforeFilter. You can set a variable:
private $action_name in the Controller and set it from within the methods and use it afterwards, in afterFilter
I am using codeigniter re-route to clean up some urls.
I am aware that I can do
$route['product/(:num)'] = "catalog/product_lookup_by_id/$1";
But in some cases I have to add some extra parameters to the redirect url so that I get them as a param to the method. for example
$route['product_unique_and_rare'] = "catalog/product_lookup_by_id/{HERE I WANT SOME ADDITIONAL EXTRA PARAM}";
How to do this so that I get the value in the param of the method rather than the value in uri->resegment
you can try this
$route['product_unique_and_rare/(:num)'] = "catalog/product_lookup_by_id/$1";
Get the param in product_lookup_id like this
function product_lookup_id($product_id){
/*$product_id will be the passed parameter*/
}
So, if someone goes for http://domain.com/product_unique_and_rare/23, $product_id will get the value 23.
You can hard-code the parameter too, but I believe you aren't looking for that.
I had a read of the documentation, but couldn't see an example of how it would be possible to use the variable in traditional PHP style of $_POST['var']
I'm pretty sure my URL is legit:
domain.com/module/controller/action/var/value/
Using the above as an example:
$var didn't work
$_POST['var'] didn't work
How is it done?
As presented in zend controller's documentation page you can retrieve parameters like this:
public function userinfoAction()
{
$request = $this->getRequest();
$username = $request->getParam('username');
$username = $this->_getParam('username');
}
You should also note that request documentation states:
In order to do some of its work, getParam() actually retrieves from several sources. In order of priority, these include: user parameters set via setParam(), GET parameters, and finally POST parameters. Be aware of this when pulling data via this method.
If you wish to pull only from parameters you set via setParam(), use the getUserParam().
Additionally, as of 1.5.0, you can lock down which parameter sources will be searched. setParamSources() allows you to specify an empty array or an array with one or more of the values '_GET' or '_POST' indicating which parameter sources are allowed (by default, both are allowed); if you wish to restrict access to only '_GET' specify setParamSources(array('_GET')).
$this->_request->getParam('paramName', $defaultValueToReturnIfParamIsNotSet);
I am trying to pass parameters from one action (foo) to another (foobar).
In action foo, I set the arguments thus:
$request->getParameterHolder()->set('arg1', 'alice');
$request->getParameterHolder()->set('arg2', 'bob');
In action foobar, I try to retrieve the params thus:
$arg1 = $request->getParameter('arg1');
$arg2 = $request->getParameter('arg2');
$this->forward404Unless($arg1 && $arg2); //always forwarded
Note: I am aware that I can save the params into the user session variable - but I dont want to do that. I want to pass them as parameters - any ideas how to get this to work?
You can simply try this:
$this->redirect('module/action2?'.
http_build_query(array("arg1"=> "alice", "arg2"=>"bob")));
greg0ire's answer sounds like it's what you are asking for but there are a couple of other approaches that might be worth looking at if passing query string parameters isn't a hard requirement.
You could use a forward if you want the foobar action to execute after foo. Unlike a redirect this will live in the same request cycle so you can pass variables without touching the session.
You don't say why you don't want to use the session but there is a halfway house in Symfony: flash attributes. These are stored in the session but are guaranteed not to live beyond the next request which may be a suitable compromise.
Can I use CodeIgniter's input class to xss clean GET data like this:
$somevar = $this->input->xss_clean($_GET['somevar']);
CodeIgniter's suggest that xss_clean method should be used for the submitted data.
I wonder whether $_GET vars are submitted or just visiting a URL.
So can i use it in that fashion?
Try using:
$this->input->get()
This function is identical to the post function, only it fetches get data:
$this->input->get('somevar', TRUE);
The function returns FALSE (boolean) if the item you are attempting to retrieve does not exist.
The second optional parameter lets you run the data through the XSS filter. It's enabled by setting the second parameter to boolean TRUE;
The GET array is unset by CI on startup because it uses the URI segments instead.
But you can use the xss_clean method on any var you want, just like your example, but you will find $_GET to be empty. The input class is available everywhere by default.