Get #part in URL with PHP/Symfony

末鹿安然 提交于 2019-12-01 14:06:01

问题


I'm working with Symfony 1.2. I've a view with a list of objects. I can order them, filter them by category, or moving to the next page (there is pagination). Everything is done with AJAX, so I don't have to load all the page again.

What I want to achieve is to have http://urltopage#page=1&order=title&cats=1,2 for example; so the new page is saved in the browser history, and he can paste it to another web.

I haven't found a way to get the #part. I know that's only for the browser but I can't believe I can't get through PHP. I'm sure there is a simple solution I'm missing...

thanks a lot!


回答1:


You can't get it through PHP because it's never transmitted to the server.

You can, however, get it with JavaScript via window.location.hash, then transmit it to the server via AJAX.




回答2:


The anchor, as you said, is an information that's used by the browser -- and is not sent to the server, when doing a standard HTT GET request.

This means there is no way for your PHP script to get that information...


... unless you try using some Javascript-based magic, like using an Ajax request, sending some additionnal parameter that would contain the value of the anchor, or something like that... but that wouldn't be quite a standard way of doing things, and I would not recommend doing this...




回答3:


window.location.hash is the only way ("hash"/anchor is client-side only, never ever visible by servers), but nothing forbids you from manipulating the querystring (converting to GET params) or building a FORM using JS and converting to POST params.

That's the basics to build an AJAX javascript request handler/manager. But in the end you should end up building a GET/POST "normal" url to send your request to.

You could also store in a cookie the hash, but again, I wouldn't use it as the way to handle routing or data passing.



来源:https://stackoverflow.com/questions/2624119/get-part-in-url-with-php-symfony

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!