MVC and similar patterns ======================== Phraw can be used with a MVC pattern, a MVP pattern and similars because does not obligate the use on one or another: Phraw does not make a choice for you. The MVC pattern is very popular for web applications. MVC stands for Model, View, Controller. This little guide show how to simply use it. The Controller is Phraw itself and the router section: takes the requests from the browser and coordinate the application. The Model is the layer that operate on datas like databases, files and so on. The View is the layer that creates the response for the browser. There is another layer, the Presenter, that are the templates; this is because a template engine, like Smarty, can execute a bit of logic in order to generate the HTML. MVCP example ------------ In this example we build a tiny website that shows a name selected from a little database. Controller ^^^^^^^^^^ For more informations see the :doc:`routing` guide. In the "index.php" file, simply create the routing rules, this is just a simple example: .. code-block:: php detect_no_trailing_slash()) { # Fix the trailing slash $phraw->fix_trailing_slash(); } else if ($phraw->route('', 'equal')) { # Routing for the home page require_once('resources/views.php'); view_home($phraw, $smarty); } else { # Page not found $smarty->display_error(); } ?> Models ^^^^^^ Create the "resources/models.php" file. Here is where put database functions and objects. The file name "models.php" is just a suggestion. In this example build an array to use like a database: .. code-block:: php View ^^^^ Create the "resources/views.php" file. Here is where put view functions or objects. The file name "views.php" is just a suggestion. The view displays the name requested by the ``$_GET['name']`` parameter. .. code-block:: php assign('name', null); } else if (isset($people[(int) $_GET['name']])) { $smarty->assign('name', $people[(int) $_GET['name']]); } else { $smarty->assign('name', false); } $smarty->assign('people', $people); $smarty->display('home.html'); } ?> Presenter ^^^^^^^^^ Create the template "resources/templates/home.html". .. code-block:: html {if $name === false}

Sorry, the name is not on the database.

{else if $name === null}

No name selected.

{else}

The name is {$name}

{/if}

Please, chose a name:

    {foreach $people as $key => $value}
  1. {$value}
  2. {/foreach}
  3. Fake
Now try to see this little web site on the browser.