Integrating On Mvc Applications
If you are going to use PHPFront on MVC-based applications (websites), you have, at least, two integration methods. And either will be perfect.
- Native Integration: This is where PHPFront is deployed as the View component of the MVC application.
- Add-on Integration: This is where an MVC application has a built-in View, with PHPFront instantiated from this native View.
MVC is a software design pattern that follows the Model-View-Controller architecture.
Sample MVC
Consider a rough MVC setup. (This may not be typical of real-life MVC applications.)
Model
Models would normally be some database access library or set of libraries.
Here, we will be OK with the PHP Database Object (PDO) class as the only thing in our model. With it, we'll select data from our database.
Here is the Model in a file named Model.php
.
Model.php
Class Model
{
// Holds the PDO object
public $PDO;
// Constructor. Instantiates PDO
public function __construct()
{
// Setup database connection
$this->PDO = new PDO("mysql:host=127.0.0.1; dbname=_DB_NAME_; charset=utf8", "_DB_USER_", "_DB_PASSWORD_");
// Be sure to use working values for _DB_NAME_, _DB_USER_, and _DB_PASSWORD_
}
// Gets rows of data from the 'users' database table
public function getUsers()
{
$query = $this->PDO->query('SELECT name, email, username FROM users');
// Returns 3 rows
$all_users = $query->fetchAll();
return $all_users;
}
}
Controller
Controllers would normally be some class or set of classes that responds to HTTP requests from the user, performs appropriate tasks in the Model, gives results back to the View for display. Controllers generally interface between the Model and the View but isolate them.
Here, we'll code a controller class for ourself in a file named Controller.php
.
Controller.php
Class Controller
{
// Holds the Model object
public $Model;
// Holds the View object
public $View;
// Constructor. Instantiates PDO
public function __construct()
{
// Setup model
$this->Model = new Model;
}
// Sends all data to View for rendering
public function createPage()
{
}
}
View
Views would also normally be some class or set of classes for parsing all the contents from Controllers and displaying them. They would also translate user interactions into HTTP requests back to Controllers.
Now, for our View, we have two options: just use PHPFront (Native Integration) or code a View class from scratch and add PHPFront (Add-On Integration).
One more thing, let's borrow the template used on our Hellow World Example
and add a table element after the <p>
element.
Notice that we're creating only a row with a single column even though we hope to get, at least, 3 rows with 3 columns of data.
This skeleton for a table is enough for PHPFront to work with. It will help us complete the remaining rows and columns using Repeats.
<!-- Users Table -->
<table id="users_data">
<tr><td></td></tr>
</table>
Native Integration
This is where PHPFront is deployed as the View component of the MVC application.
So our View will be PHPFront.
Its role is to receive data from the Controller through its assign()
function and then displaying it.
Now, let's complete our Controller class with PHPFront integrated. After that we'll be done with our application.
Controller.php
In the constructor, add the following code:
// Instantiate PHPFront
$this->View = new PHPFront;
In the createPage()
function, add the following code:
// Get all data from model and send to the #users_data table in the template
$this->View->assign('#users_data', $this->Model->getUsers());
// Display page
$this->View->render();
From the obvious, the whole application, right from the start, is recognizing PHPFront as its View component.
This is native, or inbuilt, with the core application.
Add-on Integration
This is where an MVC application has a built-in View, with PHPFront instantiated from this native View.
In this case, we will create a View class. Data gathered by the Controller will be built up on this native View.
Then from this View, it will be handed over
to PHPFront either individually using PHPFront's assign()
function or as a whole using PHPFront's assignDataStack()
function.
Our View class will be in a file named View.php
Class View
{
// Holds all data from Controller
public $data = array();
// Constructor
public function __construct()
{
}
// Sends the built-up data to PHPFront
// Renders it
public function display()
{
// Instantiate PHPFront
$PHPFront = new PHPFront;
// Assign
$PHPFront->assign('#users_data', $this->data['all_users']);
// Render
$PHPFront->render();
}
}
Now, let's complete our Controller class with the native View added.
In the constructor, add the following code:
// Instantiate our native View
$this->View = new View;
In the createPage()
function, add the following code:
// Get all data from model and add to the View::$data array
$this->View->data['users_data'] = $this->Model->getUsers();
// Display page
$this->View->display();
Preview
Let's run our MVC app. The Model.php
, Controller.php
, View.php
, and the PHPFront folder are all in the root directory of our server.
Now, we'll also create an index.php
file that will kick-start the process. Browse to your server to run our app.
// Include the necessary files.
include 'Model.php';
include 'Controller.php';
include 'View.php';
include 'PHPFront/lib/PHPFront.php';
// Our Controller is the entry point
$app = new Controller;
// Create page
$app->createPage();
Both the Native Integration and the Add-on Integration above will give the same result.
Also, let's take an interest in the #users_data
table.
Notice how PHPFront produced a fully formed table from the single-row, single-column structure we coded.
Our $all_users
data could have been any number of rows of any number of columns, and PHPFront Repeats would have still nicely completed the rest.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> - PHPFront</title>
</head>
<body>
<header>
<nav id="menu">
<a id="link1" href="/phpfront/"></a>
<a id="link1" href="/phpfront/about"></a>
<a id="link1" href="/phpfront/follow"></a>
</nav>
</header>
<main>
<h1>[Heading 1]</h1>
<p>[This is default text.]</p>
<!-- Users Table -->
<table id="users_data">
<tr><td>Ryan</td><td>ryan@example.com</td><td>ryanexample</td></tr>
<tr><td>James</td><td>james@example.com</td><td>jamesexample</td></tr>
<tr><td>Jack</td><td>jack@example.com</td><td>jackexample</td></tr>
</table>
<aside id="sidebar_wrapper">
</aside>
</main>
<footer>
<small> (c) 2016 Me </small>
</footer>
</body>
</html>
Compare Integrations
While the application's MVC working principles remained the same, implementations were diferent and one was easier.
Native PHPFront integrations are faster, and are the simplest method of finishing your front-end - after all the hard work in the Model and Controller.
From your Controller, you could obtain simple content or nested arrays - complex array of any dimension. And, instead of creating a View to manually loop over these data, you just hand them over to PHPFront. Your application is ready!
Native PHPFront Integration is the implementation in the BraveCode Web Application Framework!
True, Add-on PHPFront integrations have a bit of extra work of creating a native View.
But this may be neccessary where:
(1) there are other things to do with data just before rendering by PHPFront,
(2) we do not have permissions to replace the application's existing View,
Or, (3) we want support for multiple template engines, in which case the native View will hold the data and decide the template engine to use.
This way, undeploying PHPFront or switching between template engines will have nothing to do with the rest of the application but the few lines of code in the View.