使用单个laravel实例的多个项目 [英] Multiple Projects using single laravel instance

查看:793
本文介绍了使用单个laravel实例的多个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Laravel的新手.根据我对该框架的研究,我发现它对我的项目非常有用.但是,在自定义它以将Laravel框架的单个实例用于多个项目时,我面临着困难.我不想遵循Laravel中提供的多站点方法,即在项目的模型和控制器中使用目录结构,因为我无法在Git中一步完成与项目相关的更改.

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.

我想要这样的东西.

  • 常见的Laravel框架 (具有通用的库和供应商文件.还具有不同项目所使用的通用功能)

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

应用/控制器

应用/模型

应用/观看次数

供应商

引导程序

Proj1 (具有自己的实体,并能够使用通用Laravel框架中的通用库和模型函数)

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

应用/控制器

应用/模型

应用/观看次数

引导程序

Proj2 (具有自己的实体,并能够使用通用Laravel框架中的通用库和模型函数)

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

应用/控制器

应用/模型

应用/观看次数

引导程序

即,新项目必须具有自己的应用程序目录,并且能够使用普通项目中的模型函数.

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.

推荐答案

使用命名空间来组织和分隔事物

创建PSR-4自动加载项以按名称空间分隔您的项目文件:

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"
},

您在Core中放入的所有常见内容,在相关项目文件中不常见的所有内容.

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:

这是Core中的BaseController,供所有控制器使用:

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 {

}

这是使用您的Core Controller的Main应用程序的BaseController:

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 {

}

这是使用主应用程序的基本控制器的Main应用程序的HomeController,因为它们位于相同的命名空间中,甚至不需要导入文件:

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 {

}

Laravel中的每个单个类文件都可以通过这种方式进行组织,即使您的视图也是如此,因此您可以拥有这些文件:

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']);
});

提供这些网址:

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  

这篇关于使用单个laravel实例的多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