Playing
01. Using Whoops with Slim 4

Episodes

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

Transcript

00:00
In this snippet, I'm going to show you how to get WURPS set up with Slim 4.
00:05
And if you're not familiar with WURPS, it is essentially an error handler which by default renders a much nicer page than what you would probably be used to, particularly within Slim. So the first thing that we're going to do is go ahead and put in a fresh Slim application. I'm going to use the Cocoa Slender package for this, which will just set up a super simple
00:25
framework structure. So let's go ahead and do that, and what we'll then do is take a look at the default Slim error handling. So I'm going to go ahead and create this project, of course using Slim 4, and I'm just going
00:36
to call this Slim WURPS and just wait for this to finish. Okay, now that's done, let's head straight over to the text editor and just take a look at what happens when we see an error inside of a basic Slim application. So I'm not going to go through the structure of this Slender package.
00:52
It basically just sets up a very, very simple app with a couple of settings here and also Twig views, but we're not going to be focusing on them. Okay, so let's head over to the terminal. Let's go into the Slim WURPS directory, and let's go into the public directory, and let's
01:10
set up a really simple PHP web server running on localhost on port 8000. Let's go ahead and open this up in the browser just so we can see hello from app. Now we're going to test this out with a not found error. So the first thing that you can see when we have a page that isn't found within Slim by
01:26
default, we get this just plain PHP error, and this will be a fatal error because Slim is throwing an HTTP not found exception and it's uncaught. So at the moment, we don't even have basic Slim error handling. So that's really the first job.
01:43
Let's go over to the text editor, and just anywhere down here, we're going to go ahead and reference the app which we create with the AppFactory, and we're going to go ahead and add in Slim's error middleware. So we can use this using the addErrorMiddleware method over on the app class, and if we just
02:01
open up the app class and go to that addErrorMiddleware method, you can see that we pass three booleans through to here. Now this depends on your environment, so whether you want to display error details, whether you want to log errors, and also whether you want to log error details as well.
02:18
Now just for the sake of testing this out, I'm going to set all of these over to true, and I'm going to head back over to the browser and give this a refresh. So what's actually happening now is Slim is using an HTML renderer to render the exception out, but actually give you a little bit more information.
02:33
So you can see we've got a 404 not found header, and we've got all of the details, and also the stack trace as well. So we've got lots of useful information here, but it isn't quite what we would normally see from the whoops package.
02:46
Now we're not going to be pulling whoops in on its own. Really in this snippet, I just want to get across pulling this in as quickly and easily as possible, and that is going to be using the phpslim whoops package, and I'll leave a link for this in the course links for you.
03:02
So let's head down and let's get this installed and just check out how to get this working really, really simply. So I'm going to go ahead and just pull in slim whoops. Let's go back over to the terminal.
03:13
Just cancel this off really quickly and do a composer require on this, and I guess we could pull this version in as well specifically. It doesn't really matter what you do, it just depends on the version that you want to pull in.
03:25
But let's pull this in and let's first of all go back into the main directory before we try that. Okay, so now that's done, let's go back into public and let's reboot up that web server and let's come over to this page here.
03:36
Now of course nothing will have changed yet, but what this package provides to us is some middleware. So I'm going to comment out the add error middleware method that we've just created and I'm going to open up the whoops middleware that we get through with this package.
03:50
So you can see here that we've got a whoops guard. What's actually happening here is when we come across an error, this middleware will essentially catch this, and this whoops guard will deal with any of the settings that you pass through to this, which we'll look at very briefly, but most of these are on the
04:07
documentation. And you can see here that when we actually handle this or when we install this, we've got this setting here called enable. So we know that we specifically want to enable it.
04:17
And this is really useful because of course on a production environment, you don't want to show a pretty error page, it's really only for development purposes. So how do we get this whoops middleware set up? Well that's pretty straightforward because within Slim, all we need to do to add some
04:33
middleware is just use app add. That's as simple as possible. So I'm going to go ahead and add this whoops middleware that we've just taken a look at. And let's go ahead and pull the namespace in for this just at the top.
04:44
And let's come over and just check out what we see. Okay, so this looks like it's actually working. So by default, this is enabled. But like I said, what we want to do is make sure that we disable this when we're on a
04:55
production environment. Now this really depends on how your app is set up. But if we just head over to the text editor here, what we kind of want to do, I'm just going to pull this down just so it's a little bit tidier, is have some kind of signifier
05:09
with which environment we're on. And this is really, really important. So inside of the Slender setup, I have this EMV file. And this is being read using the .EMV library.
05:21
So it's pulling in that EMV file. And now inside of my project's environment or my server's environment, I now have these environment variables ready to go. So for example, I've got app name here, which we saw a little bit earlier when we booted
05:34
this project up, it said, hello from app, that was actually reading that value. If we just head over to the home controller here, you can see that it's getting this from the settings app name, passing that down to the view. And of course, over here, what we're doing is we're pulling that from our environment
05:50
using the native get EMV function. So what we want to do is use this app debug value to turn the whoops middleware on and off depending on the environment that we're working with. Now, just before we do that, let's just dump something out here.
06:07
I'm going to dump out the value of get EMV, which is that native PHP function and app debug. This is really, really important. Let's give that a refresh.
06:14
In fact, that's going to probably break, so let's just comment this out real quick. And let's give that a refresh. So we get true, but notice this is true as a string. So being really careful here, what we can actually do is to enable or disable this.
06:29
We can go ahead and using enable in here, we can compare if this equals the string true. So we don't really want to mess around with casting this to a Boolean. You can do if you want, but you tend to find that that will kind of mess things up, and it's a lot harder to do.
06:44
So what I tend to do is just compare this to the string value. So now when we come over, this is working. But if, for example, when we deploy our project and we have a completely different EMV file with new values, and we say app debug false, or if we have some kind of app EMV that might
07:04
equal something like local staging or production, it really depends on how your app's set up, then you can go ahead and enable or disable the middleware based on that. So now when we come over, you can see we just get the normal PHP error. And that's not too bad because on a production environment, you'll probably have PHP errors
07:23
turned off within your php.ini config. So we don't really need to worry too much about seeing this basic error. So that is just a little guide on getting whoops set up within Slim 4. There are a ton of options that you can pass through to this.
07:36
There are a ton of things that you can do with whoops using this intermediate package. But all of that information is over on the documentation. So rather than go through it, I'll let you have a look and see what you want to do. Most of the time when I'm pulling this in for a Slim 4 project, I just keep this as
07:52
I normally would. And as long as app debug is set to true, then I get the benefit of seeing this nicer page. Now, of course, a not found error is pretty standard. It's pretty easy.
08:03
We don't really need to see this stack trace or any environment details with a not found exception that's been thrown. But of course, if you have a slightly different error, then this will be really, really useful for you.
1 episode 8 mins

Overview

Whoops gives you a much nicer way to display errors while you're building. In this snippet, we'll get Whoops installed with Slim 4.

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

Episode discussion

No comments, yet. Be the first!