How to Implement Plugins in an MVC Framework

Asked

Viewed 165 times

1

I’m creating my own Framework MVC, mainly for studies, and would like to implement plugins 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 grab the files from the plugins and get out running?

How, where and when to run the script of plugin?

I want to know how is the scope of a plugin. If it would be a class I should instantiate in some property of the Controller, or if I should not run anything, and the developer who is using the framework you should worry about that?

What to encapsulate and what to allow the plugin?

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

Is there any default for plugins in MVC?

1 answer

3


Caro Kaduamara,

Implement equal to the Laravel, for commiserate having in its project a briefcase vendor. All packages are installed (copied) to that folder by adding to the key require of your composer.json and after the command of composer update.

Basic example of the file composer.json:

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

Inside that briefcase is a autoload.php, make the reference with include/require in your project Framework MVC, and will run the packages for your project.

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

Editing

1) Control of installed plugins?

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

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

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

After this setting type it in the command prompt (console) php.exe composer.phar update this command installs the package in your application in a folder called vendor (pattern).

Observing: to uninstall the package just remove from the key require the package name and run at command prompt (console) php.exe composer.phar update again.

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

From the item setup and installation 1) the plugin is available on your system. The execution and control is done by the developer who will use it in the best way possible, ie, who determines which moment will use is the developer who is using his own Framework MVC.

Example: I want to use the package installed in the 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 author of the package places in your github how the package is used, the explanation is 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 item 2) it is clear that the email package installed for sending messages is only triggered when the developer instills its classe.

4) Is there any default for plugins in MVC?

Exists, in the example given by Packagist, follows a pattern where your installations are made from the configuration of a file in the format json (composer.json) and when the composer has the responsibility to manage. In the site itself has the explanation of how to create packages so that in your autoload.php, the package is registered and your application has access.

Reading:

Dependency Manager for PHP

Book Composer

Laravel.

  • John thanks for the answer, but she is not answering another(s) question(s)), O que encapsular e o que permitir ao plugin? and Como, onde e quando executar o script do plugin?. I do not know if I was very clear... If you want I can improve the question.

  • Kaduamaral, I gave you a package manager (which can be an extension of your application). The plugin or package or helper are made with a certain end before an application core. This is how many PHP framework works and in it you develop an infinity of subsystems within the larger system. See if you understand me the plugin you install in your application is like this: are code directed to a certain purpose that assists the main in the tasks. Example: a plugin for sending email, a login form and user password, everything can be done with this manager

  • I will edit my reply and try to direct to the maximum so that you understand what it means to manage application packages.

  • Thanks João, I am waiting. If you can make a simple example, as the sending of emails.

  • 1

    One thing I noticed in your case, you are confused access with package/plugin. Package/Plugin are mini systems that are aggregated in the larger system than in your case is Framework MVC. The default is made by you, but this manager (Composer) is the current one used in several MVC frameworks with Laravel, Lumen, Slim, etc. Enter this http://www.slimframework.com/ look how it is installed. is up to the larger systems are already managed by Composer.

  • Anything you can ask!

  • 1

    You answered my questions. Thank you.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.