Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1

(^212) CHAPTER 14 ■ MVC ARCHITECTURE
Next, make a default view template, as shown in Listing 14-8.
Listing 14-8. The Default View



pico application/views/index.php
Hello, <?php echo $this->name; ?>!
That’s it. Reload your browser, and you should see “Hello, Kevin!”
URL Parameters
You’re probably not named Kevin though, so let’s integrate your name with this script. Because
of encapsulation, to do this, you need to modify only the controller.
Replace your application/controllers/index.php file with the file shown in Listing 14-9.
Listing 14-9. Using URL Parameters
pico –w application/controllers/index.php
<?php
class index implements IController {
public function index() {
$fc = FrontController::getInstance();
$params = $fc->getParams();
$view = new View();
$view->name = $params['name'];
$result = $view->render('../views/index.php');
$fc->setBody($result);
}
}
Now change the URL to http://yourdomain.com/index/index/name/yourname =, and you
should see “Hello, yourname!”
As you can see, creating an MVC framework is actually pretty basic. Well, this sample
framework is so basic that it provides almost no functionality.
The frameworks you will find on the market have significant built-in features, such as
database abstraction, data filtering and validation, and so on. In the next two chapters, you will
learn about the built-in facilities a typical MVC framework provides.
McArthur_819-9C14.fm Page 212 Thursday, February 28, 2008 7:44 AM


Free download pdf