Laravel 5.1 page authentication using routes

- 1 answer

Ad

I'm working on a site that needs an admin panel. I am currently trying to set up the authentication of that panel, though I can not find a way to deny access from any guest users (non-admins). I have a login page, of course, and after login, it routes to the admin page, though you can also go to /admin when you're not logged in.

routes.php :

Route::get('home', function(){
if (Auth::guest()) {
    return Redirect::to('/');
} else {
    return Redirect::to('admin');
}
});

Route::get('admin', function () {
    return view('pages.admin.start');
});

MainController.php :

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class MainController extends Controller {

 public function getIndex() {
    return view('pages.index');
 }

 public function getAbout() {
    return view('pages.about');
 }

 public function getPortfolio() {
    return view('pages.portfolio');
 }

 public function getShop() {
    return view('pages.shop');
 }

 public function getContact() {
    return view('pages.contact');
 }

 /*public function getAdmin() {
      return view('pages.admin.start');
 }*/
}

I could really use some help here, because I'm totaly stuck, and yes, I have read the documentation, though maybe I'm just missing something.

Ad

Answer

Ad

Assuming you have a line like this:

'auth' => 'App\Http\Middleware\Authenticate',

in your app/Http/Kernel.php file:

put all the routes you need "authenticated" inside the grouping, but keep the "guest" routes outside of them :

Route::get('home', function(){
if (Auth::guest()) {
    return Redirect::to('/');
} else {
    return Redirect::to('admin');
}
});



Route::group( ['middleware' => 'auth' ], function(){
    Route::get('admin', function () {
        return view('pages.admin.start');
    });
    Route::just-another-route()...;
    Route::just-another-route()...;

});

Documentation: http://laravel.com/docs/5.1/routing#route-groups

Ad
source: stackoverflow.com
Ad