Laravel 4 - Package or "modules"? -


i'm starting work laravel 4, seems great choice, anyway before coding first project i'd understand if usual approach ok laravel.

usually keep triad backend , frontend separated under /modules folder, this:

/modules        /backend                /config                /controllers                /models                /migrations                /ecc..        /frontend (and on...) 

with laravel i'm not sure how manage this. i'm trying packages, php artisan workbench me/mypackage --resources don't build entire folder structure... put controllers , models, , how setup routes?

then found link enable modules-like system. so, what's approach should follow keeping things in laravel way?

well, let's begin...

first, created andreyco\cart package using artisan.
package , it's structure

|workbench |-andreyco |---cart |-----public |-----src |-------andreyco |---------cart |-------config |-------lang |-------migrations |-------views |-----tests |-----vendor 

in answers, use exact package example.

imagine, folder workbench/andreyco/cart/src application folder. if do, should know of answers. (actually app package well)

q: how setup routes
a: create file -> workbench/andreyco/cart/src/routes.php. done.

q: where put controllers , models
a: create controllers , models folder there.
testcontroller located @ workbench/andreyco/cart/src/controllers/testcontroller.php file. same models.
directory tree this

|workbench |-andreyco |---cart |-----public |-----src |-------andreyco |---------cart |-------config |-------controlers |-------lang |-------migrations |-------models |-------views |-----tests |-----vendor 

i created routes.php, testcontroller.php , testmodel.php

// workbench/andreyco/cart/src/routes.php <?php  route::get('test', 'andreyco\\cart\\controllers\\testcontroller@index');    // workbench/andreyco/cart/src/controllers/testcontroller.php <?php namespace andreyco\cart\controllers;  use andreyco\cart\models\testmodel;  class testcontroller extends \basecontroller {     public function index()     {         return testmodel::printcurrentlocation(__dir__);     } }    // workbench/andreyco/cart/src/models/testmodel.php <?php namespace andreyco\cart\models;  class testmodel extends \eloquent {     public static function printcurrentlocation($location)     {         return "testing package controller, script located at: {$location}";     } } 

as can see, used namespaces, should.
namespaces make life lot of easier.

important: after creating files, need update composer.json file, classes autoloaded

// composer.json "autoload": {     "classmap": [         ...         "workbench/andreyco/cart/src/controllers",         "workbench/andreyco/cart/src/models"     ] }, 

after this, dump changes using composer dump-autoload -o command.

q: so, what's approach should follow keeping things in laravel way?
a: in opinion, should stick packages. @ least, would. that's way laravel designed work.

i hope helps you, luck!

edit
views not problem here. work in main app package.

// workbench/cart/src/view/foldername/viewname.blade.php  <h1>testing view file.</h1> {{ "blade" }} syntax parsed well, no problem here. 

returning view package's controller pretty simple

public function index()     {         return \view::make('cart::foldername.viewname');     } 

Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -