Introducing Dump Server0:00
Now, if I run php artisan, you'll find a new command in Laravel 5.7 called dump-server. This is kind of cool, it's a wrapper around Symfony's VarDumper that sets up a dump-server to very cleanly review all of the various VarDumps on your site. Also nice is this is simply a community package by Marcel. You'll learn more about him when we review Laravel Nova. Anyhow, let's get started. Let's visit our routes/web.php file, and I'm simply going to dump hello world. Okay, if I were to view this in the browser, here's what I'd get. However, if I were to instead run the new dump-server command, alright, we can see a server is listening, we can quit with Ctrl C, nice and easy.
However, if I were to instead run the new dump-server command, alright, we can see a server is listening, we can quit with Ctrl C, nice and easy. Let's boot it back up though. Now, if I give it a refresh, I don't see anything, instead it catches that dump, it hides it from the UI because often it'll interfere and sometimes if it gets locked within a p-tag or something, you're looking at the source to figure out what you dumped, it's a big nightmare. Instead, we can review that data here. How cool is that? Let's do another one.
Dumping Route Data1:09
How cool is that? Let's do another one. Let's set up a route for maybe the about page, and I don't know, maybe we're grabbing all users, maybe users in this app represents administrators, the owners of the site. So we'll say import my User class and give me all of them, and then we will return some kind of view. We'll just do welcome again since I don't have an about page. Anyhow, if I were to dump users along the way, and if we go back to Chrome, we now visit the about page, once again, I don't see anything. However, in the console, I will.
the about page, once again, I don't see anything. However, in the console, I will. So here's the data that we dumped. We visited the about page, and the server picked up that we dumped this User collection here. Why don't we instead cast that to an array? Okay, one more time, refresh, and we'll now find that data here. Or maybe, one last example, you're testing out your contact page. So when we submit a POST request to create a new contact, it'll email you or something like that, maybe we accept some information through the query string as the request.
Testing POST Requests2:08
So when we submit a POST request to create a new contact, it'll email you or something like that, maybe we accept some information through the query string as the request. Maybe you just want to see what you're getting there. Okay, let's dump all of the request data. Now I don't have any contact view set up, so let's try this out from the terminal. I like to use a tool called HTTPie. I'll show you that in a second, but very quickly, let's do a POST request to laravel57.test/contact, and I will send through my name. All right, so if we run this up, it blew up. Let's see.
Handling CSRF Protection2:41
All right, so if we run this up, it blew up. Let's see. It looks like we have an HTTP exception. Oh, and you know what? I know exactly what this is. So we're trying to submit a POST request, and out-of-the-box Laravel provides CSRF protection, but we never included a token. So let's go to app/Http/Kernel.php, and just because we're tinkering around, I'm going to visit my web middleware group, and I will turn that off. Otherwise, within your routes file, you could instead store this route within api or something.
my web middleware group, and I will turn that off. Otherwise, within your routes file, you could instead store this route within API or something like that, which is not bound to the web middleware group. Okay, anyways, if I were to give that another run, once again, we don't see anything because it was dumped over here. There it is. Let's do it again. HTTP gives us a nice way to pass params that will automatically be escaped. So this would be the same as using a query string URL. All right, we give that another run, and there is the data coming in.
Using HTTPie and Postman3:32
So this would be the same as using a query string URL. All right, we give that another run, and there is the data coming in. If you like that and want to install it, visit http://httpie.org. It's really simple. Alternatively, you could use any number of apps available for Windows or Mac or Linux. Another one I like is called Postman. It's a little more intensive, and you can grab this from the App Store on your Mac. So for example, if I want to make a POST request to that same endpoint, laravel57.test/contact?name=Sally. We'll send that, and once again, we should now see it pop up here.
contact name is Sally. We'll send that, and once again, we should now see it pop up here. You can also usually send through JSON if you need to. So for example, let's go to headers. Let's set the Content-Type here to application/json. That's what we're sending through. Then we'll go to body, and we're going to send through raw JSON. And then here, you know, we could say take five users or whatever, order by their age, anything you want. And now that will be sent through in the request.
anything you want. And now that will be sent through in the request. So if I submit this again, we should still have name equals Sally, but that should be merged with this data as well. So if we come back, there we go. Name is Sally, and then the two items from our JSON request. So yeah, that's all there is to it. Now remember, this will work for dump. It'll also work for dd. So yes, you'll still kill the execution.
Dump Server with dd4:52
It'll also work for dd. So yes, you'll still kill the execution. Like if I come back and give this a refresh, yes, we have died. However, that dump has also been swallowed up and spit out here. So expect to see me using this dump server in every future Laracast lesson.
