php 从 URL 隐藏 GET 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17086732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hide GET parameter from URL
提问by Dhanendran Rajagopal
How to hide URL GET parameters (http://domain.com/MyFirstYii/page?view=about). I've searched lot of posts. They all are saying about rewrite and URL manager, but i couldn't achieve what i want. :(
如何隐藏 URL GET 参数 ( http://domain.com/MyFirstYii/page?view=about)。我搜索了很多帖子。他们都在谈论重写和 URL 管理器,但我无法实现我想要的。:(
My scenario is,
我的场景是,
I just want to hide the URL GET parameters.
我只想隐藏 URL GET 参数。
Eg:
例如:
http://domain.com/MyFirstYii/page***?view=about***
I wanted to hide ***?view=about***
.
我想躲起来***?view=about***
。
Then URL should look like this http://domain.com/MyFirstYii/page
. Other pages like this http://domain.com/MyFirstYii/post
. In a simple words my GET parameters should act like POST parameters.
那么 URL 应该是这样的http://domain.com/MyFirstYii/page
。其他页面类似http://domain.com/MyFirstYii/post
。简单来说,我的 GET 参数应该像 POST 参数一样。
Thanks in Advance.
提前致谢。
Edit:
编辑:
I want to create some rules in the URLManager, but what kind of rules will hide the GET parameter.
我想在URLManager 中创建一些规则,但是什么样的规则会隐藏 GET 参数。
采纳答案by Maug Lee
\win regexp means ?word“ character and such url part as ?my-prety-page“ will NOT match. To hide GET params you must improve your urlManager rules. You can write such a rule for pages using SEF urls:
正则表达式中的\w表示 ?word” 字符,而诸如 ?my-prety-page“这样的 url 部分将不匹配。要隐藏 GET 参数,您必须改进 urlManager 规则。您可以为使用 SEF url 的页面编写这样的规则:
'<controller:\w+>/<id:\d+>/<title:[^\/]*>/*' => '<controller>/view'
In this case when you enter url
在这种情况下,当您输入 url
http://example.com/page/12/my-prety-title
a Page controller will be called to perform view action with id and title as arguments. It is the same if you enter this url:
将调用一个 Page 控制器来执行以 id 和 title 作为参数的视图操作。如果你输入这个网址也是一样的:
http://example.com/page/view?id=12&title=my-prety-title
The last part /*
in rule allows to keep additional params. E.g. if your address is
/*
规则的最后一部分允许保留额外的参数。例如,如果您的地址是
http://example.com/user/55/john-doe-junior/foo/bar/
in UserController
's actionView
you can write
在UserController
的actionView
,你可以写
echo '<pre>' ;
print_r($_GET);
echo '</pre>' ;
die();
and you'll see
你会看到
Array
(
[id] => 55
[title] => john-doe-junior
[foo] => bar
)
回答by MrSoundless
Add this url rule to the top of your url rules:
将此 url 规则添加到 url 规则的顶部:
'page/<view:\w>' => 'user/page'
I'm assuming the next:
我假设下一个:
- controller name: UserController
- action name: actionPage
- 控制器名称:UserController
- 动作名称:actionPage
If my assumptions are wrong, please provide the right controller name and action name, so I can fix the answer.
如果我的假设是错误的,请提供正确的控制器名称和操作名称,以便我修复答案。
UPDATE: fixed controller name based on comment
更新:基于评论的固定控制器名称
UPDATE2:
更新2:
If you want this to work for all actions in your controller, use:
如果您希望它适用于控制器中的所有操作,请使用:
'<action:\w>/<view:\w>' => 'user/<action>'
回答by Abhishek
uncomment these line from main.php
从 main.php 中取消注释这些行
'urlManager'=>array( 'urlFormat'=>'path', 'showScriptName'=>false, 'rules'=>array( 'MyFirstYii/post/<view>'=>'MyFirstYii/post', '<controller:\w+>/<id:\d+>'=>'<controller>/view', '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>', '<controller:\w+>/<action:\w+>'=>'<controller>/<action>', ), ),
And put .htaccess file in root directory of project and write following code
并将.htaccess文件放在项目的根目录下并编写以下代码
RewriteEngine on # if a directory or a file exists, use it directly RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # otherwise forward it to index.php RewriteRule . index.php
回答by Oscar
If you intend to use GET and need these parameter, you can't hide it, as this is the way GET works. If you really need to hide the parameter, you must switch to POST, as then parameters will be passed in the request payload instead of in url.
如果您打算使用 GET 并需要这些参数,则无法隐藏它,因为这是 GET 的工作方式。如果您确实需要隐藏参数,则必须切换到 POST,因为这样参数将在请求有效负载中而不是在 url 中传递。
回答by Shekhar Sharma
use post method instead of get....that's the best and efficient solution.
使用 post 方法而不是 get ......这是最好和有效的解决方案。
to follow up on your query check out this site:
要跟进您的查询,请查看此网站:
[http://pure-essence.net/2007/06/29/simple-php-path-rewrite/]
[http://pure-essence.net/2007/06/29/simple-php-path-rewrite/]