在我的web应用程序中,我想有url到帖子,就像在这里堆栈溢出。例如:
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保持完全相同,当我粘贴它,就像它发生时,你在浏览器中粘贴Stack Overflow链接,并点击Enter。
我需要它是一个get请求,因为用户可以直接使用链接,因此我的控制器需要直接从它获取信息。
用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>
您的.htaccess
看起来好像已经更改为将所请求的uri作为参数q
的查询字符串变量传递回index.php。
而不是:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?q=$1
试着把它改回Laravel的原始版本。
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
嗨,你可以为postName变量添加一个模式
Route::get('/{postId}/{postName}', function($postId, $postName) {
return "$postId - $postName";
})->where('postName', '.*');
你的问题发生在生产还是在Laravel的开发服务器上?