7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial
FREELessons: 13Length: 1.4 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.3 Create Route Controllers

Now that you have a route in place, you need to be able to handle web requests that are sent to that route. To do this, you will need to create a PHP class and method, and then map this method to your route.

2.3 Create Route Controllers

Let's now finish up this routing example by creating our first controller. So the first thing that I wanna do is I wanna grab our controller name here, or the fully qualified name here. Now let's go ahead and copy that cuz we're gonna need to use that. And we need to be very certain that we have defined our namespace and our controller class the exact way that we had defined it within our routing file. Now you could create your controllers first, or you could create your routing files first. Either way, it doesn't really matter. Just make sure that you are using consistent naming and that it is the same across the board so that PHP and Drupal can find it and use it within your module. So now we're gonna start creating some custom code within our module. So let's go ahead and create a new folder. So let's create a new folder. We're gonna call this src. And then within here, let's go ahead and create another folder, and we're gonna call this, Controller. Now, within here where's we're gonna start to create our controller. So what I wanna do is I wanna create a new file. And this is going to be my CalculatorController.php. So here's going to be our first bit of logic. So the first thing that we need to do when we're writing PHP logic is we need to open and close our PHP tags here just like this. And the first thing that I'm gonna wanna do is I need to define what namespace I'm working in. Now, remember, within my routing file, I defined my namespace as such, Drupal\calculator\Controller and my class is CalculatorController. So the way that we're gonna handle this now is we're gonna say namespace and we're gonna specify that namespace. So now I'm going to grab my class name off of here. So my namespace is going to be Drupal, calculator, and Controller. Now I need to define a class. And that class is going to be called CalculatorController. And then we're gonna do open and close curly brackets, and here is our class. So, so far so good, we've created our class, we've created it within a namespace. So let's go ahead and save this. And just for yuks, we're gonna come back over here to our website. And let's go ahead and clear our caches this time and let's see what happens. Well, we still get an error, so let's come back over here and check PHP. And this is going to say, method hello does not exist. Okay, well, now we see we have to make sure we've done that second part as well. So I need to define a function within here, or a method, depending on whatever you wanna call it, that's going to be called hello. So I need to create a public function, and we're gonna call it hello. And we're gonna do open and close parentheses here as we don't really want to specify any parameters. We're gonna get to that, but for now let's just keep things fairly simple. So let's go ahead and save that now. Let's come back over to our application. Let's go ahead and clear our caches again. Okay, so now our caches are cleared. So now, theoretically, if I went to my route, maybe we'd see something happen. And so the route I want is my/calculator. So let's go ahead and copy that. Let's come back over to our site. And let's just go ahead and paste that in there, my/calculator. Well, the website has encountered an error. Please try again later. Okay, what's the error this time? The controller must return a response, null was given. Did you forget to add a return statement? Well, yes, as a matter of fact I did. Now this is where we start to get into some of the logic of our application. When it comes to Drupal and adding content to your site, the way that we handle that is by specifying a controller and a function, as I've done here. And then we have to return an array of values. An associative array, that's going to define a couple of special keys that have values. And those keys are predefined, that we can use, and the two that we're gonna use most often right now are gonna be title and markups. So let's see how those things work. So right now, I'm going to return an array. And within here, I'm going to specify two keys. Now, as I said before, those are gonna be title and markup. But it's not just title and markup there. They are very special, and it's gonna be #title. So that's gonna be the first key. Now I can specify a value in here, and let's just call this Greetings. All right, that's gonna be the value of title. And then we are also going to specify #markup. Now, as you might imagine, markup is where we're actually allowed to specify HTML markup. So I can put just about anything in here that I want that is valid HTML. But for now, I'm just gonna put some basic text in there, and we'll continue to evolve this over time as well. So, I'm just gonna say something like, hello there, just like that. So let's go ahead and save our changes. We will come back over here, and just for good measure, we're gonna clear our caches. And once that is done, I'm gonna come back over here and I'm going to execute my path again. And, look at that, we have greetings and hello there. So, as you saw before, I put in #title. So I can come in here and actually take a look at this and see what's going on. And, as you can see here, I have a title in here. So this is just kinda some predefined ways that I can get access to certain things. So, in this case, I'm able to specify an h1 title of the content that I want to display on my site using #title. And the same will go for our markup. If you look over here, and I look at Hello there, let's go ahead and inspect this. And down here, this is going to specify our content. So by specifying #markup, that's where I'm actually going to define the content that is going to be shown on my site. So, nothing too exciting here, but we have gotten to a point where we can now create a module, we can create some custom routes within there that are going to contain some custom logic. Within there, we're defining our controller, in this case, our CalculatorController as well as the function or the method that are gonna handle the requests coming in. And then we can create that controller in PHP, return an associative array with some keys and values, some specially defined keys and values, and then actually show some content on the site. So you've really learned an awful lot in just the first couple of lessons on how to do that. But I will agree with you. I can see already in the back of your mind, this is a little dull, this is a little boring. There's really nothing else I can do with this. I mean, I can just refresh this over and over again. But what if I wanted to customize it, what if I wanted to add a parameter, what if I wanted to pass in somebody's name and do some concatenation and some HTML formatting and all that sort of good stuff? And my answer to you is, let's check that out in the next lesson.

Back to the top