Actual directory overwriting Laravel route

会有一股神秘感。 提交于 2019-12-06 06:03:45

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/*

The issue is with the following line:

RewriteCond %{REQUEST_FILENAME} !-d

In your example this tells the web server not to send the /admin route to Laravel because it is a physical directory. If you remove that then the /admin route should work. This will however prevent browsing directly to a folder and getting a directory listing, which should not be a problem. The next line in the htaccess will allow you to link to asset files contained in your admin directory and not have them get processed by Laravel, so that should not be removed.

The newer version of Laravel also contains:

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

It should be updated to the following in order to avoid a redirect loop on the admin route:

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

So if using the newest version of Laravel, your htaccess file should look something like this:

<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>

For routes.php:

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

For using assets in your public container you can now use:

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

Where assets in the /public/assets/js/jquery.js

Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!