Click here to Skip to main content
15,881,967 members
Articles / Programming Languages / PHP

PHP Micro-Framework HLEB

Rate me:
Please Sign up or sign in to vote.
3.77/5 (20 votes)
6 Jan 2022MIT3 min read 34K   18   12   3
This article gives an overview of installation, customization, routing, group of routes, controllers, modules, templates, page builder, debug panel and console of a micro-framework HLEB.

HLEB LOGO

Micro-Framework HLEB

Requires PHP version 7.0 or higher (including version 8).

Link to instructions (RU)

Routing > Controllers > Models > Page Builder

A distinctive feature of the micro-framework HLEB is the minimalism of the code and the speed of work. The choice of this framework allows you to launch a full-fledged product with minimal time costs and appeals to documentation; it is easy, simple and fast. At the same time, it solves typical tasks, such as routing, shifting actions to controllers, model support, so, the basic MVC implementation. This is the very minimum you need to quickly launch an application.

Installation

To start the mini-framework HLEB:

  1. Download the folder with the project from its original location (phphleb/hleb).

    Using Composer:

    Bash
    $ composer create-project phphleb/hleb
  2. Assign the address of the resource to the "public" subdirectory.
  3. Establish the rights to allow changes for web server for the "storage" folder and all folders and files within it.

Upon completion of these steps, you can verify installation by typing the resource address assigned earlier (locally or on a remote server) in the address bar of the browser. If installation is successful, a parked page with the framework logo will be displayed.

Customization

Command character constants in the micro-framework HLEB are set in the start.hleb.php file. Initially, a file with this name does not exist and must be copied from the default.start.hleb.php file in the same project root directory.

Attention! Constant HLEB_PROJECT_DEBUG enables / disables debug mode. Do not use debug mode on a public server.

Routing

Project routes are compiled by the developer in the "/routes/main.php" file, other files with routes from the "routes" folder can be inserted (included) into this file, which together constitute a routing map.

Routes are determined by class Route methods, the main of which is get(). All methods of this class are available and used only in the routing map.

Attention! Route files are cached and should not contain any code containing external data.

PHP
Route::get('/', 'Hello, world!');

Display the contents of the "/views/index.php" file using the view() function (also available in controllers).

PHP
Route::get('/', view('index'));

This is an example of a more complex-named route. Here, $x and $y values are transferred to the "/views/map/new.php" file, and conditions for the dynamic address are set ("version" and "page" can take different values). You can call a route up by its name using dedicated functions of the framework.

PHP
Route::get('/ru/{version}/{page?}/', view('/map/new', 
['x' => 59.9, 'y' => 30.3]))->where(['version' => '[a-z0-9]+', 
'page' => '[a-z]+'])->name('RouteName'); // /ru/.../.../ or /ru/.../

Groups of Routes

Methods located before a route or group:

type()->, prefix()->, protect()->, before()->

PHP
Route::prefix('/lang/')->before('AuthClassBefore')->getGroup();
  Route::get('/page/', "<h1>Page</h1>"); // /lang/page/
  Route::protect()->type('post')->get('/ajax/', '{"connect":1}'); // /lang/ajax/
Route::endGroup();

Methods located after a route or group:

->where(), ->after()

PHP
Route::type(['get','post'])->before('ClassBefore')->get
('/path/')->controller('ClassController')->after('ClassAfter');

Controllers

Creating a simple controller with such content:

PHP
// File /app/Controllers/TestController.php
namespace App\Controllers;
use App\Models\UserModel;
use Hleb\Constructor\Handlers\Request;
class TestController extends \MainController
{
    function index($value) // $value = 'friends'
    {
     $data = UserModel::getData(['id' => Request::get('id'), 'join' => $value]);
     return view('/user/profile', ['data' => $data]);
    }
}

You can use it in the route map:

PHP
Route::get('/profile/{id}/')->controller
('TestController',['friends'])->where(['id' => '[0-9]+']);

or:

PHP
Route::get('/profile/{id}/')->controller
('TestController@index',['friends'])->where(['id' => '[0-9]+']);

Replacing class and method calls from url:

PHP
Route::get('/example/{class}/{method}/')->controller('<class>Controller@get<method>'); //Converts `site.com/example/all-users/user/` to `AllUsersController@getUser`

Modules

For modular development, you need to create the folder 'modules'.

  • /modules/example
    • /DefaultModuleController.php (or 'Controller.php' without specifying the controller in the route)
    • /content.php
    • /templates/origin.php
PHP
Route::get('/test/module/example/')->module('example', 'DefaultModuleController');
PHP
// File /modules/example/DefaultModuleController.php (similar to standard controller)
namespace Modules\Example;
class DefaultModuleController extends \MainController
{
   function index()
   {
      return view('content');
   }
}
PHP
// File /modules/example/content.php
insertTemplate('/example/templates/origin');

Models

PHP
// File /app/Models/UserModel.php
namespace App\Models;
class UserModel extends \MainModel
{
  static function getData($params)
  {
    $data = /* ... */ // A query to the database, returning an array of user data.
    return $data;
  }
}

ORM

Mutexes

Bash
$ composer require phphleb/conductor

The use of mutexes is worthwhile in cases, when access to any code is to be locked, until it is executed in the current process or the set locking time period expires.

More details

Dependency Injection implementation

Bash
$ composer require phphleb/draft

$ php console phphleb/draft --add

This way is different from the traditional DI (Dependency injection), since it does not injects dependencies programmatically at runtime, but in advance, by generating and changing classes according to settings. The created classes exist as files; their correctness can be checked, dependencies are "visible" for IDE, so as testing is possible.

More details

User registration

Bash
$ composer require phphleb/hlogin
Bash
$ php console phphleb/hlogin --add

These two steps install the module for registration. More details

Templates

PHP
// File /resources/views/content.php
insertTemplate('templates/origin', ['title' => 'Short text', 'content' => 'Long text']);
PHP
// File /resources/views/templates/origin.php
echo $title; // Short text
echo $content; // Long text

Page Builder

PHP
Route::renderMap('#Header_map', ['/parts/header', '/parts/resources']);
Route::renderMap('#Footer_map', ['/parts/reviews', '/parts/footer']);

Route::get('/', render(['#Header_map', '/pages/index', '#Footer_map'], ['variable' => 'value']));

Optional use of Twig template engine

Bash
$ composer require "twig/twig:^3.0"
PHP
Route::get('/template/', view('templates/index.twig', ['variable' => 'value']));

Debug Panel

PHP
WorkDebug::add($debug_data, 'description');

Console

List of standard console commands (run from the project folder):

Bash
$ php console  --help
An example of the task being performed.
Bash
$ php console  default-task 

History

  • 7th January, 2022: Updating the Page Builder
  • 24th December, 2021: The includeTemplate method has been replaced with insertTemplate
  • 4th December, 2021: Added mutexes
  • 3rd November, 2021: DI (Dependency injection) implementation
  • 3rd October, 2021: User registration module
  • 3rd February, 2021: Tag substitution in controllers
  • 13th December, 2020: PHP 8 support
  • 27th May, 2020: Added support for "Twig"
  • 20th March, 2020: Added modular development
  • 27th December, 2019: Initial version

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Web Developer
Russian Federation Russian Federation
Web Developer

Comments and Discussions

 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA7-Jan-22 3:52
professionalȘtefan-Mihai MOGA7-Jan-22 3:52 
GeneralMessage Closed Pin
3-Nov-21 3:03
Gámér-Giri Moderator3-Nov-21 3:03 
QuestionWhat does HLEB stand for? Pin
Member 145304804-Oct-21 10:43
Member 145304804-Oct-21 10:43 
AnswerRe: What does HLEB stand for? Pin
Foma Tuturov4-Oct-21 21:48
Foma Tuturov4-Oct-21 21:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.