For this you will use Middlewares .
Middlewares
Middleware provides a mechanism for filtering HTTP requests in the
application. For example, Laravel includes middleware that
verifies that the user of your application is authenticated. If the user
is not authenticated, the middleware will redirect the user
to the login page. However, if the user is authenticated,
the middleware will allow the request to move forward in your application.
Auth Middleware
Laravel already comes with some middleware for you to use, and one of them is auth
. You can apply this middleware in a number of ways.
Specific Route
Assigning the middleware to the route via the fluent method.
Route::get('admin/posts/create', function () {
//
})->middleware('auth');
Routing Group
Assigning the middleware to a group of routes.
Route::middleware(['auth'])->group(function () {
Route::get('admin/posts/create', function () {});
Route::get('admin/user/profile', function () {});
});
Via Controller
You can assign direct to the controller as well.
class PostsController extends Controller
{
public function __construct()
{
// Nesse caso o middleware auth será aplicado a todos os métodos
$this->middleware('auth');
// mas, você pode fazer uso dos métodos fluentes: only e except
// ex.: $this->middleware('auth')->only(['create', 'store']);
// ex.: $this->middleware('auth')->except('index');
}
}