megaads / clara
Module management in Laravel
Requires
- php: >=5.6.4
- illuminate/support: >=5.3
- jeremeamia/superclosure: ^2.4
- laravel/framework: 5.4.*
- megaads/composer-merge-plugin: *
- dev-master
- 2.0.0
- 1.1.49
- 1.1.48
- 1.1.47
- 1.1.46
- 1.1.45
- 1.1.44
- 1.1.43
- 1.1.42
- 1.1.41
- 1.1.40
- 1.1.39
- 1.1.38
- 1.1.37
- 1.1.36
- 1.1.35
- 1.1.34
- 1.1.33
- 1.1.32
- 1.1.31
- 1.1.30
- 1.1.29
- 1.1.28
- 1.1.27
- 1.1.26
- 1.1.25
- 1.1.24
- 1.1.23
- 1.1.22
- 1.1.21
- 1.1.20
- 1.1.19
- 1.1.18
- 1.1.17
- 1.1.16
- 1.1.15
- 1.1.14
- 1.1.13
- 1.1.12
- 1.1.11
- 1.1.10
- 1.1.9
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.0
- dev-feature-clara-nested-view
- dev-release-clara-v1
- dev-feature-clara-reverse-to-before-nestedview
- dev-l52
- dev-l54
- dev-old-laravel-version
This package is auto-updated.
Last update: 2024-11-19 04:42:17 UTC
README
A module management in Laravel
System requirements
- PHP: >=5.6
- Laravel Framework: >=5.4
Installation
Clara is packed as a composer package. So it can be installed quickly:
-
Require the composer package
composer require megaads/clara
-
Register the provider:
Megaads\Clara\Providers\ModuleServiceProvider
-
Register the facade:
Megaads\Clara\Facades\ModuleFacade
-
Autoloading
By default, Module classes are not loaded automatically. You can autoload your modules in composer.json
{ "autoload": { "psr-4": { "App\\": "app/", "Modules\\": "app/Modules" } }, "extra": { "merge-plugin": { "include": [ "app/Modules/*/module.json" ] } } }
- Publish Clara configurations
To override Clara default configuration options, you will need to publish Clara configuration file to the application's config directory.
php artisan vendor:publish --provider="Megaads\Clara\Providers\ModuleServiceProvider"
Module Management
Create module
php artisan module:make <ModuleName> ...
Naming conventions
Module names should follow UpperCamelCase
syntax (without any space characters). For example: FacebookComment
and module namespace in kebab-case
that correspond to a module name in CamelCase
. For example: facebook-comment
Folder structure
app
│
└───Modules
└───ModuleName
│
└───Config
│ app.php
│
└───Controllers
│ Controller.php
│ ...
│
└───Helpers
│ helper.php
│ ...
│
└───Middlewares
│ ExampleMiddleware.php
│ ...
│
└───Models
│ ...
│
└───Resources
│ Views
│ Assets
│ ...
│
└───Routes
│ routes.php
│ ...
│
└───Kernel.php
│
└───module.json
│
└───start.php
module.json
: the module configuration file is based oncomposer.json
form. All properties in themodule.json
will be merged tocomposer.json
of the project.start.php
: the module's start file that will be loaded every requests. So module actions, module views... can be registered in this file.
Module installing
Install a module using the module name
php artisan module:install <ModuleName>
Clara takes the name of the module that requested to search for it in the app repository that you have registered in config/clara.php
configuration, after downloading the module files, Clara adds the module configuration to the module.json
file
Install module list from module.json
php artisan module:install
This will searches and downloads the modules listed in module.json
from the app repository into the app/Modules/
directory
Install a module from a zip file or an URL
php artisan module:install <ModuleZipFilePath> <ModuleZipFileURL> ...
Enable a module
php artisan module:enable <ModuleName> ...
Disable a module
php artisan module:disable <ModuleName> ...
Remove a module
php artisan module:remove <ModuleName> ...
Remove all modules
php artisan module:remove-all
Module Action
Fire a action
Using PHP
Module::action('action_name', [params]);
Using blade statement
@action('action_name', [params])
Handle a action
Module::onAction('action_name', function ($params) { }, PRIORITY);
Handle a action using a controller
Module::onAction('action_name', 'Modules\Example\Controllers\HomeController@action', PRIORITY);
By default, Clara supplies actions:
- module_made
- module_loaded
- module_disabled
- module_enabled
- module_removed
- module_removed_all
Module View
Register a view
Using PHP
Module::view('view_name', [params], IS_MULTI_LAYER);
Module::view('view_name', 'This is a view placeholder', IS_MULTI_LAYER);
Module::view('view_name', function() { return 'This is a view placeholder'; }, IS_MULTI_LAYER);
Using blade statement
@view('view_name', [params])
Handle a view
Module::onView('view_name', function ($params) { return view('{module-namespace}:home.index'); }, PRIORITY);
Handle a view using a controller
Module::onView('view_name', 'Modules\ExampleModule\Controllers\HomeController@index', PRIORITY);
Module variable
Register a variable
Using PHP
$variable = Module::variable('handle', $default, PRIORITY);
Using blade statement
@variable('variable_name', 'handle', $default);
Handle a variable
Module::onVariable('hanlde', function ($params) { }, PRIORITY, NUUM_OF_PARAM);
Module Assets
Clara will create a symbol link from module asset directory app/Modules/{ModuleName}/Resources/Assets
to public/modules/{module-namespace}
while a module was installed, made or enabled.
Include a module asset
Using PHP
<script type="text/javascript" src="<?= Module::asset('{module-namespace}/js/demo.js') ?>"></script>
Using blade statement
<script type="text/javascript" src="@asset('{module-namespace}/js/demo.js')"></script>
Create module asset link manually
php artisan module:asset:link <ModuleName>
Create module asset link manually for all activated modules
php artisan module:asset:link --all
Module Configurations
All of the configuration files for the module are stored in the {ModuleName}/Config
directory
Accessing Configuration Values
Sometimes you may need to access configuration values at run-time. You may do so using the Config
class
Config::get('{module-namespace}::app.message', 'hello world');
Module Utility Methods
Get all modules
$modules = Module::all();
Get the current module
$module = Module::this();
Get module options
$option = Module::option('option.name');
Set module option
$option = Module::option('option.name', 'option.value');
License
The Clara is open-sourced software licensed under the MIT license
Contact us / Instant feedback
Email: info@megaads.vn | phult.contact@gmail.com
If you find a bug, please report it here on Github