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
14. Adding routing

Episodes

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

Transcript

00:00
Now that we have our request, the next step is to start to create routes. Now we're going to do this really roughly inside of bootstrap and app,
00:07
and then I'm going to show you how we can split this up into files for much better organization. Okay, let's get rid of what we've are dumping out here, and let's go and create a new service provider that will hold our router. So once again, let's go and grab any of these service providers that we've already got,
00:24
and let's create out a route service provider, which will hold this for us. Okay, let's paste this in, we'll get rid of what we don't need in boot, and let's change this over to route service provider. Once again, not forgetting to register this over in our config,
00:42
so route service provider, and we're done. We now have a new service provider specifically for our routes. Once again, we want to figure out what package we want to pull in to handle routing for us. We're not going to be building our own router,
00:55
because that is a huge job and would be a course in itself. So we're going to use fast route, but we're not directly going to use this. Fast route is an incredibly popular router, it's used by frameworks like slim. We are instead going to use league route, which uses fast route behind the scenes,
01:14
but it adds some additional functionality, like the ability to register controllers, apply middleware to our routes, which is really important, and a bunch of other things as well. So keep in mind that we are using fast route behind the scenes,
01:29
but we're using this route package to make our lives a lot easier. Okay, let's head over to the current version here, and let's go over to the installation and get started with this. So again, of course, we're going to use composer to pull this in.
01:43
So let's pull this into our project, and let's just start to use this. So we might just need to allow any dependencies to be upgraded. So we'll use the W flag for that, and now it's installed. Okay, let's add our router to our container,
01:58
and then we'll look at registering some routes. So we're going to grab our container once again, add in a item to our container, and this is going to be router from league and route. So let's name that with the class for that.
02:12
And then in here, let's make sure we set this to shared, like we're going to do with the majority of our services, and let's create a new router. So new router, and we'll return that router from here.
02:26
And we're going to do a couple of other things here in between. Now, one thing that we need to do with our router is set a strategy. You can find all of this in the documentation for league router, but we just want the standard application strategy.
02:40
We have other strategies like JSON strategies, which will decorate your responses with JSON if you're building an API. But for us, we just want this to be the standard application strategy. So in here, we're going to use our router and we're going to set a strategy.
02:55
This comes with a couple of strategies and you can also build your own, but we are going to set this to a new application strategy from this package. And then from this, we're going to set our container. So this is aware of our container and we are already in a service provider.
03:12
So we can just either pass in get container or container directly into here. Okay, that is our router now set up. It's easy as that to just do a couple of things to get this working. Let's try now over in bootstrap an app to register a few routes.
03:28
And like I said, we'll do this in here and then move this over a little bit later. So let's do this within the point where we initialize our app and then run it. Run will respond to any of the routes that we have created. So I'm going to create out a temporary router variable here
03:45
so I can access from our container, the router. So from league route like this, and then this is where we just register any routes. So let's say router and we'll say get, and this will just be our homepage. So let's set this to a forward slash.
04:00
That's obviously the route that we want this to respond to. And let's create out a closure in here and var dump home. And then we'll just kill the page inside of there. So at this point, now that we've got our router on here,
04:16
what we should be able to do is head over to our application and see some sort of response from this. Now we're getting an error here and we might see this quite commonly. What's happening here is the router or the route collector
04:30
expects the route parser to be passed in, but we've given a resolvable argument. The reason for this, if we head over to our route service provider is that we didn't register this as a service. So basically our container is not returning the router to us.
04:45
So let's go ahead and add in our router directly into here. And that should now resolve the router out correctly and pass this in for us. Okay. So if we give this a refresh, we're seeing run app.
04:56
So nothing is actually happening at the moment, but we are responding to that route or we actually accessing that route. So over in our app, let's just get rid of run app because we need to do something in here to actually respond to this.
05:10
Let's give this a refresh. And of course we see nothing in here at the moment, but we now know that we are potentially responding to this route that we've just created. Let's take a look in the next episode at how we actually respond to this router request.
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!