Create type of module - lucbu/AngkorCMS GitHub Wiki
Create a new type of module
For each module, you’ll have to add a line in the file ‘config\angkorcmsmodules’ in the array ‘nature’ you’ll have to add an item with as the key, the name you want to give to the module, and as value, the name of the config files associated to the modules.
(For example the module 'map' is included like “ ‘map’=>’angkorcmsmap’, “)
Create the folder
To create each plugins I’m using the package called franzliedke/studio (GitHub).
I create plugins using the command:
vendor\bin\studio create foo/bar
Two kinds of modules
Unique:
A unique module is a module that can’t be adapted to a new item, like the login plugin or the changelang plugin.
There is a need of a ViewComposer to render the view which has to be shown.
The installation of the plugin will be a seed of his information in the table angkorcms_modules and the publishing of his Blade/JS/CSS file, here is an example:
DB::table('angkorcms_modules')->insert(array(
'name' => 'Change Lang',
'title' => '',
'unique' => true,
'nature' => 'changelang',
'lang_id' => null,
));
Customizable:
A customizable module, is a module that depend on an item like a slideshow which will contains slides, or a map which will contains location or others information.
There is a need of a ViewComposer to render the view (named maker) which allow the user to edit the module. The view used to show the module, will be loaded with the principal object defined in the config file with the name that you give to the module in ‘angkorcmsmodules.php’. For example for the module map, the object AngkorCMSMap associated to each module of nature map, will be accessible in the show view under the variable $map. But you still can set a view composer to add data.
Adapt the Default-Plugin model
There is a folder in the root of the CMS called Plugin-Default which contain all files needed to create a plugin. All files should be modified.
Customizable:
- -Object- : Name of the main object
- -Plugin- : Name of the plugin (ex. AngkorCMSSlideshow)
- -pluginmin- : Name of the plugin with little letters (ex. angkorcmsslideshows)
- -namespace- : Namespace (ex. AngkorCMS\Slideshow)
- -folderName- : Name of the plugin's folder (ex. slideshow)
- -vendorName- : Name of the vendor (ex. angkorcms)
- -showView- : Name of the view to be shown
Unique:
- -Plugin- : Name of the plugin (ex. AngkorCMSSlideshow)
- -pluginmin- : Name of the plugin with little letters (ex. angkorcmsslideshows)
- -namespace- : Namespace (ex. AngkorCMS\Slideshow)
- -folderName- : Name of the plugin's folder (ex. slideshow)
- -vendorName- : Name of the vendor (ex. angkorcms)
- -showView- : Name of the view to be shown
You’ll also have to customize the repository which will have to implement the interface in the contract folder.
You also can add translations to modules the way, you’ll do it in any Laravel application.
For coding the view, you’ll access to some data:
-
$unique_id: An unique id that can be used to define html tag(s).
-
$module: The module with his 'name':string, 'title':string, 'nature':string, 'unique':boolean, 'lang':AngkorCMSLang
-
$'module-name' (only if customizable plugin): The module define by the package that declare it. (for example slideshows)
-
$parameters: Array containing "url_base" that is the url created with the slug of the AngkorCMSPageTrans that the visitor is currently on
-
$attributes: Attributes used to create div around each modules (The modules are already created but you can still have access to it)
-
$attr: attributes to pass to modules, Attributes that you can use in your modules to allow user to customize plugins (for example letting them choose the CSS class of some HTML elements)
But you also can add data by creating a ViewComposer getting launch when your view is called.
ServiceProvider
A service provider is used to declare assets to be published, ViewComposers, Commands, Routes...
<?php namespace -namespace-;
use -namespace-\Commands\-Plugin-InstallCommand;
use Illuminate\Support\ServiceProvider;
use View;
class -Plugin-ServiceProvider extends ServiceProvider {
public function boot() {
//Allow to use View like view('-folderName-::-view-');
$this->loadViewsFrom(__DIR__ . '/resources/views', '-folderName-');
//Copy all the file from the path defined by the key to the path define by the value
$this->publishes([
__DIR__ . '/config/-pluginmin-.php' => config_path('-pluginmin-.php'),
__DIR__ . '/resources/views' => base_path('resources/views/-vendorName-/-folderName-'),
__DIR__ . '/js' => base_path('public/js/-pluginmin-'),
]);
//Include the routes files
include __DIR__ . '/Http/routes.php';
//Set a composer on the view, it will be loaded everytime the view is called
View::composer('-vendorName-/-folderName-/maker', '-namespace-\Http\ViewComposers\MakerComposer');
}
public function register() {
$this->registerCommands();
//Register the controller
$this->app->make('-namespace-\Http\Controllers\-Object-Controller');
}
protected function registerCommands() {
$this->registerMigrateCommand();
}
protected function registerMigrateCommand() {
$this->commands('command.-pluginmin-.install');
$this->app->singleton('command.-pluginmin-.install', function ($app) {
return new -Plugin-InstallCommand($app);
});
}
public function provides() {
return [
'command.-pluginmin-.install',
];
}
}
ViewComposer
A ViewComposer is a class that will fill a view with data whenever the view is called.
And the view composer:
use AngkorCMS\MultiLanguages\Repositories\Eloquent\AngkorCMSLangRepository;
use Illuminate\Contracts\View\View;
class ShowComposer {
protected $lang_repository;
public function __construct(AngkorCMSLangRepository $lang_repository) {
$this->lang_repository = $lang_repository;
}
public function compose(View $view) {
//Get the data from the view
$viewParameters = $view->getData();
//Set languages to the $data
$parameters = $viewParameters['parameters'];
$data = array('langs' => $this->lang_repository->all());
// Agregate and Send the data to the view
$data = array_merge($data, $viewParameters);
$view->with($data);
}
}