使用单个 Laravel 实例的多个项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22555094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple Projects using single laravel instance
提问by kanchan
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 的新手。根据我对这个框架的研究,我发现它对我的项目非常有用。但是,我在自定义它以将 Laravel 框架的单个实例用于多个项目时遇到了困难。我不想遵循 Laravel 中可用的多站点方法,即在项目的模型和控制器中使用目录结构,因为我将无法在 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
通用 Laravel 框架(具有通用库和供应商文件。还具有不同项目使用的通用功能)
应用程序/控制器
应用程序/模型
应用程序/视图
小贩
引导程序
Proj1(拥有自己的实体并且能够使用来自通用 Laravel 框架的通用库和模型函数)
应用程序/控制器
应用程序/模型
应用程序/视图
引导程序
Proj2(拥有自己的实体并且能够使用来自通用 Laravel 框架的通用库和模型函数)
应用程序/控制器
应用程序/模型
应用程序/视图
引导程序
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.
这将减轻我的任务,对我即将进行的项目非常有用。如果有人能帮我解决这个问题,我将不胜感激。
回答by Antonio Carlos Ribeiro
Use Namespaces To Organize and Separate Things
使用命名空间来组织和分离事物
Create PSR-4 autoloading entries to separate your projects files by namespace:
创建 PSR-4 自动加载条目以按命名空间分隔您的项目文件:
"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.
放在 Core 中的所有常见内容,以及相关项目文件中不常见的所有内容。
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:
这是 Core 中的 BaseController,由您的所有控制器使用:
<?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:
这是使用核心控制器的主应用程序的 BaseController:
<?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:
这是 Main 应用程序的 HomeController 使用它的 Base Controller,因为它们在同一个命名空间中,你甚至不需要导入文件:
<?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:
Laravel 中的每个类文件都可以这样组织,甚至是你的视图,所以你可以拥有这些文件:
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
回答by petercoles
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.
您可以在应用程序文件夹下为每个项目创建一个目录,并为您在其下创建的结构中的每个类命名空间。然后,您将使用 PSR(最好是 PSR-4)自动加载来使这些类可访问。
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.
然而,说了这么多,这不是我会做的。你还没有说你为什么要这样构建你的项目,所以你可能有一个很好的理由,但我个人更喜欢把公共库放到他们自己的包中(有助于确保清晰的边界和干净的 api)并使用 Composer将它们拉入每个项目。