📜 ⬆️ ⬇️

We write your php framework for the evening

Periodically, there is a task to write some small functional, to use for this a large framework like Zend / Yii / CI / Kohana, etc., either you don’t feel like it or you don’t have the opportunity.

Some time ago, I had the task to write a small web application, it was not possible to install the above framework. I decided to write my own, which works according to the MVC principle, 1 evening went to the base, twisting and optimization went on the course of development.

It can seem to the beginning web developers that it is difficult, I will try to make this article understand that it is not.

')
To begin with, we will define the requirements:


To implement the CNC, we will use mod_rewrite, the url will look like this: mysite.com/controller_name/ controller_action / param_name_1 / param_value_1 / param_name_2 / param_value_2

We need to create the base classes of the model, view, controller.
abstract class CController extends CBaseObject ... abstract class CModel extends CBaseObject ... class CView ... class CViewSmarty extends Smarty ... 

All requests will be processed by index.php, the settings will be stored in config.php as a regular array.
part of config.php
  $config['SYSPATH'] = "system"; $config['APPPATH'] = "application"; $config['BASEPATH'] = "/projects/savage"; 


It will also require autoloading classes.
  function autoload_classes($param) { include("config.php"); if (strtolower(substr($param, 0, 1)) == "m") { include_once ($config['APPPATH'] . "/models/" . strtolower($param) . ".php"); } if (strtolower(substr($param, 0, 1)) == "c") { $part_path = strtolower($param) . ".php"; if (file_exists($config['SYSPATH'] . "/classes/" . $part_path)) { $file = $config['SYSPATH'] . "/classes/" . $part_path; } else { $file = $config['APPPATH'] . "/controllers/" . $part_path; } include_once $file; } } spl_autoload_register('autoload_classes'); 


index.php will be responsible for calling the controller's methods, while checking whether the first parameter is the controller identifier or the default controller method (so that you can write like this: mysite.com/mycontroller/myaction and so mysite.com/myaction)

Let us turn to the implementation of the controller, its task is the implementation of logic, deciding which representation to use.
For the methods used in url, we will prefix: action_ (familiar to ci / kohana users).
  class CMain extends CController { public function action_index() { $widgets = new CWidgets(); $prm_data = array( 'widget_login' => $widgets->widget_login(), ); $view = new CView(); $view->assign($prm_data); $view->display("index"); } ... 


We also need to work with data, for this we create a model, and the model may not necessarily work with the database, sometimes it is enough to store an array. In my example, the usual array is used, if necessary, you can work with the database (via PDO).
  class MUsers extends CModel { ... public function get_list() { for ($i = 1; $i < 6; $i++) { $this->data[] = array('id' => $i, 'login' => 'user ' . $i, 'pw' => ($i * 2 + $i)); } return $this->data; } } //!        PDO //!      IP public function check_login($params) { $prm_data = array(':id' => $params['suid'], ':ip' => $_SERVER['REMOTE_ADDR']); $data = self::fetch_all("select id from ##_log where id = :id and ip = :ip", $prm_data); if (count($data) > 0) { return TRUE; } else { return FALSE; } } 


Representation, there are many ways to implement the display, someone prefers to use native php template maker, someone likes Smarty or something.
I liked Smarty, so I did the basic presentation methods like in Smatry.

That's all, the framework is ready, but it is not as powerful as brands, but for a number of tasks it is optimal.

Watch a demo here , download without Smarty , download from Smarty .

Source: https://habr.com/ru/post/141714/


All Articles