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
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
We always retrieve the value in a $_GET['var'], but is it possible to assign a value as well? I have a php page where at one point, through ajax, I want to stay on the same page but change the query string or do an assignment like this:
$_GET['myvar'] = 'newvalue';
Is this possible?
Yes you can override the $_GET. however that is only for that request.
with ajax you do a new request and in the ajax call you can just use diffrent values for the data.
Yes, you can assign to the $_GET array, but it won't change the query string in the URL.
It's probably not the wisest thing to do though, as it will be overwritten in the next request
$_GET is just like a regular array. The only difference is that the keys of this array will be automatically populated with values that come with the request using HTTP GET. They are called superglobals because they are automatically and globally available anywhere in your script, other wise they behave just like regular arrays.
So if you have a request like mypage.php?key=value, PHP automatically does something equal to this for you:
$_GET['key'] = 'value';
And just like any regular array, you can overwrite it with a different value. However I really do not see a use case for that unless you are doing some testing or some really weird thingy..
With this Symfony page, I am passing $_GET parameters in the URI like this:
http://www.mysite.com/article?page=4&sort=1
Once in my layout, there are certain links in the page that need to have the same query string in them.
Anyways, using Symfony's url_for() command I'm making URLs like so:
$url = url_for('article/index?.http_build_query($_GET));
That way it makes a new url using the $_GET variables. For some of the links I'm changing the $_GET values ahead of time, like $_GET['sort']=0; before generating the url. That's why I'm using this method.
Anyways, when I look at the generated URL, it now looks like this:
http://www.mysite.com/article?page=4&%3Bsort=1
The &%3B is the encoded form of & which is just an & character.
So the problem is that when I check for my $_GET parameters in my controller now, there is no longer a sort parameter that is passed. It's now called &%3Bsort... It's causing all sorts of issues.
Two questions:
How do I avoid this problem? Can I decode the $_GET parameter key values in my controller or something?
Why is symfony encoding a & character in the first place? It's a perfectly acceptable URI character. Heck, even the encoded value, &%3B contains a & !!!
I believe, it is because of output escaping is ON in your application. As a result, $_GET array is wrapped inside sfOutputEscaperArrayDecorator class. You can get a raw value using this: $_GET->getRawValue().
$url = url_for('article/index?.http_build_query($_GET->getRawValue()))
Or you can decode the result query using sfOutputEscaper::unescape
$url = url_for('article/index?.sfOutputEscaper::unescape(http_build_query($_GET)));
Hope this will be useful.
Best if you use Symfony's own method for getting the request parameters. For example, in templates, use:
$sf_request->getParameter('some_param');
If you must use $_GET, maybe try:
((( $sf_data->getRaw('_GET') )))
... to get past the output escaping. Not totally sure if that'll work as is.
Regular expressions have never been one of my strong points, and this one has me stumped. As part of a project, I want to develop an SEO link class in PHP. Handling the mod_rewrite through Apache is fairly straightforward for me, and that works great.
However, I'd like to create a function which is able to generate the SEO link based on a dynamic URL I pass in as the first (and only) parameter to the function.
For example, this would be the function call in PHP:
Blog Post Title
The function CreateLink would then analyse the string passed in, and output something like this:
blog/blog-post-title
The URL stub of the blog post is stored in the Database already. I think the best way to achieve this is to analyse the dynamic URL string passed in, and generate an associative array to be analysed. My question is, what would the Regular Expression be to take the URL and produce the following associative array in PHP?
link_pieces['page_type'] = 'blog/post';
link_pieces['post'] = 123;
link_pieces['category'] = 5;
Where page_type is the base directory and request page without extension, and the other array values are the request vars?
You can just use parse_url and parse_str, no need for regexes.
Use parse_url to break the URL into parts:
This function parses a URL and returns an associative array containing any of the various components of the URL that are present.
Then use parse_str to break down the querystring part of the URL.
I have a bunch of methods of sorting date=desc,etc,etc and they are posted via a $_GET I want to take all the $_GET variables ($_GET['anythingatall']) and transform them from $_GET['variable]=blah to &variable=blah
Is there a simple way to do this?
You are interested in $_SERVER['QUERY_STRING'], I think. This will contain everything passed in $_GET but in the format you desire.
This might work for you
$string = http_build_query($_GET, null, '&')
Alex's solution should work too, and admittedly cleaner. If you wanted to create a query string from any other array using http_build_query should work fine.
If all you're wanting to do is pass on the existing query string (which is available in $_GET), you can use $_SERVER['QUERY_STRING'], which will be exactly what you're looking for, a query string representation of the $_GET array (assuming you didn't change it)
See the PHP documentation on the $_SERVER superglobal.
you may want to take a look at http://us.php.net/manual/en/function.http-build-query.php