How to implement Plugins in an MVC Framework

1

I'm creating my own Framework MVC , mainly for studies, and would like to implement plugins on it, but I have no idea how to do it.

I would like to know some points, such as:

Control of plugins installed?

How to control which plugins are installed in the Framework? Should I just get the files from the plugins directory and exit running?

Plugin

I want to know what the scope of a plugin looks like. If it would be a class that I should instantiate on some Controller property , or should not I run anything, and the developer using the framework should bother about it?

What to encapsulate and what to allow the plugin?

I need to worry about the plugin with which the plugin has access. For example, the connection to the database would be a default attribute that the plugin would have access to. But should I worry about anything else, like models and etc?

Is there any standard for plugins in MVC?

    
asked by anonymous 30.05.2016 / 20:22

1 answer

3

Dear KaduAmara,

Match the Laravel , by composer taking on your project a folder vendor . All packages are installed (copied) to this folder by adding the require key of your composer.json and after the composer update command.

Basic example of the file composer.json :

{
    "name": "stackoverflow/packages",
    "description": "Packages",
    "authors": [
        {
            "name": "stackoverflow",
            "email": "[email protected]"
        }
    ],
    "minimum-stability": "dev",
    "require": {}
}

Within this folder you have a autoload.php , refer to include / require in your MVC Framework project. a>, and the packages will work for your project.

Tip: install Laravel on your computer and package repository (Packagist - The PHP Package Repository ), install some packages and observe the procedure.

Editing

1) Control of installed plugins?

With the composer manager and its composer.json configuration file you have control of what has been installed in your application, including versioning and improvements in the package (plugin) installed.

Example: to install the package fuel / email add in your configuration file composer.json in the key require fuel / email of version 1.8 where your main system Framework MVC use when needed. Note the configuration:

{
    "name": "stackoverflow/packages",
    "description": "Packages",
    "authors": [
        {
            "name": "stackoverflow",
            "email": "[email protected]"
        }
    ],
    "minimum-stability": "dev",
    "require": {
           "fuel/email":"1.8.*"
    }
}

After this setting, type in the command prompt (console) php.exe composer.phar update this command installs the package in your application into a folder named vendor (default).

Note: To uninstall the package, just remove the require key from the package name and run it from the% / p>

2) How, where and when to run the plugin script?

From the configuration and installation of the item 1) the plugin is available on your system. The execution and control is done by the developer who will use the best possible way, that is, who determines what moment he will use is the developer himself who is using his Framework MVC .

Example: I want to use the package installed on item 1) , then:

$mail = Email::forge();
$mail->from('[email protected]', 'Your Name Here');
$mail->to('[email protected]');
$mail->to('[email protected]', 'His/Her Name');
$email->body('My email body');
$email->html_body(\View::forge('email/template', $email_data));
$email->alt_body('This is my alt body, for non-html viewers.');
$email->subject('This is the subject');
$email->priority(\Email::P_HIGH);
$result = $email->send();

The package author places the github package in the link .

3) What to encapsulate and what to allow the plugin?

The installed package has access to your system through the intervention of the programmer, this is not a reason to access, but to use, take something that is already ready and add as functionality in your system. In the 2) item it is clear that the email package installed for sending messages is only triggered when the developer instantiates their php.exe composer.phar update .

4) Is there any standard for plugins in MVC?

In the example given by the Packagist , there is a pattern where your installations are done by configuring a file in json ( classe ) and when composer.json has the responsibility to manage. In the website itself you have the explanation of how to create packages so that in your composer , the package is registered and your application has access.

Reading:

Dependency Manager for PHP

Book Composer

laravel.com

    
30.05.2016 / 21:46