You are not logged in.

Important Note: This forum is being archived and will be completely locked down on the 7th of February. The new forum can be found at http://laravel.io.

Announcement

    Laravel 4 Advanced Architecture: Book Now On Sale

#1 2013-05-31 09:52:53

bstrahija
Artisan
From: Croatia
Registered: 2012-08-28
Posts: 158
Website

Simple tutorial on modules in Laravel 4

Sometimes I like to separate my apps into modules to separate my controllers, so I wrote this up wink

http://creolab.hr/2013/05/modules-in-laravel-4/


We can exist in ambiguity, but it means the deepest loneliness.
Tutorials: Part 1 | Part 2 | Part 3 | Part 4 | Modules in L4 | Image manipulation
creolab.hr

Offline

#2 2013-05-31 22:27:19

swizzcom
Artisan
Registered: 2012-09-18
Posts: 16

Re: Simple tutorial on modules in Laravel 4

Hi! I've found your article very interesting! Thank you for sharing this material.

Offline

#3 2013-06-01 08:43:12

bstrahija
Artisan
From: Croatia
Registered: 2012-08-28
Posts: 158
Website

Re: Simple tutorial on modules in Laravel 4

Glad you like it.


We can exist in ambiguity, but it means the deepest loneliness.
Tutorials: Part 1 | Part 2 | Part 3 | Part 4 | Modules in L4 | Image manipulation
creolab.hr

Offline

#4 2013-06-03 08:46:30

bstrahija
Artisan
From: Croatia
Registered: 2012-08-28
Posts: 158
Website

Re: Simple tutorial on modules in Laravel 4

There was a small issue with registering translations, so I updated the article a little bit smile


We can exist in ambiguity, but it means the deepest loneliness.
Tutorials: Part 1 | Part 2 | Part 3 | Part 4 | Modules in L4 | Image manipulation
creolab.hr

Offline

#5 2013-06-12 18:26:03

fizz
Banned
From: USA
Registered: 2013-04-29
Posts: 8

Re: Simple tutorial on modules in Laravel 4

Thanks for the share, it's actually really helpful,

Offline

#6 2013-06-30 09:06:23

summer_charlie
Apprentice
Registered: 2013-05-28
Posts: 1

Re: Simple tutorial on modules in Laravel 4

nice post.


A little knowledge is a dangerous thing, so is a lot.

Offline

#7 2013-07-05 08:12:52

casper
Artisan
From: The Netherlands
Registered: 2013-06-24
Posts: 8
Website

Re: Simple tutorial on modules in Laravel 4

Thanks for sharing.

Offline

#8 2013-07-15 03:43:57

zambranoher
Artisan
Registered: 2013-06-29
Posts: 6

Re: Simple tutorial on modules in Laravel 4

So many thanks, it has been the best way i could make order in my app laravel 4, i hope you will post more of those usefull tutos. cheers!!
btw i prefer this way even than a package modules because, with your method the modules can be arranged in my own way, which I prefer widely

Offline

#9 2013-08-05 18:30:03

Tipo
Artisan
Registered: 2013-02-25
Posts: 117

Re: Simple tutorial on modules in Laravel 4

I tried to use this way to separate my modules too.
The first things works perfect.
But then the problems began with routing.

// Users
Route::post('admin/users/{id}/update', 'UsersController@update');
Route::post('admin/users/{id}/destroy', 'UsersController@destroy');
Route::get('admin/users/{id}/edit', 'UsersController@edit');
Route::post('admin/users/store', 'UsersController@store');
Route::get('admin/users/create', 'UsersController@create');
Route::get('admin/users/{id}', 'UsersController@show');
Route::get('admin/users', 'UsersController@index');

// Admin
Route::get('admin', 'AdminController@index');

When I call that URL:
http://laravel/admin/users/create
I got this error:;

Unable to generate a URL for the named route "admin.users.create" as such route does not exist.

Offline

