Manual
Table of contents
MVC architecture
Generalities - Crash course
The framework is built upon the MVC architecture. This architecture divides the code in 3 parts :
- The controller, which does the back-end computing. It mades the model and the view work together.
- The model, which does interface the database with the controller. In an object-relational mapping, it represents your data structure in the code.
- The view, which is simply what the final user sees (the frontend)
In Namespace-PIP
In Namespace-PIP, as in PIP (and also a lot of other frameworks), the framework divides these parts into different files. These files are put in the root folder for your website, /application/
(relative to the framework's root) :
- in the
controllers
folder for the controllers, - in the
models
folder for the models, - in the
views
folder for the views.
There is already an sample controller, a sample model and a sample view in the framework. You can use them to start builing your website, they contain a minimal setup for one page. There is a few rules for making your components in Namespace-PIP :
- Every controller is a class extending the
Controller
class. - Every controller has to be in the
Controller
namespace. - In a controller, a page is represented by a function, bound to a route in the config file.
- Every model is a class extending either the
Model
class (using a database) or theModelNoDB
class (not using a database). - Every model has to be in the
Model
namespace. - The view is just a PHP file, containing the HTML and the few structures (conditions and iterations) that will allow to output variable data.
For example, here is an annotated version of the example controller :
<?php namespace Controller; //The controller is defined into the Controller namespace. use \View\View; //Don't forget to include all the classes from other namespaces you'll use. //It includes Models and the View class, each in their respective namespaces. class Main extends Controller //The controller extends the Controller class. { public function index() //This method is bound to a route (in the config), so it's a page. { $template = new View('main'); //We instanciate a View from the view file main.php $template->render(); //We render it and show it to the world } } ?>
In extension to theses rules, the way you put the classes in the files is also important. If you keep one class per file (and name the files after that class name), the framework will do the association alone and load the class when you'll need it. If not, you'll need either to include the file (if it's a model) or specify in the configuration all the files which need to be loaded for each class with $config['autoload']
(there is already data in it, as an array, be careful to not overwrite it).
The Controller
As said above, the controller is the starting point of your page. It receives the data from the client (see Routing section), processes it (optionally with a Model), creates a view and passes the data to it. It's also the central point of your page.
In the controller (which represents a section of your website), a page is represented by a method. The method can take parameters, which are given by the routing system if it is correctly configured. The sample controller (above) defines only one page (named "index") with no parameter. Note that, with default routes defined in config (the ones supplied in config.sample.php
), the method called when you don't specify it in the URL will be index
. This behavior can be changed just by editing the configuration file.
The controller gives you a few functions (only two at the moment) to help you :
Controller::redirect($url)
: Redirects the client to another page, using the Location header. The$url
parameter has to be a virtual path (i.e. a path that will be routed) to the destination page, from the site root.Controller::HTTPReturnCode($code)
: Sets the return code of the page, and its message. You just have to put the desired return code as parameter. If you want to have a list of available codes, they're in/system/utils/http_codes.php
.
$this->
.
To call models and views in your controller, you just have to instanciate the classes (using new
, after declaring them in your namespace with use
), and start using it. Example :
<?php namespace Controller; use \View\View; use \Model\Example as ExampleModel; //Aliasing the model name is optional. class Main extends Controller //The controller extends the Controller class. { public function index() //This method is bound to a route (in the config), so it's a page. { $model = new ExampleModel(); $data = $model->getSomething(0); $template = new View('main'); $template->data = $data; $template->render(); } } ?>