Dashboard API 404 issue0:00
For this next example, I've put together a little scenario. We have a new dashboard root in this Laravel 11 project. It's protected by auth, and it returns this dashboard view. Inside this dashboard, I've imported AlpineJS, and here at the bottom, essentially, I have an interval set up where every second, I'll go ahead and grab the latest users in our application, which I'm hoping to output in this card here, but we haven't set up the root yet. You can see we're getting a 404 not found inside the console. So let's fix that. Your reflex reaction will be to go into routes.php and open API.
Install API with Artisan0:38
So let's fix that. Your reflex reaction will be to go into routes.php and open API. There is no API.php file, don't panic. This is part of Laravel's minimal skeleton, because you don't need an API when you first start for most projects, but it's very easy to set up. We don't have to do any work manually. In fact, go to the terminal and type php artisan install:api. It will go ahead and install Sanctum as a dependency, which is not required by default anymore. It will also go ahead and create that file, and it will even update our bootstrap.
Configure Sanctum middleware1:14
anymore. It will also go ahead and create that file, and it will even update our bootstrap/app.php. Let me show you. Let's open up bootstrap/app.php and note that in the with routing helper, it's actually configured this for us. We could do these steps manually if we wanted to, or if we weren't using Laravel Sanctum and wanted to use a different API provider instead. Obviously I do want to use Sanctum, and I want to make sure that my API requests are using stateful authentication. So I'll chain that onto the end of our middleware that we added in the last episode.
Create latest users endpoint1:43
using stateful authentication. So I'll chain that onto the end of our middleware that we added in the last episode. I believe it's stateful API, and that will do all the work for me. Finally, we need to make sure that we add the hasApiTokens straight to our User model. Let's head to the User model and drop that in place, hasApiTokens, and then we'll migrate our database. With setup complete, we can now jump into api.php, and let's edit the default route to be latestUsers, and latestUsers is going to return User. Just get them in the latest order, we'll limit it to the last five, and then we'll get the results.
Just get them in the latest order, we'll limit it to the last five, and then we'll get the results. And note the middleware being used here is indeed authentication via Sanctum. Now if we come back to the front end and refresh, you can see that every second it's updating, and because users are being added to our application, we see those new users appear in front of our eyes. So no, Laravel 11 isn't scaffolded for APIs out of the box, but it's literally a single php artisan command away. I think that's a nice compromise. The same is true for broadcasting, it won't be available out of the box, but it's easy
Install broadcasting scaffolding2:52
I think that's a nice compromise. The same is true for broadcasting, it won't be available out of the box, but it's easy to install using php artisan install:broadcasting, which will publish our broadcasting routes file, as we can see here, channels.php, and will also configure our application for us. bootstrap/app.php now also includes channels in the with routing helper. Additionally, if we go down to our resources directory, note that a new echo.js file has been created with the Laravel echo setup that's been removed by default, but installing broadcasting adds it back in, and our bootstrap.js file is updated to import that new echo.js file. Again, you could do it manually, but it's nice that Laravel has that command already.
adds it back in, and our bootstrap.js file is updated to import that new echo.js file. Again, you could do it manually, but it's nice that Laravel has that command already and configured for us.