#10 2013-08-06 16:39:31

indragunawan
Artisan
From: Medan, Indonesia
Registered: 2013-03-18
Posts: 23

Re: Simple tutorial on modules in Laravel 4

when the module have more than one controller so the second one will be class not found, why can be like that?

PROBLEM SOLVED:
just wrong path,
1 module can more than 1 controllers

Last edited by indragunawan (2013-08-06 16:47:17)


Warm Regards From Indonesia

Offline

#11 2013-10-07 14:57:10

bstrahija
Artisan
From: Croatia
Registered: 2012-08-28
Posts: 158
Website

Re: Simple tutorial on modules in Laravel 4

I wanted to use modules in my last project, but wanted to do it via a composer package and none of the existing ones had the simplicity I wanted so I created a package of my own.
Go ahead and check it out: https://packagist.org/packages/creolab/laravel-modules
Also some examples: https://github.com/bstrahija/laravel-modules-example

I would really appreciate any feedback wink


We can exist in ambiguity, but it means the deepest loneliness.
Tutorials: Part 1 | Part 2 | Part 3 | Part 4 | Modules in L4 | Image manipulation
creolab.hr

Offline

#12 2013-10-07 16:14:28

abstractmedia
Artisan
Registered: 2012-05-13
Posts: 146

Re: Simple tutorial on modules in Laravel 4

@bstrahija

I think you get the same problem as me. Your folders are always lowercase but your Namspace always has a capital letter at the beginning.
The works so far only on UNIX system. But not on Windows. I think that you need to move with you also.

Please correct me if I'm wrong.

Offline

#13 2013-10-08 11:14:53

bstrahija
Artisan
From: Croatia
Registered: 2012-08-28
Posts: 158
Website

Re: Simple tutorial on modules in Laravel 4

Which folders do you mean? I don't think this has anything to do with Laravel or this package, maybe there's a composer issue on your side.
It is setup to autoload as a classmap which basicly means that composer scans these folders for classes.
Take a look at "vendor/composer/autoload_classmap.php", because this is the file composer creates, and it should have your paths inside:

return array(
    'App\\Modules\\Auth\\Controllers\\AuthController' => $baseDir . '/app/modules/auth/controllers/AuthController.php',
    'App\\Modules\\Content\\Controllers\\ContentController' => $baseDir . '/app/modules/content/controllers/ContentController.php',
    'BaseController' => $baseDir . '/app/controllers/BaseController.php',
    'DatabaseSeeder' => $baseDir . '/app/database/seeds/DatabaseSeeder.php',
    'HomeController' => $baseDir . '/app/controllers/HomeController.php',
    'IlluminateQueueClosure' => $vendorDir . '/laravel/framework/src/Illuminate/Queue/IlluminateQueueClosure.php',
    'SessionHandlerInterface' => $vendorDir . '/symfony/http-foundation/Symfony/Component/HttpFoundation/Resources/stubs/SessionHandlerInterface.php',
    'TestCase' => $baseDir . '/app/tests/TestCase.php',
    'User' => $baseDir . '/app/models/User.php',
);

We can exist in ambiguity, but it means the deepest loneliness.
Tutorials: Part 1 | Part 2 | Part 3 | Part 4 | Modules in L4 | Image manipulation
creolab.hr

Offline

#14 2013-10-08 11:53:42

bstrahija
Artisan
From: Croatia
Registered: 2012-08-28
Posts: 158
Website

Re: Simple tutorial on modules in Laravel 4

Also, a new blog post on the package: http://creolab.hr/2013/10/modules-laravel-4-part-2/


We can exist in ambiguity, but it means the deepest loneliness.
Tutorials: Part 1 | Part 2 | Part 3 | Part 4 | Modules in L4 | Image manipulation
creolab.hr

Offline

#15 2013-10-08 13:28:37

d~l
Artisan
Registered: 2013-07-03
Posts: 187

Re: Simple tutorial on modules in Laravel 4

