3

I am developing an application using Laravel. However I have question about the modular structure of the application.

Here's an example.

Suppose I need a module called "featured items";

This is a small module that should be included in some controllers.

The thing is I'm not sure how to this? Should this be a new controller which will be called inside other controllers?

Should this be a model? Should this be written as a bundle?

Does anyone have any idea about what is the best approach?

Sinan
  • 5,819
  • 11
  • 39
  • 66

4 Answers4

7

I dont think there's a right answer to this really as it is matter of preference though there are things to bear in mind to let you make an intelligent decision as to where your code should be placed.

Does the module have a small footprint and will function with no more than itself?

  • Create a library

Does the module require it's own routes and views but no requirement for libraries?

  • Create an application controller
  • Use Laravel Routes
  • Use bundle controllers

Does the module contain both the above?

  • Create a bundle with controllers and libraries

It's not as simplistic, other factors should be taken into consideration as well. If the module requires multiple controllers for example, you'd be better off writing it in a bundle. As bundles do not require you to have anything more than start.php file within the bundle directory, you register what you want with the Autoloader. From this you can see a lot of peoples preferences would be to build all their modular code in bundles.

David Barker
  • 14,484
  • 3
  • 48
  • 77
1

This package gives you the ability to use Laravel 5 with module system. You can simply drop or generate modules with their own controllers, models, views, translations and a routes file into the app/Modules folder and go on working with them.

Follow the steps

Installation

The best way to install this package is through your terminal via Composer.

Run the following command from your projects root

composer require artem-schander/l5-modular Once this operation is complete, simply add the service provider to your project's config/app.php and you're done.

Service Provider

ArtemSchander\L5Modular\ModuleServiceProvider::class,

Getting started

The built in Artisan command php artisan make:module name [--no-migration] [--no-translation] generates a ready to use module in the app/Modules folder and a migration/translation if necessary.

https://github.com/Artem-Schander/L5Modular

0

How about put it in a library and autoloading it ? http://laravel.com/docs/models#libraries

so that you can just call it in any controller.

Knight
  • 484
  • 4
  • 10
0

Something like featured items would probably do a call to your database. I would make a method in your Product model for example called getFeaturedItems. Then use eloquent or fluent to get the results.

dynamo
  • 382
  • 7
  • 17