10

I'm starting to work with Laravel 4, it seems to be a great choice, anyway before coding my first project i'd like to understand if my usual approach could be ok with laravel.

Usually i keep triad for backend and frontend separated under a /modules folder, like this:

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

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

Then i found this link to enable modules-like system. So, what's the approach i should follow for keeping things in the laravel way?

Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
Luciano
  • 1,455
  • 8
  • 22
  • 52

1 Answers1

14

Well, let's begin...

First, I created Andreyco\Cart package using Artisan.
Package and it's structure

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

In the answers, I will use this exact package as example.

Imagine, that folder workbench/andreyco/cart/src is the application folder. If you do, you should know the most of the answers. (Actually App is package as well)

Q: how to setup routes
A: Create the file -> workbench/andreyco/cart/src/routes.php. This is done.

Q: where to put controllers and models
A: Just create controllers and models folder there.
So the TestController would be located at workbench/andreyco/cart/src/controllers/TestController.php file. Very same with models.
Directory tree would look like this

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

I created the routes.php, TestController.php and the 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 is located at: {$location}";
    }
}

As you can see, I used namespaces, so you should.
Namespaces make your life a lot of easier.

Important: after creating those files, you need to update composer.json file, so that classes could be autoloaded

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

After this, dump the changes using composer dump-autoload -o command.

Q: So, what's the approach i should follow for keeping things in the laravel way?
A: In my opinion, you should stick to packages. At least, I would. That's the way Laravel was designed to work.

I hope this helps you, good luck!

Edit
Views are not problem here. They work just like in main app package.

// workbench/cart/src/view/foldername/viewname.blade.php

<h1>Testing view file.</h1>
{{ "Blade" }} syntax is parsed as well, no problem here.

Returning view from package's controller is pretty simple

public function index()
    {
        return \View::make('cart::foldername.viewname');
    }
Andreyco
  • 22,476
  • 5
  • 61
  • 65
  • 1
    Thanks you Andrej. That was really helpful! So, routes must be namespaced, but why the double slash? – Luciano Aug 09 '13 at 10:43
  • 1
    Double backslash is used to escape `\` character. Someone correct me, if I'm wrong. Laravel translates this into namespace correctly – Andreyco Aug 09 '13 at 10:57
  • Apparently I forgot to escape the ` \ ` character in previous comment. – Andreyco Aug 09 '13 at 12:06
  • Ok got it. Seems that routes, if placed inside app/routes.php works without double backslash, anyway now i'm having another doubt: how i use partials view into the package's master template? I've tried namespacing like this @include('backend::_partials.nav') or this @include('backend::_partials/nav') but still not working. – Luciano Aug 09 '13 at 16:14
  • Sorry for being late. I updated the answer with info how to return View object. To answer question about controllers & namespaces: you can use namespaced controllers in `app/routes.php` as well. You do not have to use namespaces at packages (or app) at all - it's up to you, but namespaces can make your live easier. – Andreyco Aug 11 '13 at 18:07
  • @Andreyco - Thanks, this is very useful! @Luciano - You dont namespace your views. You just put them in folders. When you want to include a view, you can just do `@include(aaaa.bbbb.cccc.view)` This would refer to the following directory structure `views/aaaa/bbbb/cccc/view.blade.php` Hope this helps – Gravy Oct 01 '13 at 22:36
  • Oh, and from within a package, the view can be referred to by `packagename::viewfolder.viewname` – Gravy Oct 01 '13 at 22:41