user4540007
user4540007

Reputation:

Best practice to modular programming in Laravel 5+

I'm starting a new project and I want to reuse some parts of it, mainly the stuff related to user registration and authentication. I can copy and paste all of the code but I want to use again. I know there is Package Development in Laravel but it's not easy and feel like there must be a better way. Some days ago I find a pingpong/modules but I don't know about it. It's third party plugin and don't trust it.

Use this plugin is true? Is this plugin is updated later? What's different between Embedd Package Laravel and pingpong/modules? or Do you have any suggestion?

Upvotes: 6

Views: 2181

Answers (3)

Yoram de Langen
Yoram de Langen

Reputation: 5509

Pingpong modules seems to be build for the earlier version of Laravel 5 and in how far they are compatible with future versions (and maybe current 5.1.11) I cannot say.

There isn't much activity going look the commit history for 2.1, as of today(18 dec) the last commit was over 6 months ago.

But is the package specifically designed for Laravel? It seems to. They offer a bunch of features which are useful for development. The only unfortunate thing is you get a LOT of code within your own git environment (is it a good thing? I don't know, what do you prefer).

Personally I don't like it in this way for development, I prefer them in the vendor/ folder else it's a pain to update it to newer a version.

Since Laravel 5 Taylor wanted to make package development not too specific anymore, like in Laravel 4. The only thing what you can do (but not have to) to make your package using Laravel is using the ServiceProvider's. The ServiceProvider is the bootstrap into the Laravel application.

If you want to extend or implement your own functionality, fork the repo and build it yourself on top off it and host it (through github/packagist or a private repo using Satis).

Pingpong modules (2.1) is build for Laravel 5 and they you described (Embedded Laravel Package) is more for Laravel 4, because the more specific way you have to write the package.

But, there is alternative?

Whenever you want a more active project/package for development you should tryout Asgard CMS. They are pretty modular and I thought I read somewhere it was inspired by this package (totally not sure).

How about building yourself?

Of course you can build your own packages to achieve the same result. And create it as modular as you want. I created a lot modules for my company and we can create pretty easy a entire system and using and extending/overriding modules. Even small parts from a module can be overwritten to project specific needs.

We have chosen for almost the same structure as the app/ folder which Laravel projects, in case of CMS/API modules.

A packages look like:

tests/
src/
    Acme/
        Controllers/
        Requests/
    Models/
        Module.php // contains some specifc calculations for example
    ModelServiceProvider.php
composer.json

In the composer.json file we autoload: "Module\\": "src/" And in the config/app.php we register the ModuleServiceProvider. Now we injected the functionality into Laravel's container and can we use it through the app() instance.

But whenever we only want to use the Models with in another project or standalone, we can still use it because the autoloaded features from composer and the way we build the package. Possible to use:

<?php 

    require_once __DIR__ .'/vendor/autoload.php';

    use Module\Models\Module;

    $module = new Module;

Edit

The package structure we like to use, to have a section for API or CMS stuff:

tests/
src/
    Cms/
        Controllers/
        Requests/
    Api/
        Controllers/
        Transformers/
    Models/
        Module.php // contains some specifc calculations for example
    Providers/
        CmsServiceProvider.php // includes `ModuleServiceProvider`
        ApiServiceProvider.php // includes `ModuleServiceProvider`
    ModuleServiceProvider.php // contains global stuff like commands etc.
composer.json

and instead of registering ModuleServiceProvider in config/app.php we register the ApiServiceProvider or CmsServiceProvider depending on the wishes of the client/project.

Upvotes: 3

Tim van Uum
Tim van Uum

Reputation: 1903

I've used pingpong modules. It a pretty cool package. I'm not sure if it's updated much. But it's a very simple package. The only thing it does is create a folder with almost the same structure as in the app folder + views. But these are modules. You can reuse it if you program them right. The same goes for the other answer from jimmy if you have a good structure you can reuse anything.

EDIT

In the image below you'll see an example of pingpong modules. As you it's pretty much the same structure as the app folder. Maybe more the root folder. Normally it runs start.php and you have a routes.php file int he Http folder. I customized mine a bit. And load the frontend and backend routes within the RouteServiceProvider. This is build with laravel 5.1.

enter image description here

Upvotes: 0

Jimmy Obonyo Abor
Jimmy Obonyo Abor

Reputation: 7885

To reuse your classes simply use php namespaces or use to call back your clases.

Using the namespace

namespace Acme\Tools;
class Foo
{
 echo "me";
}

You can the call class foo

<?php 

$foo = new \Acme\Tools\Foo();

Using Use.

You can also use use Statement as below :

<?php
use \Acme\Tools\Foo;
$foo = new Foo();

Use Middleware You should also use middleware to filter who should use the scripts ie the Auth middle-ware , which will help you in filtering users , registrations , logins READ MORE http://laravel.com/docs/5.1/middleware

Use Eloquent Use ORM to create REST apis to your models , its very simple , always let your controller class extend eloquent use Illuminate\Database\Eloquent\Model; ie as :

use Illuminate\Database\Eloquent\Model; .Read More http://laravel.com/docs/5.1/eloquent

Lastly Use Laravel In built Helper functions There are numerous Laravel In built Helper functions , to use simply go over the documentation to help you

Upvotes: 1

Related Questions