Laravel-多站点/项目 [英] Laravel - Multi Site/Project
问题描述
我目前正在寻找一个框架来支持我自己的新框架. Laravel似乎是我最好的选择.
I'm currently looking for a framework to support my own new framework. Laravel seems to be my best choice.
我有一个非常具体的结构.我的项目将具有一个包含通用模型,视图和控制器的基础设计,以及一些具有特定视图的子项目.我们的目标是为同一企业的所有者提供一个平台,在那里我们提供一个独特的网站.在许多情况下,只有设计有所变化,而结构和组件却保持不变.我的想法是在laravel上创建一个可维护的结构,其中每个站点都从其拉动其模型,控制器和视图,如果有特殊需要,可以创建其他视图.
I have a very specific structure in mind. My project will have a base design with common models, views and controllers, as well as some sub projects with there specific views. The goal is to provide a platform for owners of the same business, where we provide a unique website. In many cases only the design varies, whilst the structure and components stay the same. My idea was to create a maintainable structure on laravel wich every sites pulls its models, controllers and views from and if there is a specific need an additional view can be created.
过去有人曾从事过模拟项目吗?
Has somebody had experience with a simular project in the past?
我看到了一些主要障碍:
I see some major obstacles:
- 创建子项目
- 使用多数据库连接
- 使用主项目中的模型和视图
推荐答案
Laravel非常灵活且可高度配置,您完全不需要做这样的事情.例如,对于数据库,您可以创建两个连接:main
,这是到主数据库表的固定连接,而project
对于当前的项目表,则应如下所示:
Laravel is very flexible and highly configurable, you should have no problem doing things like that at all. As for the database, for example, you can create two connections: main
, a fixed connection to your main database tables, and project
for the current project tables, here's what it should look like:
'main' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
'project' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
您应该能够:
配置模型连接:
<?php
namespace App;
class Posts extends Model
{
protected $connection = 'project';
}
直接查询连接:
DB::connection('project')->table('users')->where('activated', true)->get();
在运行时配置数据库:
config([
'database.connections.project.database' => 'project1db',
'database.connections.project.user' => $user,
'database.connections.project.password' => $password,
]);
对于视图,您可以通过执行以下操作告诉Laravel在任何需要的位置查找视图:
As for the views, you can tell Laravel to find views wherever you need by simply doing:
View::addLocation('/path/to/project1/');
这篇关于Laravel-多站点/项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!