This episode is for members only

Level up with a premium membership

Join now
Already a member? Sign in to continue
Playing
47. Custom 404 (and other error) views

Episodes

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

Transcript

00:00
Let's think about how we want to render a 404 page inside of our exception handler. Now there's a couple of ways that we could do this. We could make this manual. So for example, we could grab from the exception here the status code.
00:14
So let's grab the status code from our exception. And if that's the case and it equals 404, we could go ahead and build up a response in here, return it, and then we could pass that down to here to be rendered out with our response. We're not going to do that.
00:32
We want to kind of make this as automated as possible. So we're going to borrow something from another framework, Laravel, where when we go over to our view section, we can just create out a directory in here called errors.
00:44
And then we can have an error named after the status code. So let's go ahead and create that 404.twig file out. And we're just going to say not found. Now you can make this as beautiful as you want now that it's a view.
00:58
And once we render it, you can change that over. OK, so over to our exception handler. Inside of here, we now want to go ahead and check if we have a status code for that view. So how do we do this?
01:11
Well, let's go and first of all, pull in our view functionality inside of our constructor so we can actually render a view to a response. So we're going to go ahead and pull this in like so. And let's pull this up because we're not going to need anything else in there.
01:26
OK, so in here, let's think about how we want this to work. Let's say we want to grab out a view from another method. We'll keep this as tidy as possible. So we'll say get error view.
01:38
Let's pass in our exception because that contains our status code. So for example, this was 404. This method could look inside of our errors folder to see if a 404.twig file exists. Now, if that's the case, let's just kill the page here and say 404,
01:56
and then we'll fill in this method. So let's go and create out this get error view method. And we know that we're accepting into this and a throwable exception. So we'll just call that throwable for now.
02:10
And the first thing that we want to do is check if the throwable contains a status code. So let's dump on that exception. And let's see what we get. So I'm going to go ahead and hit this.
02:23
And yeah, sure enough, we get our not found exception dumped out. Now, you can see that we've got this status just here. Let's go ahead and open up the not found exception from the router and see what we've got. I think it's probably this one here.
02:38
And let's have a look. Yeah, so we've got a not found. Yeah, the code is 404. We're calling the parent constructor to set that.
02:45
And if we just look at this base exception, you can see that we have a get status code method. So we know that that is an exception, an HTTP exception, which has a status code. So what we can do before we do anything is just check if this method exists. So if the method doesn't exist on the exception, so we can use this.
03:06
It's just a native PHP function to check if a method exists within a class. We just want to return null from here because we know that we can't look for a view if the status code method doesn't exist. Next, what we can do is build up the path to the view that we want to check.
03:25
So we've put this in an errors directory. So let's go ahead and build this up now. So errors. And then we'll have from the exception, get status code.
03:35
And then we will end up with the Twig extension because all of our views end with Twig. OK, so let's go and just dump on the view now. So when we hit that 404, we should see the path to our 404 page. Great.
03:49
Now, the next thing we want to do before we render this is check if the view actually exists. If it doesn't exist, we're just going to rethrow the exception. Otherwise, we're going to render it. So how do we do this?
04:01
Well, we built our own view class earlier, and we have our render method, but we don't have an exists method. So let's go ahead and build that out now. It's pretty straightforward because Twig already has this functionality.
04:14
So we want to take in here a view as a string to check if it exists. So let's go and access Twig. What we do to check if something exists is grab the loader. So it's a little bit different than just calling a single method on this.
04:28
Then we're going to use the exists method to check that view. So now we can just access view from our container and check if a particular view exists. So let's go over here and create out another if statement, and we'll check if it doesn't exist.
04:42
So view exists, and we'll pass that view that we've just built up into here. If it doesn't exist, again, we just want to return null. And then up here, view will not be truthy, or we could return false, and then we'll just not do anything.
04:58
OK, so last but not least, we want to return the rendered version of this template. So we're going to return this view, and we're going to render out that view that we've built up. And what you could also do is pass through any additional data that you want. And let's just change that over to render, of course.
05:19
Now, what you could also do is pass down any additional data you want. So you could even pass the exception down into this so you could access any information from that. We're not going to do that, but we now have the flexibility to do that
05:31
because we have it available. OK, so now this will return a rendered view. Let's go up here, and we'll dump 404 just to check this is still working. So we know that the view exists because we are getting something back from here.
05:44
Let's go ahead and dump on the view and see what we get. And there we go. We get not found. Now, this is the HTML content.
05:51
So for example, if your template contains some markup, that is what we're going to get back, and that is what we can write to the response. So let's open up our exception handler again. Let's just preview this in the browser.
06:03
And there we go. So this is what we want to now write to the response that we get through to here and then return it back. So how do we do this?
06:14
Well, we've already seen this within our controllers. We want to grab the body from the response that we're passing through to here. We want to write the view. So that's the HTML.
06:25
And then we want to return the response from here. Now, because we're potentially returning a response from our exception handler and not just throwing an error back over to our app class, we now have a return value for this.
06:38
So we're going to grab the return value, which could potentially be the response. And then now that we're doing this, we create a response. We assign it again here, or we assign it again here. And then we emit that response.
06:52
So now within our exception handler, because we are returning this freshly written to response, this is now going to overwrite it. And it's going to emit the new response, which of course is our custom 404 page. So there we go.
07:08
We have created out a custom 404 by checking the status code of the exception that gets passed in. And of course, we're doing that over here and just making sure that that actually exists, checking if the view exists, and then going ahead and rendering out just down here.
50 episodes4 hrs 32 mins

Course 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!

Comments

No comments, yet. Be the first to leave a comment.