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

Solution: Apply the model-view-controller pattern - PHP Tutorial

From the course: PHP: Design Patterns

Start my 1-month free trial

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…

Contents