Documentation
PFP is very similar in it’s architecture to CodeIgniter and will likely look very familiar to you if you already have experince with CodeIgniter. However remember that PFP is a tiny framework and does not include a lot of the functionality that comes with CodeIgniter.
Model-View-Controller
PFP is based on the Model-View-Controller development pattern. MVC is a software approach that separates application logic from presentation. In practice, it permits your web pages to contain minimal scripting since the presentation is separate from the PHP scripting.
- The Model represents your data structures. Typically your model classes will contain functions that help you retrieve, insert, and update information in your database.
- The View is the information that is being presented to a user. A View will normally be a web page, but can be any type of “page”.
- The Controller serves as an intermediary between the Model, the View, and any other resources needed to process the HTTP request and generate a web page.
Folder Structure
With MVC in mind the physical layout of PFP is fairly simple. Your application specific files go in the “application” folder (you don’t need to touch the system folder). Inside the application folder there are folders for all of the specific application entities:
- config
- controller
- helpers
- models
- plugins
- views
When PFP loads files it assumes they are in the corresponding folders. So make sure you place your files in the correct folders.
We encourage you to use the “static” folder in the root to store you static resource files (CSS, JS etc) however you can put them anywhere. You can also use the BASE_URL
variable to help include files in your HTML. For example:
<link rel="stylesheet" href="<?php echo BASE_URL; ?>static/css/style.css" type="text/css" media="screen" />
Naming Conventions
All classes in PFP use PascalCase naming. The associated file names must be the same except all lower case. So for example the class ThisIsAClass
would have the filename thisisaclass.php
. Underscores in classes must be included in file names as well.
URL Structure
By default, URLs in PFP are designed to be search-engine and human friendly. Rather than using the standard “query string” approach to URLs that is synonymous with dynamic systems, PFP uses a segment-based approach:
example.com/class/function/param
By default index.php
is hidden in the URL. This is done using the .htaccess
file in the root directory.
Controllers
Controllers are the driving force of a PFP application. As you can see from the URL Structure, segments of the URL are mapped to a class and function. These classes are controllers stored in the “application/controller” directory. So for example the URL…
example.com/auth/login
… would map to the following Controller with the filename auth.php
:
<?php
class Auth extends Controller
{
function index()
{
// This is the default function
// (i.e. no function is set in the URL)
}
function login()
{
echo 'Hello World!';
}
}
?>
… and the output would be “Hello World!”.
The default controller and error controller can be set in application/config/config.php
Note that if you need to declare a constructor you must also call the parent constructor like:
<?php
class Example extends Controller
{
public function __construct()
{
parent::__construct();
// Your own constructor code
}
}
?>
There are several helper functions that can also be used in controllers. Most of these functions take the parameter $name
of the corresponding class:
loadModel($name)
- Load a modelloadView($name)
- Load a viewloadPlugin($name)
- Load a pluginloadHelper($name)
- Load a helperredirect($location)
- Redirect to a page without having to include the base URL. E.g:
<?php
$this->redirect('some_class/some_function');
?>
Views
This version of PFP automatically appends a header and a footer to every view. The content of these sections is in application/views/header.php
and application/views/header.php
.
Usually, the header will look like:
<html>
<head>
<title>My Site</title>
</head>
<body>
And the footer:
</body>
</html>
This means that the views usually contain the “content section” of a page. Views are almost always loaded by Controllers. So for example if you had a view called main_view.php
that contained the following HTML:
<h1>Welcome to my Site!</h1>
… you would load it in a controller by doing the following:
<?php
class Main extends Controller
{
function index()
{
$template = $this->loadView('main_view');
$template->render();
}
}
?>
The resulting HTML page would then be:
<html>
<head>
<title>My Site</title>
</head>
<body>
<h1>Welcome to my Site!</h1>
</body>
</html>
The View class has a helper function called set($key, $val)
that allows you to pass variables from the Controller to the View.
<?php
$template = $this->loadView('main_view');
$template->set('someval', 200);
$template->render();
?>
… then in the view you could do:
<?php echo $someval; ?>
… and the output would be 200
. Any kind of PHP variable can be passed to a view in this way.
Models
In PFP models are classes whose sole responsibility it is to deal with data (usually from a database).
For example:
<?php
class Example_model extends Model
{
public function getSomething($id)
{
$stmt = $this->prepareStatement('
SELECT *
FROM something
WHERE id = ?');
$stmt->bindValue(1, $id, PDO::PARAM_INT);
$result = $this->executeStatement($stmt);
if(count($result) == 1)
{
return $result[0];
}
return null;
}
}
?>
… then in a controller you would do:
<?php
function index()
{
$example = $this->loadModel('Example_model');
$something = $example->getSomething($id);
$template = $this->loadView('main_view');
$template->set('someval', $something);
$template->render();
}
?>
Now the results of your database query would be available in your view in $someval
. Note that in the above code there are no steps to connect to the database. That is because this is done automatically. For this to work you must provide your database details in config.php
:
<?php
define('DB_DSN', 'mysql:dbname=test;host=localhost'); // Database DSN
define('DB_USERNAME', ''); // Database username
define('DB_PASSWORD', ''); // Database password
?>
There are several helper functions that can also be used in models:
prepareStatement($sql)
- Prepares and returns a statement from a queryexecuteStatement($statement, $parameters = null)
- Executes a statement, with or without parameters as an array, and returns an array containing all of the result set rows (indexed by column name)executeStatementUpdate($statement, $parameters = null)
- Executes an update statement, and returns the number of updated rowsexecuteStatementInsert($statement, $parameters = null)
- Executes an insert statement, and returns the last inserted id
Helpers and Plugins
There are two types of additional resources you can use in PFP.
- Helpers are classes which you can use that don’t fall under the category of “controllers”. These will usually be classes that provide extra functionality that you can use in your controllers. PFP ships with two helper classes (
Session_helper
andUrl_helper
) which are examples of how to use helpers. - Plugins are literally any PHP files and can provide any functionality you want. By loading a plugin you are simply including the PHP file from the “plugins” folder. This can be useful if you want to use third party libraries in your PFP application.