Protecting all admin/ routes with auth in Laravel

前端 未结 3 1156

I am brand new to laravel and am setting up admin panel authorization on my first application. The way I have my files setup currently setup is:

controllers/
             


        
相关标签:
3条回答
  • 2021-02-06 01:33

    In your admin/login route you have an unnecessary return before the Auth::logout() call, nuke that and it should fix it up.

    Another issue here is that only your one 'admin' route is getting filtered. You could wrap all of your admin routes with a Route::group() and apply the 'auth' before filter or you could use Route::filter('pattern: admin/*', 'auth') too.

    Check out:

    http://laravel.com/docs/routing#filters

    For the second issue, is your Admin Dashboard controller class named Admin_Dashboard_Controller and if so, do you have an action_index() or get_index() function in there returning a view?

    Check out:

    http://laravel.com/docs/controllers#nested-controllers

    (I'm assuming you're using L3 here btw.)

    0 讨论(0)
  • 2021-02-06 01:34

    So I was able to solve my problem a slightly different way. I created an (base) Admin_Controller in the root of the controllers folder, with a constructor calling the auth filter before execution:

    class Admin_Controller extends Base_Controller {
    
        public function __construct()
        {
            $this->filter('before', 'auth');
        }
    
    }
    

    and then made all my admin related controllers in /controllers/admin extend Admin_Controller and call the parent constructor:

    class Admin_Dashboard_Controller extends Admin_Controller {
    
        public function __construct()
        {
            parent::__construct();
        }
    
        public function action_index()
        {
            return View::make('admin.dashboard');
        }
    
    }
    

    This might not be the most eloquent solution, but it does the job!

    0 讨论(0)
  • 2021-02-06 01:49

    For future readers, a very clean way to handle this is using Laravel's Route Groups:

    Route groups allow you to share route attributes, such as middleware or namespaces, across a large number of routes without needing to define those attributes on each individual route.

    Route::group(['middleware' => 'auth'], function () {
        Route::get('/', function ()    {
            // Uses Auth Middleware
        });
    
        Route::get('user/profile', function () {
            // Uses Auth Middleware
        });
    });
    

    They can be used not only for authentication, but also Namespaces, Sub-Domains, and more.

    0 讨论(0)
提交回复
热议问题