如何为没有HTML表单和URL的$ _GET创建查询字符串? [英] how to create query string for $_GET without HTML form and URL?
问题描述
影像我们在主页:www.example.com
Imaging we are at home page: www.example.com
我们在此页面有一个超链接。
we have a hyperlink on this page.
<a href="category/Pizzas">Pizzas</a>
点击此超链接时,地址栏中的网址为:
when click this hyperlink, the url at address bar will be:
www.example.com/category/Pizzas
它会向我们展示不同种类的披萨。问题是我的服务器中没有类别/比萨饼这样的路径。它的工作方式是将category和Pizzas传递给两个查询字符串(page和cat),它们将存储在$ _GET中。所以真正的网址是:
and it will show us the different kinds of pizzas. The problem is I don't have a path like category/Pizzas in my server. The way it works is to pass category and Pizzas to two query strings (page and cat), which will be stored in $_GET. So the real url is :
www.example.com/?page=category&cat=Pizzas
然后有一个函数可以呈现比萨饼页面。
Then there is a function to render the Pizzas page.
你可以图像我们有一些其他的主页上的超链接,他们有不同的网址。例如:
You can image we have some other hyperlinks on the main page and they have different urls. For example:
<a href="category/Salads">Salads</a>
<a href="category/Wraps">Wraps</a>
<a href="category/Grinders">Grinders</a>
当您点击它时,查询字符串(页面和猫)将从url中捕获该值并进行相应的渲染。
when you click it, query strings(page and cat) will capture the value from url and render accordingly.
我不明白的部分是如何以及在哪里定义查询字符串(在这种情况下是:page和cat),并从URL类别/ Pizzas所以我们可以有page = category& cat = Pizzas。源代码中没有这两个查询字符串的HTML表单。它完美地工作。
The part I don't understand is how and where to define the query string (in this case: page and cat) and capture value from URL like category/Pizzas so we can have page=category&cat=Pizzas. There is no HTML form for these two query strings in source code. It just works perfectly.
ps。如果你有兴趣,这里是我正在谈论的项目。这不是我的项目。
https://github.com/nelsonreitz/project0
ps. if you are interested, here is the project I'm talking about. This is not my project. https://github.com/nelsonreitz/project0
推荐答案
在这个回购中,看看public / .htaccess
这就是魔术发生的地方。它实际上是将网址映射到参数的网络服务器。代码如下所示:
In that repo, take a look in public/.htaccess This is where the magic happens. It's the web server which actually does the job of mapping the url to parameters. The code looks like this:
RewriteEngine on
RewriteBase /
RewriteRule ^category/(.*)$ index.php?page=category&cat=$1 [L]
url类似于/将被重写为index.php?page = category& cat =
What that's doing is saying any url that looks like category/ will be rewritten to index.php?page=category&cat=
因此,index.php实际上处理所有这些请求,并且具有正确的GET值 - 即使使用的网址不同。
So index.php actually handles all those requests, and has the correct GET value - even though the url used is different.
这篇关于如何为没有HTML表单和URL的$ _GET创建查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!