Package service provider routes0:00
Alright, let's get started. Now, the first thing to understand, when you pull in that laracasts/cypress package through composer, here's the ServiceProvider that will ship with it. So immediately notice, if you're in production mode, we don't do anything. It's too dangerous. But otherwise, it adds this set of routes. Right here. Let's have a look. routes/cypress. And notice this convention here.
Routes slash cypress. And notice this convention here. For any action that we require, we expose an endpoint that defers to this CypressController. And that's basically the way it works. And the same is true for all of these endpoints. Even things like this. If you need to fetch a CSRF token. Alright? Let's have a look. Make a request.
Cypress CSRF command flow0:38
Let's have a look. Make a request. That generates one, returns it as JSON. And that's it for the server side. So then, on your cypress end, like I mentioned, we just make the POST requests. So for example, here's the cypress command called CSRF token. We can use it like this. And notice, it makes a request, a GET request, to that endpoint. And then we fetch the body of the result, and return it. So here's what I'm thinking.
And then we fetch the body of the result, and return it. So here's what I'm thinking. If I go back to that CypressController. And again, notice I'm editing the vendor code specifically, only because I'm playing around here. We're not keeping any of it. We're just in the tinkering phase, to figure out what we should do. So if you're wondering why I'm editing vendor code, that's why. We're not keeping any of it. Anyways, at the bottom, or at the very top, let's just do this like this, so it's easy,
Add endpoint for app routes1:17
We're not keeping any of it. Anyways, at the bottom, or at the very top, let's just do this like this, so it's easy, and not cluttered. What if we added a method here, to fetch your routes? Now in Laravel, you can fetch all of the routes, by using the Route facade. And then I happen to know there is a... I'm saying I happen to know, and then I can't remember. Is it called getRoutes? Something like that? Let's play around.
Something like that? Let's play around. php artisan tinker. route.getRoutes. Is that right? Yeah. And that will return a route collection. And for that route collection, there's another getRoutes method, that will turn that into what you see here. So notice we have an array of route instances, and each of those contains information about
Condense route metadata2:15
There's more than I require. So why don't we condense that down to only the information that our Cypress test might need. We'll begin by fetching those routes. Kind of weird. getRoutes, getRoutes. But I think that's right. And then I'm going to return the routes, but first I want to map over them, like this function. And I think that will be a route, like so. And now we could say something like, okay, what do we need here?
And I think that will be a route, like so. And now we could say something like, okay, what do we need here? Let's switch on back. I do want the method. I want the name. I want the URI. In some cases, we may need the domain as well. So a few things. So for example, if we just started with the name, I could say route, and we should have a bunch of methods here, like getName.
So for example, if we just started with the name, I could say route, and we should have a bunch of methods here, like getName. Let's have a look here. Gets the name of the route. Exactly what we want. We'll also need get, I said getDomain, get the domain defined for the route. Let's do that, domain, domain. What else? Get, should we get the action? So that would be the controller it points to.
Get, should we get the action? So that would be the controller it points to. You might need that. We'll include it. Route. Get. Action. And we might need to trim, if there's a slash, I'm not sure. But otherwise, what else do we need? The URI.
But otherwise, what else do we need? The URI. Route. get. URI. Is that right? get. Hmm. What is it? What's it called?
What is it? What's it called? Route. U. URI. Let's have a look. Yeah. I wonder why that doesn't follow the other conventions. Get. Anyways, who cares? So we have name, domain, action, URI.
Anyways, who cares? So we have name, domain, action, URI. And then we'll also need to know what the method is. Is it for a GET request or a POST request? So method, should we do methods or methods? Or method. Let's start with route. And what is it? I don't use this overly much. methods.
I don't use this overly much. Methods. Let's have a look. All right. Get the HTTPS verbs for the route. And I think that would return, excuse me, I think that would return, will it be something like this? Get head? Or will it be an array? What is being returned?
Or will it be an array? What is being returned? Returns an array. Yeah. So I guess that's going to return that. Because remember, one URI can respond to multiple request verbs. Okay. So in order to just play around with this, I can even do it in php artisan tinker again. Okay. Here's what we get.
Key routes by name4:45
Okay. Here's what we get. We have a collection where each item has the name, domain, action, URI, and method. This is what I think we want. But even better, what if instead of using an index for the key, what if the index for each item is the name itself? So we want to effectively key this array according to the name, item, or property. And we can do that pretty easily with Illuminate Collections by saying keyBy the name. Okay. So now if I ran this all again, notice if we scroll up, yeah, each key corresponds to
Register /cypress/routes endpoint5:16
Okay. So now if I ran this all again, notice if we scroll up, yeah, each key corresponds to a single route name. So we have our Cypress routes as well as any other routes the application has. But don't forget, this is a brand new project, so we don't have any named routes. And that's fine. Okay. So now that we know this works, at least our first pass at it, let's now expose an endpoint. I'm going to switch back to that routes file, create a new one here. If we make a GET request to /cypress/routes, that will hit the method routes, and
I'm going to switch back to that routes file, create a new one here. If we make a GET request to /cypress/routes, that will hit the method routes, and then we'll have cypressRoutes as the name. Great. So now think about it. On our server side, we've exposed a new route that when hit will return to you a collection of all named routes in your application. So in the next episode, we'll switch over and create a Cypress command to make that request.
