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>
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.
来源:https://stackoverflow.com/questions/18063959/actual-directory-overwriting-laravel-route