Could two projects use same database?

2.1k views Asked by At

The question might sound vague but in fact, it's something I've been wondering for some time and I don't know how can I do it.

I've been working with Laravel 5 for a while, but most of the projects I've developed using Laravel, have the frontend app and the backend app in the same project This time the requirement is to make two separate projects, one for the frontend, and the other one for the backend.

The inquiere comes up when trying to figure out how will the controller interact with the Model, and how will the migrations be handled? Will I have to make a model for each table in both the frontend and the backend?

The goal is to use the same database for both projects, without having problems with the migrations and the models.

EDIT 1 What i mean by Frontend App is the website where the data will be displayed, where people will interact, where they will see something. The Backend App is the one where the admins will edit the content of the frontend, where they will be able to CRUD to the database; in the frontend app i just want to select data from db.

2

There are 2 answers

7
Ariful Haque On

What assumption from your question is, your backend will insert, update, delete or do any other query to your database and your frontend will be responsible for controlling forms, pages and showing data. With this model, if you want to use the same Models and migrations, you'll actually not need to create separate projects for backend and frontend. I've managed to separate the backend (api/v1) and frontend in same projects using separated routes. I'll describe it bellow.

Folder Structure
Folder Structure

For my backend controllers, I've put them in api/v1 folder. For each controller in api/v1 folder, use the namespace Api\V1 like this.

<?php namespace App\Http\Controllers\Api\V1;

  use App\MyModel;

Now to call all of your backend api using router, use route group. Example:

Route::group(
    [
        'namespace' => 'Api\V1',
        'prefix' => 'api/v1'
    ], function(){

        Route::get('counties', ['uses'=>'CountiesApiController@index']);
        Route::get('counties/{id}', ['uses'=>'CountiesApiController@show']);

        //... your all other routes here

});

Now call your backend api controllers from anywhere in your projects. Your api can use any model in your projects and obviously your other main controllers will work as your frontend controller and you can do all regular stuffs.

0
Watercayman On

I had this same issue, and thought I'd answer for anyone in future who may wish to do same. There are many cases where you may have two separate apps that need to draw from the same database. In my case, it was a backend that input various data sets, and a frontend/client-facing site that clients drew information from a specific area. Both were huge apps, and having them separate was a benefit.

The magic comes from a few very simple lines in composer surrounding PSR4 namespacing.

If you know in advance of creating both projects which models you want to share, you can simply create a separate folder and put all of these models into namespace 'Share' or somesuch. Then pull them down into both apps via composer's autoload in same way as below.

If, like me, you have a project already completed (and don't want to change namespacing at this point in the original) and NOW want to add another project that uses many of the same models, here's what I did:

  • Create new Laravel project
  • Set new namespace on new project as something different than 'App'

    php artisan app:name NewName

  • Make sure to do this step first! If you open files, or make changes, the auto-re-namespace doesn't work.

  • Add instructions in Composer.json to look for all old models (and anything else) via the namespace "App", and all models of the current app under NewName (NOTE you can go above the root install folder):

"autoload": { "classmap": [ "database" ], "psr-4": { "NewName\\\": "app/", "App\\\": "../original-project-folder/app/" } }, - Because migrations may depend on a local class model, you need to keep migrations for the new project in the new project folder. You can run these after the original migration and it works fine.

  • If, like me, you have a different set of users for app#2, and want to change the User model (so as not to use the original app User class), this is a bit more work, mainly around authorization. Make changes to app/config/auth.php to ensure the right class name is available for your new user class.

Took me forever to figure this out - it's not easily searchable. Hope this helps someone.