Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1

(^208) CHAPTER 14 ■ MVC ARCHITECTURE
Listing 14-3. The Bootstrap Loader



pico –w document_root/index.php
<?php
//Require Components
require_once('../application/models/front.php');
require_once('../application/models/icontroller.php');
require_once('../application/models/view.php');
//Require Controllers
require_once('../application/controllers/index.php');
//Initialize the FrontController
$front = FrontController::getInstance();
$front->route();
echo $front->getBody();
■Note You do not need a closing ?> in Listing 14-3, as the end of file will close the tag automatically. It is
not recommended to include closing tags, as newlines at the end of the file can result in premature output
and may cause issues sending HTTP headers.
The Front Controller
The front controller’s job is to parse the URL and to instantiate the controller and invoke the
action method. This is a singleton class (see Listing 3-1 in Chapter 3), which is responsible for
the application’s program flow.
The theory of operation is that URLs follow the format /controller/action/key1/value1/



.... Once the URL has been parsed, you will use the reflection API (covered in Chapter 7) to
invoke the action method on the IController implementing class.
To get started, first create the FrontController class, as shown in Listing 14-4.


Listing 14-4. The FrontController Class

> pico –w application/models/front.php

<?php

class FrontController {

protected $_controller, $_action, $_params, $_body;

McArthur_819-9C14.fm Page 208 Thursday, February 28, 2008 7:44 AM

Free download pdf