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
28. Creating an example user profile page

Episodes

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

Transcript

00:00
So let's solidify what we've learned about accessing the database and in
00:04
particular using models, and we want to be able to click on each of these users and go through to a dedicated profile page for that user and show some data about them. So if we head over to our web routes, this is where we're going to define out a new route for a user's profile.
00:22
So let's start to do this now. And we'll say get. And in here, we're going to say users slash. And then we want to include the ID or some sort of identifier of the user
00:35
so we can pull them out of the database. For this, we use placeholders wrapped in curly braces, and we just pass the name of the variable that we expect here. And then we go ahead and pass this over to a controller or a closure.
00:47
So let's go and create out a new controller. We'll grab the structure of the dashboard controller to keep this simple. And we'll call this user controller. So let's say user controller.
00:59
And we'll just go ahead and paste this in here. OK, so we need our view because we want to be able to render a view out. And we need a response. And we want to render something like user.twig.
01:09
You can put these in directories, by the way. So if you wanted to put this in a user's directory, just do this with a slash. OK, let's create the view out for this. So user.twig.
01:18
And for now, let's just put user in there just to make sure this is working. Head back over to our web routes and we'll reference that user controller in here, which doesn't exist at the moment. So let's just make sure we rename this.
01:35
And back over to our web routes, let's pull this in like so. OK, great. So now what we can do is over on our home view, we can link this up. So let's add in an href in here to link through to that page.
01:54
And in here, we want to say users slash. And then we want to take the user ID. Normally, we wouldn't use an ID. But since we're working with limited data, let's just leave it at that.
02:06
OK, so now I can click through to users slash one, two, or whichever user I want to access. So let's start with Mabel and we'll go ahead and access that user within the user controller. So when we use placeholders in our web routes like this, these get passed into our controller. So these are an array of arguments that we can use.
02:32
Let's just dump these out here just to see them. And then we'll extract them out and plug that user out of the database. So let's grab our arguments, come over, give that a refresh. And the user now matches up with the user that we're requesting.
02:47
So what we can do with this is either access this directly, or we could go ahead and destructure the arguments and give this a variable name. So we can take the arguments, pluck the user out of here, grab the user ID, and then we can look this user up.
03:01
So to do this, we can create a variable out here that represents the user. We can either use the database directly or we could use our models. So let's just use our models here. And we could say something like find passing in the user ID.
03:16
If we do a var dump on this again and kill the page, you'll see that we get back an instance of that user, app models user. And this contains a bunch of stuff, but it also contains all of the data inside of there as well. Our dump functionality isn't very pretty at the moment,
03:32
so we might change that up a little bit later. We could also use the dump function, which comes from Symfony, which we also use within Twig templates. And you can see that looks a lot better as well.
03:43
So under the attribute section, it just gives us all of the information about the user we want. And really, we can just pass this user directly down to our view and then start to extract any of the data out that we need. So we could just do this.
03:57
And then over in user, we could go ahead and create our page. So let's take one of our existing pages just to keep things simple, pull it over, and let's change this up. So we'll go ahead and set a title here.
04:09
So let's set the block title in that block there. And in here, let's output the user's name, user.name. And we can do this here as well, user.name. Remember, user is an object.
04:22
So now we're just accessing a property on that object using twigs.notation. OK, so down here, you could create some details about the user if you wanted to, depending on what you had in your database. And when we give that a refresh, there we go.
04:37
So if we head back to home and click on any of these others, this is now updating the title and the header on the page with all of that user's information. So you'll want to use the same kind of flow for any data
04:49
that you want to grab from the URL and display out here. There's a couple of other things that you can do as well. For example, if we had user ID of 10, that doesn't exist. But at the moment, what we're doing is just passing down an empty model to our view.
05:04
Now, what we can do is say something like find or fail. Or you could just do a manual if statement and throw a custom exception, which we'll be looking at later. But find or fail will just fail the request with no query result.
05:17
So it will just give you an error. And of course, if debugging is off, this will just give a standard error on the page or just not show anything. We'll look at this later, though, because we'll want to customize things
05:28
like 404 pages if this happens. So we'll come back to this a little bit later. But find or fail is a good option because we don't want to show the page at all if the user doesn't exist.
05:38
OK. So now that we've done that, we have now looked at an example of grabbing data from the URL, showing it on the page. And you can use this to build pretty much anything you need to.
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!