从 URL 隐藏 GET 参数

2022-01-04 00:00:00 php yii url-rewriting

如何隐藏 URL GET 参数 (http://domain.com/MyFirstYii/page?view=大约).我搜索了很多帖子.他们都在谈论重写和 URL 管理器,但我无法实现我想要的.:(

我的场景是,

我只想隐藏 URL GET 参数.

例如:

http://domain.com/MyFirstYii/page***?view=about***

我想隐藏***?view=about***.

然后 URL 应该看起来像这样 http://domain.com/MyFirstYii/page.其他类似这样的页面 http://domain.com/MyFirstYii/post.简单来说,我的 GET 参数应该像 POST 参数一样.

提前致谢.

我想在URLManager中创建一些规则,但是什么样的规则会隐藏GET参数.

解决方案

w 在正则表达式中表示word"字符,而诸如my-prety-page"这样的 url 部分将不匹配.要隐藏 GET 参数,您必须改进 urlManager 规则.您可以为使用 SEF url 的页面编写这样的规则:

'///*' =>'<控制器>/视图'

在这种情况下,当您输入 url 时

http://example.com/page/12/my-prety-title

一个页面控制器将被调用以执行带有 id 和 title 作为参数的视图操作.输入这个网址也是一样的:

http://example.com/page/view?id=12&title=my-prety-title

规则中的最后一部分 /* 允许保留其他参数.例如.如果您的地址是

http://example.com/user/55/john-doe-junior/foo/bar/

UserControlleractionView中你可以写

echo '

';打印_r($_GET);echo '</pre>';死();

你会看到

数组([id] =>55[标题] =>小约翰多伊[foo] =>酒吧)

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. :(

My scenario is,

I just want to hide the URL GET parameters.

Eg:

http://domain.com/MyFirstYii/page***?view=about***

I wanted to hide ***?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.

Thanks in Advance.

Edit:

I want to create some rules in the URLManager, but what kind of rules will hide the GET parameter.

解决方案

w in 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:

'<controller:w+>/<id:d+>/<title:[^/]*>/*' => '<controller>/view'

In this case when you enter 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:

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

echo '<pre>' ;
print_r($_GET);
echo '</pre>' ;
die();

and you'll see

Array
(
    [id] => 55
    [title] => john-doe-junior
    [foo] => bar
)

相关文章