O'Reilly logo

PHP and Smarty on Large-Scale Web Development by Vitor Rodrigues, Bruno Pedro

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Putting It All Together

At this point you need to choose a web server to serve your content. For the purpose of this Short Cut, we chose to use the Apache web server (http://www.apache.org/). We chose it because it is easy to understand, and it's the most commonly used web server at this time.[]

The approach described on the following pages is rather unconventional for applications using PHP and Smarty, but it is an excellent and elegant way of implementing the MVC pattern.

Let's start with a simple filesystem layout. This filesystem layout is flexible, and you should feel free to change this example to fit your needs whenever necessary:

app
  - lib
    - smarty
    - pear
    - apis
    - smarty
    - SOAP
    - ...
  - handler
  - config
  - docroot
  - cache
    - smarty_compiled
    - smarty_cached
    - other_cache

According to this layout, the Smarty template engine will be installed in app/lib/smarty and PEAR will be installed in app/lib/pear. To run the following examples, make sure the app directory is in your PHP include path.

The following code exemplifies how to add a new path to your PHP include path:

<?php
ini_set('include_path', ini_get('include_path') . ':/path/to/your/app');
?>

Handling HTTP Requests

A single PHP script handles all HTTP requests. This script is responsible for choosing the right template and loading the required PHP code. We'll call it handler.php from now on.

There are two ways to deliver all the requests to the handler.php script, either through Apache's mod_rewrite module or through PHP's auto_prepend ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required