Laravel slash after url redirects to root folder(url重定向到根文件夹后的Laravel斜杠)
问题描述
我是 Laravel 的新手,它似乎是一个很棒的框架,但我有一个关于路由的问题.我想将所有 get 请求路由到一个名为 PageController@view 的控制器操作,但是当前缀为 admin 时,我希望它路由到 AdminAdminController@view.我有以下代码:
I'm new to Laravel and it seems a great framework but i have a question when it comes to routing. I want to route all get requests to one controller action named PageController@view but when the prefix is admin i want it to route to AdminAdminController@view. I have the following code:
Route::get('/{slug?}', 'PageController@view');
Route::group(array('prefix' => 'admin', 'namespace' => 'Admin', 'before' => 'auth'), function()
{
Route::get('/', 'DashboardController@main');
});
但是当我转到:/admin 时,Laravel 将其视为 slug 而不是前缀.第一行是否有某种 except 函数,或者我是否只需切换上面的行即可获得正确的结果?
But when i go to: /admin, Laravel treats it like a slug instead of a prefix. Is there somekind of except function for the first line or do i simply have to switch the above lines to get the right result?
新问题:
但是现在我在访问 http://www.mysite 时遇到了一个新问题.com/laravel/public/admin/,Laravel 将我重定向到 http://www.mysite.com/admin/ 而不是调用 AdminController@main.当我像这样删除末尾的斜杠时:http://www.mysite.com/laravel/公共/管理员它工作正常.
But now i have a new problem when i go to http://www.mysite.com/laravel/public/admin/, Laravel redirects me to http://www.mysite.com/admin/ instead of calling AdminController@main. When i remove the slash at the end like this: http://www.mysite.com/laravel/public/admin it works fine.
推荐答案
如果您以相反的顺序指定这些路由,它应该可以工作.先做最具体的路线,然后再做更通用的路线.把它想象成抓住这个、这个和这个,然后当以上都不匹配时回退到这个通用的".
If you specify those routes in the opposite order it should work. Do your most specific routes first, and the more generic ones later. Think about it as a "catch this, this, and this, and then fall back to this generic one when none of the above match".
您的新问题在于 Laravel 4.1 附带的(坏的,IMO)重定向规则.规则非常假设您的 Laravel public
目录位于服务器的文档根目录.这是规则:
Your new problem lies in the (bad, IMO) redirect rule that Laravel 4.1 ships with. The rules very much assume that your Laravel public
directory sits on the document root of the server. Here's the rule:
RewriteRule ^(.*)/$ /$1 [L,R=301
如您所见,这是说如果当前 URL 以斜杠结尾,只需将其重定向到/{url}",从而摆脱斜杠.这一切都很好,只要您的安装位于服务器的文档根目录中即可.
As you can see, that's saying "if the current URL ends in a slash, just redirect it to /{url}", thus getting rid of the slash. This is all well and good, just as long as your installation sits on your server's document root.
您可以通过在 public/.htaccess
RewriteBase(在您的情况下为 RewriteBase/laravel/public
)来解决此问题> 文件,但当然,这现在会因环境而异.这就是为什么我认为将 Laravel 4.0 的 RedirectIftrailingSlash
功能更改为 .htaccess
规则是一个坏主意.
You can fix this by specifying a RewriteBase
(in your case RewriteBase /laravel/public
) above that rule in your public/.htaccess
file, but of course, this will now change from environment to environment. This is why i consider the changing of Laravel 4.0's RedirectIftrailingSlash
functionality to be a .htaccess
rule to be a bad idea.
或者,您可以完全删除 RewriteRule
并尽量不用担心您的应用现在会同时响应 /laravel/public/admin
和 <代码>/laravel/public/admin/.
Alternatively, you can remove that RewriteRule
entirely and try not to worry about the fact that your app will now respond on both URLs /laravel/public/admin
and /laravel/public/admin/
.
这篇关于url重定向到根文件夹后的Laravel斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:url重定向到根文件夹后的Laravel斜杠
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01