使用单个 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)
应用程序/控制器
应用程序/模型
应用程序/视图
供应商
引导
项目1(拥有自己的实体并且能够使用来自通用 Laravel 框架的通用库和模型函数)
Proj1 (having its own entities and is able to use common libraries and model functions from common Laravel framework)
应用程序/控制器
应用程序/模型
应用程序/视图
引导
项目2(拥有自己的实体并且能够使用来自通用 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 AppCoreControllers;
use Controller; // This is the Laravel Controller
class BaseController extends Controller {
}
这是使用您的核心控制器的主应用程序的 BaseController:
This is a BaseController of the Main application using your Core Controller:
<?php namespace AppMainControllers;
use AppCoreControllersBaseController as CoreBaseController;
class BaseController extends CoreBaseController {
}
这是主应用程序的 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 AppMainControllers;
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' => 'AppProject1Controllers'], 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:
AppProject1ControllersHomeController@index
AppProject1ControllersPostsController@index
相关文章