PHP Classes

Laravel Template Engine Modules: Implement template modules using custom classes

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 38 All time: 10,933 This week: 455Up
Version License PHP version Categories
laravel-modules 1.0.3Custom (specified...5PHP 5, Libraries, Templates
Description 

Author

This package can implement template modules using custom classes.

It can register a new class to implement a module than can handle a new type of tags that can be used in templates.

The module class implemented by applications can return details like:

- Position of labels next to the template mark
- Priority of the module in the list of modules
- Permission to render in a template
- Caching strategy
- Template rendering results

Innovation Award
PHP Programming Innovation award nominee
September 2020
Number 2
Some applications may need to implement complex presentation screens that show different types of information in a very specific way.

Modules can be useful to reduce the complexity, so each module implements a different part of the presentation of Web application screens.

This package provides means to implement modules for templates that can be used to render screens of Laravel Web applications.

Manuel Lemos
Picture of Ivan Semenkov
  Performance   Level  
Name: Ivan Semenkov <contact>
Classes: 1 package by
Country: Ukraine Ukraine
Age: 35
All time rank: 450978 in Ukraine Ukraine
Week rank: 180 Up4 in Ukraine Ukraine Up
Innovation award
Innovation award
Nominee: 1x

Documentation

laravel-modules

A simple package for working with template modules.

Table of contents

What is?

Solution

laravel-modules is a package designed to separate the logic of an individual component from the general logic of the application. It allows to create and manage separate logical blocks (modules). Each module encapsulates methods for managing render logic, access control, caching, etc.

Module structure

Each module is standard php class with several optional methods.

namespace App\Modules\TestModule;

class TestModule {
    
   /
     * Return current module template position label string.
     * If function isn't exists as position label uses lowercase module class 
     *   name.
     * If return callback function then ModulesManager call it to resolve 
     *   module position.
     * 
     * @param void
     * @return string|callable Position label | Callback function.
     */
    public function position() {
        return "module.position";
    }

   /
     * Return current module sort priority value.
     * This value is used to sort multiples modules registered in one position.
     * If function isn't exists priority sets as zero.
     * If return callback function then ModulesManager call it to resolve 
     *   module priority weight.
     * 
     * @param void
     * @return integer|callable Sort module priority weight | Callback function.
     */ 
    public function priority() {
        return -1;
    }
    
   /
     * Return current module needs permissions.
     * If return bool value then on true module is rendered, on false none.
     * If return value type is string it is a permission access to render. 
     * If return callback function then ModulesManager call it to resolve 
     *   permissions.
     * If function isn't exists module render always.
     *  
     * @param void
     * @return string|callable Module permissions string | Callback function
     */ 
    public function permission() {
        return "module.permission";
    }
    
   /
     * Current module caching strategy. 
     * If return bool value then on true module cached always. As a cache key 
     *   uses lowercase module name. On false module newer cached.
     * If return value type is string it is uses as cache key.
     * If return callback function then ModulesManager call it to resolve
     *   caching strategy.
     * 
     * @return bool|string|callable Module cached stategy.
     */ 
    public function cache() {
        return "module.cache_key";
    }
    
   /
     * Current module cache time.
     * If return bool value then on true module cached forever. On false module 
     *   newer cached.
     * Return value for cache timeout in seconds.
     * If return callback function then ModulesManager call it to resolve
     *   cache time.
     * 
     * @param void
     * @return bool|integer|callable 
     */
    public function cacheTime() {
        return 3600; 
    }

   /
     * Render current module.
     * If function exists it is uses to render current module.
     * 
     * @param mixing Template render arguments.
     * @return string|serializable Rendered view.
     */ 
    public function render($args = null) {
        return View::make("module.view");
    }
}

Register module

Each module must be registered before use. You can use several ways for this.

  • Set module class object.
public function index(ModulesManager $manager) {
    $manager->registerModule([
        new TestModule($construct_args),
        /Register as many modules as you need/
    ]);

    return View::make('frontend');
}

  • Set full module string class name
public function index(ModulesManager $manager) {
    $manager->registerModule([
        TestModule::class,
    ]);

    return View::make('frontend');
}

To pass arguments to a constructor, pass an array, the first element of which will be the class name, and the second element will be arguments to the constructor.

public function index(ModulesManager $manager) {
    $manager->registerModule([
        [TestModule::class, $construct_args],
    ]);

    return View::make('frontend');
}

If you need to pass several parameters to the constructor, wrap them in an array.

public function index(ModulesManager $manager) {
    $manager->registerModule([
        [TestModule::class, [$construct_arg1, $consruct_arg2]],
    ]);

    return View::make('frontend');
}

Add the third element of the array to pass arguments to the render method.

public function index(ModulesManager $manager) {
    $manager->registerModule([
        [TestModule::class, $consruct_args, $render_args],
    ]);

    return View::make('frontend');
}

Requirements

  • Laravel 5+
  • PHP 5.3.7+

Installation

Require this package with composer.

composer require isemenkov/laravel-modules

Usage

namespace App\Http\Controllers;

use Illuminate\Support\Facades\View;
use isemenkov\Modules\ModulesManager;
use App\Modules\TestModule;

class TestController extends Controller {

    public function index(ModulesManager $manager) {
        $manager->registerModule([
            TestModule::class,
        ]);

        return View::make('frontend');
    }
}

<!-- frontend.blade.php -->
<!DOCTYPE html>
<html>
  <body>
    <header>
      @module(module.position)    
    </header>
  <body>
</html>

  Files folder image Files (8)  
File Role Description
Files folder imagesrc (3 files, 2 directories)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (8)  /  src  
File Role Description
Files folder imageconfig (1 file)
Files folder imageFacades (1 file)
  Plain text file Module.php Class Class source
  Plain text file ModulesManager.php Class Class source
  Plain text file ModulesServiceProvider.php Class Class source

  Files folder image Files (8)  /  src  /  config  
File Role Description
  Accessible without login Plain text file modules.php Aux. Auxiliary script

  Files folder image Files (8)  /  src  /  Facades  
File Role Description
  Plain text file Modules.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:38
This week:0
All time:10,933
This week:455Up