问题描述
我是Laravel的新手,它似乎是一个很好的框架,但是我有一个问题,当涉及到路由。我想将所有获取请求路由到一个名为PageController @ view的控制器操作,但是当前缀为admin时,我希望它可以路由到Admin \AdminController @ 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 Admin\AdminController@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把它当成一个s lug而不是一个前缀。有没有一些除了第一行的功能,或者我只需要切换上述行来获得正确的结果?
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?
新问题:
但是,当我去,Laravel将重定向到,而不是调用AdminController @ main。当我删除这样的结尾的斜线:它的工作正常。
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附带的(bad,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.
您可以通过指定 RewriteBase
(在您的情况下 RewriteBase / laravel / public
)在您的 public / .htaccess
文件,但是当然,现在将从环境变为环境。这就是为什么我认为改变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/
.
这篇关于网址重定向到根文件夹后的Laravel斜线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!