Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

surreal's avatar

General understanding of authentication (middleware/controller)

Hi there, first of all I must admit I'm still getting used to Laravel - great Framework and easy to handle/understand. But I'm struggling with the authentication middleware. I'm trying to create a backend for my site, which is only accessable if the user is logged in. I'm able to log in and get redirected to my desired page. So far so good. I used to do it with the web.php (routes) untill i've read on several threads/sites this is not recommended because the auth-middleware (http/middleware/authentication ?) should only be responsible for this. I also struggled at defining the routes, that the requested site is only available "if isAuth". To be a bit more precise: All files within one folder (e.g. /resources/views/backend) shall only be available if the user is logged in. Is it possible to "train" the auth-middleware/controller(?) to recursively protect an entire directory (and its subdirectories) instead of single files? Apart from that, how do i run a route only if the user is authenticated (else loginpage)? The Lessons "Rapid Authentication and Configuration" and "Basic Routing" dont seem to handle it the way i'd like it to be or i'm just plain stupid (which is a bit more likely)

0 likes
5 replies
Borisu's avatar

Hi,

you don't need to "protect" the files in your app, since the web server will only send the requests to the index.php in the public folder. Actually only files in that folder will be somehow accessible to the end-user. All this means that the routing is done via the application and not your web-server. That's where the web.php file comes in to play. There you register the routes you want, e.g.:

// in web.php
Route::get('/products', 'ProductsController@index');

The above example tells your application to call the index method on the ProductsController when a GET request to the uri /products is detected. In fact the application keeps a long list of all the routes you register and checks if the current user request matches any of them. If yes it just does whatever you told it to. Now to use the middleware you can do this:

// in web.php
Route::get('/products', 'ProductsController@index')->middleware('auth');

Now the application will see that the user wants a page you have registered. But it will also pass the request through a series of middleware classes which will examine the request and either approve or deny it. In this example the auth middleware will check if the user is authenticated and take appropriate action. Just to make it even clearer here's an example chain of what's happening:

GET 'https://site.com/products' -> web-server -> app/public/index.php -> Router (class) -> '/products' GET Route -> Auth Middleware -> ProductsController -> index method -> your code -> return response.

I hope this helps.

1 like
surreal's avatar

Hi Borisu, thank you for your quick reply. Does that mean the only location where Routes are being added/edited is in within the web.php though there might be a middleware involved while handling the route? What's it with the Route action (in your example "ProductsController@index") ? Obviously it's essential for routing (otherwise Route for []has no Action"). I can't find any about the correct syntax or possible variables/methods. As far is I know it is @ but what is actually doing? Index is more or less self-explaining. Do I define them by myself or are they predefind?

pascual's avatar

Hello, @surreal

The @'s in for example ProductsController@index refers to which method in the given controll will be returned.

Route::get('/products', 'ProductsController@index'); Will return whatever the index method inside the ProductsController returns when the end user accesses the /products url.

pascual's avatar
pascual
Best Answer
Level 6

And to get your toes wet when it comes to routing and simple communication with controllers, I highly recommend the "Basic Routing and Views" lection from Laravel From Scratch:

https://laracasts.com/series/laravel-from-scratch-2017/episodes/2

One of the many amazing things you can use the command line based tool Artisan for, is to generate controllers that have predefined methods for REST functionality. If you make a lot of those kind of controllers, Artisan will save you lots of time when developing.

surreal's avatar

Hi pascual, obviously I've I missed some important information mentioned in Episode 8 (https://laracasts.com/series/laravel-from-scratch-2017/episodes/8) about the correct usage of controllers and methods in Routes. I didn't create a function (e.g. in HomeController.php) which returned my desired view. Finally I also understand the sense of naming Routes (href="{{route('dashboard')}}"). It finally sunk in. Guess i'll start building my own Controller for my backend. Thank you very much @pascual and @Borisu for being patient with me! Best regards SuRReal

1 like

Please or to participate in this conversation.