【发布时间】:2015-08-11 01:50:36
【问题描述】:
在我的网络应用程序中,我希望有帖子的 URL,就像 Stack Overflow 中的这里一样。例如:
http://mydomain/posts/214741/this-is-the-postname
但是当我写这样的路线时:
Route::get('post/{postid}/{postname}',array('as' => 'postlink', 'uses' => 'Post@singlePost'));
现在发生的情况如下:
1) 将此链接粘贴到地址栏中:
http://mydomain/en/post/214741/postname
2)按回车,由于get请求,链接变为:
http://mydomain/en/post/214741/postname?q=post%2F214741%2Fpostname
但是,我想要的是 URL 与我粘贴时的 URL 完全相同,这与在浏览器中粘贴 Stack Overflow 链接并单击 Enter 时的情况完全相同。
我需要它是一个获取请求,因为用户可以直接使用该链接,因此我的控制器需要直接从中获取信息。
使用 Laravel 处理这种情况的最佳方法是什么?
我的.htaccess 文件是:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /public
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^ index.php [L]
RewriteRule ^(.*)$ /index.php?q=$1 [L,QSA]
</IfModule>
【问题讨论】:
-
您问如何将 {postid} 和 {postname} 传递给 Post 控制器的 singlePost() 方法?
-
“因为它是一个获取请求,所以查询字符串将显示在链接中” - 显示什么查询字符串?不是
postid或postname,确定吗?您是指提交get表单的结果吗? -
是的,但我希望按照我说的那样格式化链接
-
@Mr.Phil 问题就在您的 htaccess 中。您需要删除
RewriteRule ^(.*)$ /index.php?q=$1行,并将其替换为已注释掉的原始内容。您还应该删除rewritebase行,因为那可能没有帮助。为什么要更改原来的 htaccess? -
@halfer 当然,我会立即编辑我的帖子
标签: php string url laravel get