From the course: PHP: Design Patterns
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Solution: Apply the model-view-controller pattern - PHP Tutorial
From the course: PHP: Design Patterns
Solution: Apply the model-view-controller pattern
- This solution has a lot of moving parts. So let's break it down piece by piece. First of all, going from the URL patterns I gave you, we know that we should get a pair of parameters for every request. A module and an action. These map to a controller and an action respectively. Here's how the index.php file breaks down: We take the module parameter and convert it to a controller, and then pass that action into the controller itself as a parameter. Now let's look at the GreetingController. In the constructor, you can see that we're loading the model of a similar name. Then we have two methods, hello and goodbye, that retrieve a message from the model and merge it with the template. Now before we look at those, let's look at the default controller, which we extended to build this one. This controller just makes sure that we have a valid action, and attempts to call it. Nothing interesting, but very important. Now let's look at the Greeting model. It has two methods, which each return…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
(Locked)
Introducing the model-view-controller pattern2m 30s
-
(Locked)
Exploring a use case for the model-view-controller pattern2m 37s
-
(Locked)
The model-view-controller pattern in action1m
-
(Locked)
Challenge: Apply the model-view-controller pattern59s
-
(Locked)
Solution: Apply the model-view-controller pattern1m 41s
-
(Locked)
-
-
-
-
-
-