How do you get the value for a get parameter that is a list of values? I tried using request->query->get('parameter') but this only returns one value.
Suppose you have a country parameter and want to pass more than 1 value. e.g. ...?country=us,gb using get gives you 'us' only.
I can not find this in the docs.
You have to format your query parameter this way
country[]=us&country[]=gb
From their manual (http://symfony.com/doc/current/book/http_fundamentals.html)
use Symfony\Component\HttpFoundation\Request;
$request = Request::createFromGlobals();
$request->query->get('foo');
Edit:
Sorry, I misunderstood your question. You want an array of all GET valiables like $_GET will give you?
That can be done with (will also include POST parameters):
<?php $request->getParameterHolder()->getAll();
Related
I have a pagination-instance where I want to append all query parameter from the request to the next_page_url attribute.
I have query parameter with a value like &name=chris but I also have single parameter without a value like &xyz.
However, when I append all query parameters to the pagination instance, like so:
$query->simplePaginate(50)->appends($request->all());
only parameters with a value are getting appended.
How can I append all parameters to the next_page_url?
Update
I want to append query parameters to get the next chunk of requested data.
If I don't, it always gives back "next_page_url":"http://vue.dev/contacts?page=2". What I want is "next_page_url":"http://vue.dev/contacts?name&page=2"
Take URL http://vue.dev/contacts?page=2&name for example. Although perfectly valid, it's still quite ambiguous. Do we mean to include name? Do we mean to exclude name?
So I'd suggest you to use this URL instead http://vue.dev/contacts?page=2&select=name. If you decide to select more stuff you can just do http://vue.dev/contacts?page=2&select=name,age,gender.
Later in your code just use explode to use the value as an array:
$attributes = explode(',', $request->select);
Useful reading: http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api
Even though Fahmis solution is possible as well, I end up using the approach from this so-question. This has the advantage that php reads the parameter as an array automatically. My url end up looking like this:
http://vue.dev/contacts?page=2&select[]=xyz&select[]=abc
In one of the cakePHP framework's view, I take the parameters given by a user and make an action call. Here is how it looks like:
echo $this->Html->link(__('Save as PDF'),array('action'=>'view_as_pdf',$_POST['data']['Event']['employee'],$_POST['data']['Event']['project'],$_POST['data']['Event']['from'],$_POST['data']['Event']['to'],'ext' => 'pdf'));
The problem appears when *$_POST['data']['Event']['employee']* or *$_POST['data']['Event']['project']* project is not provided.
That makes a proper url like:
pdf.com/action/16/77/2014-01-01/2014-01-15
Look like:
pdf.com/action/16/2014-01-01/2014-01-15
What I would like it to look is something like:
pdf.com/action/16/null/2014-01-01/2014-01-15
Replace the items in your array passed into the link method with ternary operator and check the values. Essentially, you need to set a default value if the POSTed value is not set/empty/what-have-you.
You could do something like this:
empty($_POST['data']['Event']['project']) ? 'null' : $_POST['data']['Event']['project']
You need to pass a string of null in order for it to be passed as 'null'. Likely, the underlying code for that link method ignores empty parameters.
Doing it this way will give you the pdf.com/action/16/null/2014-01-01/2014-01-15 url you are looking to achieve.
I am having following url,
www.example.com/index.php?r=recommend/create&id=184?
title=Ruins%20of%20Mahabalipuram
&url=http://localhost/index.php/mediadetail/index/184
I need to get title and url which are query string parameters.
Has anyone worked on getting values in query string in Yii?
There is also the getParam() method in CHttpRequest.
Yii::app()->request->getParam('title')
I've found it a valuable shortcut, since it checks both $_POST and $_GET and gives priority to $_GET, so you can use it to override post variables in the address URL. It also performs null checks and you can provide a default value in the second parameter.
The drawbacks are that you can't use it for arrays and maybe it's a little bit verbose (compared to $_GET['title']).
Look the function parse_str, it would worked and if not, look parse_url but it's not necessary for what you want to do.
They'll automatically be available in your action as $_GET variables. Yii handles parsing them for you as part of the CHttpRequest object
I've seen trillions (okay, perhaps not trillions but certainly billions) of examples of using the various PHP filters with an array of options but I haven't seen how you specify just one option.
For example:
On the page at http://www.w3schools.com/php/filter_validate_int.asp it says:
Note: When specifying options in an array. The options must be in an
associative multidimensional array with the name "options". See
example below Example
?php $var=300;
$int_options = array("options"=> array("min_range"=>0,
"max_range"=>256));
var_dump(filter_var($var, FILTER_VALIDATE_INT, $int_options)); ?>
On the page at http://www.w3schools.com/php/func_filter_var.asp there is:
options Optional. Specifies an associative array of flags/options or
a single flag/option. Check each filter for possible options and flags
So - how do you specify only one option?
Say you wanted to check an integer and only specify the maximum value allowed. How do you code the filter_var option?
filter_var($var, FILTER_VALIDATE_INT, ????????)
What would you code in place of the ????????? to specify a maximum allowed value?
Is there a way to do it or do you always have to create, and pass, an associative array even if you use only one option?
Don't use w3schools for anything
Use php.net for your PHP reference
If you did #1 and #2 you would see that third parameter is optional and may be excluded. If you wish to have only one option then you simply pass it only one option in the array:
$int_options = array("options"=> array("max_range"=>256));
I have 2-dimensional GET parameters like request?a[b]=2
I would like to use the php input filter API (http://www.php.net/filter) but cannot find a reasonable way to work on the input a[b].
filter_has_var(INPUT_GET, 'a'); // true
but
filter_has_var(INPUT_GET, 'a[b]'); // false
is there a way to instruct this API to work with 2-dim input parameters ?
Thank you for your help
Jerome
a[b] is not a variable name. You can only use filter_has_var with a correct variable name. The variable name for your parameter is a regardless if it is an array or a string.
So you must first check if the get input contains the a parameter and then check it's contents.
$hasVar = filter_has_var(INPUT_GET, 'a');
$hasArray = $hasVar && is_array($_GET['a']);
Hope this helps.