2

I'm working in my first project with Laravel, it's a simple website with an admin panel:

In my "public" folder I have a directory called "admin" where I put all the styles and scripts corresponding to the admin panel. I've also defined a route in my app to handle the "admin" GET:

Route::get('/admin', 'Admin\DashboardController@index');

The problem is that since I have that "admin" folder in my public directory Laravel is ignoring the "admin" route I defined, so I can't access the proper controller. I'm suspecting it has something to do with the .htaccess but I'm not sure how to solve it. This is how my htaccess looks right now:

<IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
4

3 回答 3

2

问题在于以下行:

RewriteCond %{REQUEST_FILENAME} !-d

在您的示例中,这告诉 Web 服务器不要将 /admin 路由发送到 Laravel,因为它是一个物理目录。如果您删除它,那么 /admin 路由应该可以工作。但是,这将阻止直接浏览到文件夹并获取目录列表,这应该不是问题。htaccess 中的下一行将允许您链接到管理目录中包含的资产文件,并且不会让 Laravel 处理它们,因此不应将其删除。

较新版本的 Laravel 还包含:

# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]

应将其更新为以下内容,以避免管理路由上的重定向循环:

# Redirect Trailing Slashes...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

因此,如果使用最新版本的 Laravel,您的 htaccess 文件应如下所示:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
于 2014-07-15T13:42:34.170 回答
1

You cannot have a public directory with the same name as your route - otherwise how will Laravel know whether "/admin" is for the controller, or for the style etc.

You should store your admin style sheets etc under /assets/admin/*

于 2013-08-06T15:50:03.903 回答
-1

对于 routes.php:

Route::controller('admin/dashboard', 'Admin\DashboardController');

要在公共容器中使用资产,您现在可以使用:

<script type="text/javascript" src="{{Request::root();}}/assets/js/jquery.js"></script>

/public/assets/js/jquery.js 中的资产在哪里

希望这可以帮助。

于 2013-08-06T16:08:18.667 回答