@bstrahija
After cutting my teeth in developing one or two packages in workbench I got your modules in your Part 1 tutorial to run successfully ..

But I’m not sure how this new package (which I’ve now installed to try out) changes the manual creation of modules ..

Should I now delete (manually created) ServiceProvider in app/modules/ServiceProvider and extend Creolab\LaravelModules\ServiceProvider in every module?

I can see your addition of meta data which is helpful to switch modules on and off.

Thanks.

Offline

#16 2013-10-08 13:31:39

bstrahija
Artisan
From: Croatia
Registered: 2012-08-28
Posts: 158
Website

Re: Simple tutorial on modules in Laravel 4

No need for service providers if you're using the composer package. I plan on adding some kind of bootstrap classes, but that will come a little bit later, for this package only detects modules and registers stuff like routes, helpers, view, config etc.


We can exist in ambiguity, but it means the deepest loneliness.
Tutorials: Part 1 | Part 2 | Part 3 | Part 4 | Modules in L4 | Image manipulation
creolab.hr

Offline

#17 2013-10-08 16:35:46

slovenianGooner
Artisan
Registered: 2013-09-23
Posts: 63

Re: Simple tutorial on modules in Laravel 4

Well, mate. I used your previous implementation on a new CMS I was developing and the only issue I had was to register every module in the app.php config file. If your new package removes the need for that, I'd say it's a perfect one. Can't wait to start the upgrade to v1.1 of my CMS. I provide a web based installation of modules, and adding a service provider for each module really blows for the user experience!

Super posao, pozdrav iz Slovenije!

EDIT: Also, made a quick fix which comes in handy for me:

// Finder.php

    public function scan()
    {
        // Get all directories in modules path
        if (!is_array($this->app['config']->get('modules::path'))) {
            $modulesPath = array($this->app['config']->get('modules::path'));
        } else {
            $modulesPath = $this->app['config']->get('modules::path');
        }

        foreach ($modulesPath as $index => $pathItem) {
            $directories = $this->app['files']->directories(base_path($pathItem));

            if ($directories)
            {
                foreach ($directories as $directory)
                {
                    // Check if dir contains a module definition file
                    if ($this->app['files']->exists($directory . '/module.json'))
                    {
                        $name                 = pathinfo($directory, PATHINFO_BASENAME);
                        $this->modules[$name] = new Module($name, $directory, null, $this->app);
                    }
                }
            }
        }

        return $this->modules;
    }

// config.php

    'path' => array('app/modules','app/sites'),


That how, you can return an array of paths.

Last edited by slovenianGooner (2013-10-08 16:54:38)

Offline

#18 2013-10-08 18:23:18

bstrahija
Artisan
From: Croatia
Registered: 2012-08-28
Posts: 158
Website

Re: Simple tutorial on modules in Laravel 4

Nice, but it would be better you made a pull request wink


We can exist in ambiguity, but it means the deepest loneliness.
Tutorials: Part 1 | Part 2 | Part 3 | Part 4 | Modules in L4 | Image manipulation
creolab.hr

Offline

#19 2013-10-08 20:16:39

d~l
Artisan
Registered: 2013-07-03
Posts: 187

Re: Simple tutorial on modules in Laravel 4

More questions ...

Q1. Can modules be organised into groups of modules (sub-modules) and module folders scanned recursively to register classes?

Q2. Can Basset asset manager package be used to access module assets (javascripts, stylesheets, images)?

Offline

#20 2013-10-08 20:39:30

slovenianGooner
Artisan
Registered: 2013-09-23
Posts: 63

Re: Simple tutorial on modules in Laravel 4

bstrahija wrote:

Nice, but it would be better you made a pull request wink

Hah, I'm actually quite a n00b on GitHub and Git, but will try. Hopefully you can implement it (if it works for you), so I don't have to change it on each composer update. smile

Offline

#21 2013-10-08 22:25:08

