A mini-framework

This website is based on a framework I created. This framework doesn't have any name, so I often call it 'mini-framework'.

I will try to explain how works this framework, but it's quite difficult, and you must comment if you don't understand something. You can also write me an email.

The idea behind this framework

To practice my skills in web technologies (such as PHP, mysql, javascript, html, etc.), I like to create websites from scratch. Like that, we can see a wide range of problems about how should be created a website.

A simple example is the communication with the database. In our university, we learned how to use mysqli. We used mysqli in functionnal mode (no Oriented Object Programming). The problem with mysqli is that we have to be very carefull about the security when we have something that comes from an user to put in the database. We have to use functions like htmlspecialchars and/or mysqli_real_escape_string. Then I discovered PDO. At first I thought it was less secured, or difficult to use. It's not. Yes, we have to use objects, and we have to do prepared statements, but this is the correct way to do mysql request nowadays, and once we know that, it's easy to use :)

The second problem I faced while creating a website from scratch was it wasn't structured at all. At first I used the knowledge I learned from school, but that was not the correct way to do things (we used functionnal PHP and wrote our html just inside the code with echo. Obviously, I can't create anything that have more that 3 pages.

So I looked over the internet, and on frameworks (like Symfony), and it was great. I began to create a simple OOP website, with MVC(-like) architecture, to separate the View, Controller and Model.

After that, I decided to create a system of plugins, to be able to create something for one website and use it for another. I decided that the framework should only be using plugins. It means that, appart from the core functions of the framework, everything is plugin.

Let's take an example. In one website, I can create a simple users_plugin, that handle the login/logout of users. That plugin will handle some links/url (like mywebsite.com/users/login and mywebsite.com/users/logout. To know if an user is connected, the others plugins can just use some of the exported functions of users_plugin, and any plugin loaded before users_plugin can handle the links of users_plugin, to be able to add more functionnalities.

How it works?

Routes

A route is a link or url, composed by:

  • the name of the controller (for example users)
  • the name of the action (for example login)

An user can access a public route like that: www.mywebsite.com/users/login. The controller called will be the one associated with user, and the action will be login.

Each plugin must tell (to the framework) wich route it handles. The routes must be written in the file routes.rt (in the root of the plugin folder). An example of this file is:

users/login
users/logout

Controller

Quick overview

When an user of the website ask for a page, he uses a route. The corresponding controller will be called, to execute the action requested. If an user ask for www.mywebsite.com/users/login, the framework will look for the first plugin that handles this route.

A controller must be a PHP class, and should contain functions for each action. Here is an example:

class UsersController extends Controller {
	protected function action_login() {
		// ...
	}
	
	protected function action_logout() {
		// ...
	}
}

This file must be named UsersController.php and must be located users_plugins/controller/UsersController.php, where users_plugins is the folder of the plugin and is in the plugins folder.

The functions must also be named in the same way than the example: action_ and the name of the action. The object must be called like the file (Users - the name of the controller; Controller), and must extend a class named Controller. That class is defined in the core folder, and contains some usefull (and necessary) functions.

Actions

There is a lot of things that can be done in an action. Functions are here to allow that.

  • $this->data: this is an array that must contains every things you want to put in your webpage. The elements of this array will be accessibles in the View.
  • Plugins::callFunction('users_plugin', 'connect', $pseudo, $password);: you can call a function from an other plugin. The function must be in external/Export.php.
  • $this->getModel(): get the model associated with the current controller.
  • Plugins::getModel('users_plugin, 'users');: get the model associated with the given controller.
  • $this->setTitle($title): set the title of the page (see the layout plugin)
  • $this->setDescription($desc): set the description of the page (see the layout plugin)

With similar tags:

#4 - 0x0
Smiley32