Laravel 5.1 Page Authentication Using Routes

I am working on a site that needs an admin panel. I'm currently trying to configure authentication for this panel, although I cannot find a way to deny access to any guest users (not administrators). Of course, I have a login page, and after logging in, it goes to the admin page, although you can also go to / admin when you are 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 was completely stuck and yes, I read the documentation, although maybe I just missed something.

+4
2

, :

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

app/Http/Kernel.php:

, "" , "" :

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()...;

});

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

+1

Middleware .

1) , , , , , ; - :

class AdminMiddleware
{
    public function handle(Request $request, Closure $next )
    {
        //if User is not admin
           //redirect to no permess

        return $next($request);
    }
}

2) , , admin:

//bind the middleware to all the routes inside this group
Route::group( ['middleware' => 'adminmiddleware' ], function()
{
    Route::get('admin', function () {
       return view('pages.admin.start');
    });

    //other routes   
});
+1

Source: https://habr.com/ru/post/1619876/


All Articles