Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 Project Structure (Plugin System)

I'm trying to develop my first Laravel (5) application that can be extended by plugins. I've been reading a lot about plugin architectures, but what I'm struggling with is the best or preferred way to organise such a project in Laravel. I'd be very grateful for any help and advice here as I don't want to set off on the incorrect path.

Here's how I'm thinking about structuring and implementing this:

/ Plugins
    - PluginManager.php

    / Contracts
        - PluginInterface.php

    / Plugins

        / ExamplePlugin1
            - ExamplePlugin1.php

        / ExamplePlugin2
            - ExamplePlugin2.php

Question 1: Where would be the best place for the root /Plugins directory to be located? Directly in the app/ root folder or somewhere like app/Http?

On app startup, I want the PluginManager class to scan the Plugins/Plugins/ sub directory as that's where all installed plugins will reside. At that point, the PluginManager will create a reflection instance of those plugin classes and store them in an array so that it can loop through them later and call methods on them if they exist.

Question 2: As I want the PluginMananger to be available for all requests, should I be using a Service Provider and Facade for this?

Question 3: Is this method efficient or could anyone offer an alternative solution?

All those plugins will implement the PluginInterface interface so that the PluginManager class can call, for example, an init() function on all plugins.

Thanks for your time

like image 566
dpstudio Avatar asked Feb 10 '15 12:02

dpstudio


Video Answer


1 Answers

Question 1

The simplest way to do this, is to create a directory "app/Plugins" ( "app/Http/Plugins" only if your plugins are specific to Laravel core application or routing @RTM: "Think of the Console and Http directories as providing an API into the "core" of your application." ).

Question 2

YES! @see: performance issue using deferred providers

Question 3

May be can you make a kind of plugins.lock in your application root to avoid scanning "app/Plugins" on every requests ?

like image 185
ShuifuraX Avatar answered Oct 19 '22 23:10

ShuifuraX