This episode is for members only

Sign up to access "Build Your Own PHP Framework" right now.

Get started
Already a member? Sign in to continue
Playing
29. Setting up Sentinel

Episodes

0%
Your progress
  • Total: 4h 45m
  • Played: 0m
  • Remaining: 4h 45m
Join or sign in to track your progress

Transcript

00:00
Over the next handful of episodes, we're going to focus on authentication. Once again, we need to go ahead and find a package that's going to work nicely within our own framework. For that, we're going to choose Sentinel.
00:10
Now this is a tried and tested authentication package. Works really nicely with Eloquent, which we've already pulled in. So in this episode, we're going to get this installed. We're going to configure our database schema because at the moment we just
00:23
have a random table with just a couple of columns and we're going to get this working. Okay. So the first thing that we want to do is install this. Now we are going to go over to the native integration section, first of all, because
00:36
we're not using this with Laravel. We need to pull in a couple of additional dependencies, illuminate events and symphony HTTP foundation. So let's go over and do a composer require on both of these and
00:49
just wait for them to finish. And then we'll go ahead and pull in the Sentinel package. So let's go over to installation and let's pull this in. Now we actually want to pull in version seven.
00:58
So let's go and change this over to seven. We might get an issue with running this just because of some of the dependencies that we have are later versions of the illuminate components, but let's run this and fix this up if we come across any issues.
01:12
Okay, great. So you can see here that this requires the version 10 of the illuminate components. That's not a problem. We can just downgrade these by just switching these over to 10.0.
01:23
And we'll do the same thing for the events as well. Switch that to 10.0. Let's just run a composer update to fix them quickly. And once that's done, we'll try and pull in this package again.
01:37
And there we go. Great. So that's now pulled in and we can start to configure it before we do anything. Let's head back over to the docs and just see how this works.
01:45
So let's go over to the authentication section and see what we need to do. So as you can see, we have this facade, this Sentinel facade. It's basically just a wrapper that contains all of the methods of the concrete implementation of the Sentinel class.
02:01
So if we want to authenticate a user, we use Sentinel authenticate, and we just pass in a bunch of credentials. Now, what we don't want to do is use this facade throughout our application. There's a couple of reasons for that, which we'll get to later.
02:16
So what we're going to do is we're going to register this specifically in our service provider. We're going to boot it up, pass in the config that we need from our own application so we can configure this.
02:27
Then we're going to go ahead and set within the facade, the instance that we've configured, and then register that on our container. So kind of like what we did with our auth service provider, where we booted everything up, and then we went ahead and attached it to our container.
02:44
So let's go ahead and look at how we do this. Of course, we're going to need a service provider. So let's go over to our provider section. Once again, just grab any of these and we'll go ahead and create one out.
02:56
We'll call this auth service provider. And we'll paste this in, making sure we change the name of this to auth service provider. And of course, getting rid of anything and boot just in here.
03:08
Okay. So to use Sentinel, it's already in our application. So anywhere we need to authenticate, we can just easily use Sentinel and then authenticate or whatever else we need to do.
03:19
We don't want to do that. Like I said. So what we're going to do is create out a manual bootstrapper for Sentinel. So let's say new Sentinel bootstrapper.
03:29
And if we just take a look at this class, this takes in the config that we want to use. Now the config by default is hidden away within the package. We want to control this config.
03:41
So the first thing I'm going to do is just search for Sentinel config and pull out that config file. You can see that this comes directly from this package. This looks an awful lot like what we've done already.
03:52
So we've already created some config files in our own app, which just returns an array. So what we're going to do is we're going to take this entire config file from the Sentinel package.
04:03
And we're going to create this inside of our own application. So let's go over to config. Let's create out a PHP file called auth. And let's just paste in all of that config.
04:14
Now we'll be looking at some of this config a little bit later, but you can go ahead and tweak this to do whatever you need. Okay. So now that we have got that, we can access our config from our
04:25
container and pass it directly into here. So let's grab our container. Let's grab our config instance from our container, and let's go ahead and get all of the auth config.
04:38
So we don't want a specific piece of config. We want to pass it all into the Sentinel bootstrapper so it can be created with all of the stuff that we need. Next up, we'll go ahead and we'll set the bootstrapper that we've
04:50
manually created to the Sentinel facade. So if we just open up the Sentinel facade, which is just here, native facade Sentinel, this doesn't do much. It just uses this instance of the Sentinel class to grab
05:06
anything out that we need. So what we can do is we can, first of all, apply this to a variable so we can use it. So Sentinel, and we're going to use the Sentinel facade.
05:18
So that's under native and facades. And we're going to set the instance manually to the own bootstrap version just here. So rather than just access the facade on its own, we've taken an extra step to
05:32
set our own config before we do that. So next up, we're going to do a similar thing that we did with our database service provider. And we're going to create a Sentinel instance up here, and then we're
05:45
going to set that down here. So this Sentinel, and we're going to set that to the Sentinel variable that we've just created, but we're going to get the Sentinel instance from that.
05:58
Let's just die dump on this to see what we've got. So let's do a var dump and kill the page here and put that in there. Not forgetting over in our config and app to register this auth service provider out.
06:13
Okay. Let's head over to our homepage, give this a refresh. And there we go. We've now got an instance of Sentinel and this class, if we just dump on
06:20
it instead, will contain all of the data or all of the methods as well. And the stuff that we need to authenticate. So now that says a property under register, we can register this so we can pull it out of our container.
06:36
So once again, let's say get container. Let's add in Sentinel in here. And this is just going to return to us the Sentinel instance. So let's just return this Sentinel.
06:52
And once again, make sure we set this as shared. Okay. So last step, just make sure that we add this to our list of services in here, and we should now have access to our authentication functionality.
07:04
Now there's one more thing that we need to do before we get this working. And that is have the correct database schema. So Sentinel can read from our database. Now, luckily we have an SQL dump under MySQL 5.6 plus of the schema that we need.
07:19
So we've got things like activations, persistencies, reminders, roles, users, all of that kind of stuff. So we're just going to take all of this schema and we're going to go ahead and put this directly into our database.
07:31
So I've deleted the users table, which if you've done so already means we get an error here because it can't find the users table, we're just going to go ahead and paste this in and just run everything in here. Okay.
07:41
So if we give this a refresh, you can see that's created a bunch of tables for us. Users pretty much looks the same, but we've now got a first name, last name, any permissions they have, and really importantly, the email and the password, which we're going to use to authenticate.
07:56
So if we head over to our app now and give this a refresh, you can see sure enough, it's working nicely. We don't have that data in the users table anymore. So it's not being shown over on the homepage and we'll want to update that as well.
08:07
So just while we're here, let's go over to our home.twig file and output the user's first name instead. There are no records in there, so we won't see anything. But once we get to registering users, we will see the users appear on the homepage.
08:24
Okay. So we have successfully pulled Sentinel in now. The first thing we want to do now that we've got everything set up is create the registration flow to register users.
54 episodes4 hrs 45 mins

Overview

Starting completely from scratch, build a modern PHP framework with all the features you’d expect.

Whether you’re new to PHP or not, this is a great exercise for learning what happens under the hood, arming you with knowledge you can apply anywhere you use PHP.

We’ll cover routing, controllers, views, the container, accessing the database, models, authentication, config, CSRF protection, exception handling, pagination, validation, flashing messages and much more.

Let’s dive in and build a PHP framework, step-by-step!

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!