Laravel, Admin controllers - 403 Forbidden

2020-07-26 06:44发布

问题:

I am trying to create Admin controllers in the default controller folder. I have created "admin" folder in the "controller" folder.

In the routes file:

Route::get('/admin', 'admin/AdminController@showAdminIndex');

AdminController.php file:

namespace Admin;

class AdminController extends \BaseController {

    public function showAdminIndex()
    {
        return "Hello World";
    }

}

I get an error on the browser:

403 Forbidden

What went wrong?

回答1:

The problem is that you have an admin subfolder in the public directory. The .htaccess that ships with Laravel only boots the application if no directory or file exists at the requested URI (that's why CSS and other assets still work)

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

You basically have two options here:

  1. Rename either one, the route or the folder. If public/admin is for assets you could put it in public/assets/admin for example.

  2. Change your .htaccess to not ignore folders for rewriting

Like:

# Handle Front Controller...
# RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]`