bstrahija
Artisan
From: Croatia
Registered: 2012-08-28
Posts: 158
Website

Re: Simple tutorial on modules in Laravel 4

d~l wrote:

More questions ...

Q1. Can modules be organised into groups of modules (sub-modules) and module folders scanned recursively to register classes?

Q2. Can Basset asset manager package be used to access module assets (javascripts, stylesheets, images)?

1. Not sure what exactly you mean. Can you provide a detailed example?
2. I don't think Basset should have any problems accessing module assets. I didn't use it too much, but as far as I remember you can easily provide different paths for your assets.


We can exist in ambiguity, but it means the deepest loneliness.
Tutorials: Part 1 | Part 2 | Part 3 | Part 4 | Modules in L4 | Image manipulation
creolab.hr

Offline

#22 2013-10-08 22:42:36

bstrahija
Artisan
From: Croatia
Registered: 2012-08-28
Posts: 158
Website

Re: Simple tutorial on modules in Laravel 4

slovenianGooner wrote:
bstrahija wrote:

Nice, but it would be better you made a pull request wink

Hah, I'm actually quite a n00b on GitHub and Git, but will try. Hopefully you can implement it (if it works for you), so I don't have to change it on each composer update. smile

Just added something like that, check it out and let me know if it's fine. If you find any problems at all just open an issue in Github.


We can exist in ambiguity, but it means the deepest loneliness.
Tutorials: Part 1 | Part 2 | Part 3 | Part 4 | Modules in L4 | Image manipulation
creolab.hr

Offline

#23 2013-10-08 22:54:11

d~l
Artisan
Registered: 2013-07-03
Posts: 187

Re: Simple tutorial on modules in Laravel 4

Q1. Can modules be organised into groups of modules (sub-modules) and then module folders scanned recursively?

example of module hierarchy required ...
app/modules/modulegroupA/modulename1/
app/modules/modulegroupA/modulename2/
app/modules/modulegroupA/modulename3/
app/modules/modulegroupA/modulename4/
app/modules/modulegroupB/modulename1/
app/modules/modulegroupB/modulename2/
app/modules/modulegroupB/modulename3/
app/modules/modulegroupB/modulename4/

Offline

#24 2013-10-08 23:17:43

bstrahija
Artisan
From: Croatia
Registered: 2012-08-28
Posts: 158
Website

Re: Simple tutorial on modules in Laravel 4

d~l wrote:

Q1. Can modules be organised into groups of modules (sub-modules) and then module folders scanned recursively?

example of module hierarchy required ...
app/modules/modulegroupA/modulename1/
app/modules/modulegroupA/modulename2/
app/modules/modulegroupA/modulename3/
app/modules/modulegroupA/modulename4/
app/modules/modulegroupB/modulename1/
app/modules/modulegroupB/modulename2/
app/modules/modulegroupB/modulename3/
app/modules/modulegroupB/modulename4/

No, this isn't possible. I don't really see a point why would someone wanna do this.

Last edited by bstrahija (2013-10-08 23:18:06)


We can exist in ambiguity, but it means the deepest loneliness.
Tutorials: Part 1 | Part 2 | Part 3 | Part 4 | Modules in L4 | Image manipulation
creolab.hr

Offline

#25 2013-10-09 10:12:40

slovenianGooner
Artisan
Registered: 2013-09-23
Posts: 63

Re: Simple tutorial on modules in Laravel 4

bstrahija wrote:
slovenianGooner wrote:
bstrahija wrote:

Nice, but it would be better you made a pull request wink

Hah, I'm actually quite a n00b on GitHub and Git, but will try. Hopefully you can implement it (if it works for you), so I don't have to change it on each composer update. smile

Just added something like that, check it out and let me know if it's fine. If you find any problems at all just open an issue in Github.

Will check it out as soon as I get home from work, have it on my local machine. Thanks for the update!

Offline

Board footer

Powered by FluxBB

');