12

I am new to Laravel. As per my research on this framework I find it quite useful for my projects. However I am facing difficulty in customizing it to use a single instance of Laravel framework for multiple projects. I do not want to follow the multi site approach as available in Laravel i.e., using directory structure in models and controllers for projects because I won't be able to push my project related changes in a single step in Git.

I want something like this.

  • common Laravel framework (having common libraries and vendor files. Also having common functionalities used by different projects)

    app/controllers

    app/models

    app/views

    vendor

    bootstrap

  • Proj1 (having its own entities and is able to use common libraries and model functions from common Laravel framework)

    app/controllers

    app/models

    app/views

    bootstrap

  • Proj2 (having its own entities and is able to use common libraries and model functions from common Laravel framework)

    app/controllers

    app/models

    app/views

    bootstrap

i.e., New project must have its own app directory and is able to use model functions from common project.

This will ease my task and would be very useful for my upcoming projects. If someone could help me with this I'll really appreciate.

kajetons
  • 4,481
  • 4
  • 26
  • 37
kanchan
  • 339
  • 1
  • 3
  • 15
  • If you follow Mr. Ribeito option, you can add your projects' folders to .gitignore of the main app and have a separated git for each project. – user2094178 Apr 09 '14 at 18:37

2 Answers2

20

Use Namespaces To Organize and Separate Things

Create PSR-4 autoloading entries to separate your projects files by namespace:

"psr-4": {
    "App\\Core\\": "app/App/Core",
    "App\\Main\\": "app/App/Main",
    "App\\Project1\\": "app/App/Project1",
    "App\\Project2\\": "app/App/Project2"
},

Everything common you put in Core, everything non-common in the related project files.

Now you just have to create your files in theirs respective folders and namespace them:

This is a BaseController in Core, used by all your controllers:

<?php namespace App\Core\Controllers;

use Controller; // This is the Laravel Controller

class BaseController extends Controller {

}

This is a BaseController of the Main application using your Core Controller:

<?php namespace App\Main\Controllers;

use App\Core\Controllers\BaseController as CoreBaseController;

class BaseController extends CoreBaseController {

}

And this is a HomeController of the Main application using your its Base Controller, as they are in the same namespace you don't even need to import the file:

<?php namespace App\Main\Controllers;

class HomeController extends BaseController {

}

Every single class file in Laravel can be organized this way, even your views, so you can have those files:

app/App/Core/Controllers/BaseController.php

app/App/Main/Controllers/BaseController.php
app/App/Main/Controllers/HomeController.php

app/App/Project1/Controllers/BaseController.php
app/App/Project1/Controllers/PostsController.php
app/App/Project1/Controllers/UsersController.php

app/App/Project1/Models/User.php
app/App/Project1/Models/Post.php
app/App/Project1/Models/Roles.php

Then your routes could be separated (organized) and prefixed by namespace:

Route::group(['prefix' => 'project1', 'namespace' => 'App\Project1\Controllers'], function()
{
    Route::get('/', 'HomeController@index');

    Route::get('posts', 'PostsController@index']);
});

Giving those urls:

http://yourdomain.com/project1
http://yourdomain.com/project1/posts

And pointing actions to those controller actions:

App\Project1\Controllers\HomeController@index
App\Project1\Controllers\PostsController@index  
Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
  • This is very nice Mr. Ribeiro. Could you tell me please how do I deal with the routes for each app? Is it correct to say I just have to include them in the core routes.php? – user2094178 Apr 09 '14 at 17:19
  • Thank you again. This is also a good example on how to avoid modules. – user2094178 Apr 09 '14 at 17:46
  • 2
    While this can be done (and it's by no means a bad approach - I actually like it), it takes A LOT more work to maintain singularity between projects. The problems start when you have one project that has more velocity than others and various components need to be upgraded but could impact functionality on other projects. In my experience, it's far easier, cleaner and safer long-term to create a new Laravel project for each 'project' and have your components available as modules. – simonhamp Mar 28 '17 at 11:20
1

There's not really a question here to be answered, but in general terms what you want to do is very feasible.

You could create a directory under the apps folder for each project and namespace each of the classes in the structure you create below it. You'd then use PSR (ideally PSR-4) autoloading to make these classes accessible.

You'd put the routes for each project in their own routes file and then include them through the main routes file, or you could create a service provider for each project, add it your app config file and use that to load the routes for that project.

HOWEVER, all that said, this isn't what I would do. You haven't said why you want to structure your projects this way, so you may have a perfectly good reason, but personally I prefer to put common libraries into their own packages (helps to ensure clear boundaries and clean apis) and use Composer to pull them into each project.

petercoles
  • 1,732
  • 13
  • 20
  • I am thinking to do the same. I have already installed laravel through composer. Then I created another app by copying all the files of the previous project into another folder. Later I tried to add the Intervention/package and i get the Image class not found error. I have realized this may have been added to the original laravel project folder and not to the one i am currently working on. What do you suggest to do? – Pathros Nov 30 '14 at 14:20