Laravelプロジェクト再構築ポリシー
Laravelプロジェクト再構築ポリシー
プロジェクトgithubアドレス
昨年からPHP開発に移行して1年.phpベースを学習した後、Laravelフレームワークを使用し始めました.
1年間、主な仕事はLaravelを使って電子商取引システムを開発することです.開発したモジュールもほぼ同じである.フロントエンド、バックグラウンド、APIインタフェース.3つのモジュールは互いに独立しており、相互に関連するサイトもあります.以下、3つのモジュールの入口がそれぞれshoppingであるとする.cn, admin.shopping.cn, api.shopping.cn.
3つの独立したサイトとして、同じデータベースとModelクラスを共有し、3つの独立したプロジェクトとして開発すると、明らかに合理的ではありません.データベース構造を変更するには、3つのプロジェクトを同期して変更する必要があります.しかし、Laravelフレームワークは1つのエントリファイルしか提供されず、1つのサイトしか構築できません.ここでは、Laravelプロジェクトの再構築ポリシーを示し、1つのLaravelプロジェクトが複数のサイトを同期的に開発することを実現します.
プロジェクトの主な構成:
通常、データベース・スクリプトがschemaディレクトリに格納されるなど、プロジェクト・ドキュメントを格納するdocsパスがプロジェクト・パスの下に追加されます.srcパスの下にLaravelプロジェクトコードがあります.元のコード構造は公式サイトの紹介を参照することができます.
プロジェクト再構築
Laravelプロジェクトはweb,admin,apiの3つのモジュールに拡張され,以下はwebを例にプロジェクトファイルと構造を修正する.
プロジェクトエントリの構成エントリファイル:publicフォルダをコピーし、フロントウェブプロジェクトのエントリとしてwww-webに名前を変更し、関連する静的ファイルを格納します.サーバがwww-webを指すように設定し、ドメイン名をshoppingに設定する.cn
アプリケーションの書き換え
index.phpでbootstrapディレクトリの下のファイルappを要求する.php.アプリケーションクラスを初期化しました.ここではApplicationクラスを再構築する必要があります.
appディレクトリにWebApplicationクラスを追加IlluminateFoundationApplicationクラスを書き換え、主な機能はプロジェクトパスに関するメソッドを上書きし、publicPath、configPath、langPath、storagePathなどのパスを変更することです.
関連パスの変更 config:configディレクトリの下にwebフォルダを追加し、configの下のプロファイルをconfig/webにコピーします. lang:resource/langディレクトリの下にあるwebディレクトリを追加し、翻訳ファイルをコピーします. view:resource/viewディレクトリの下にwebディレクトリを追加し、web側のビューファイルを格納します.config/web/viewを変更します.php
新しいアプリケーションをロードします.env bootstrap/appを参照.phpファイルbootstrap/webを追加します.app.php、indexを変更します.php.ApplicationのloadEnvironmentFromを呼び出してwebのプロファイルをロードします.env.Webで、プロジェクトルートパスの下にプロファイルを追加します.一部のコードは次のとおりです:
サービスプロバイダの書き換え Providerパスの下にWebパスを追加し、ServiceProviderをこのディレクトリの下にコピーし、ネーミングスペースの変更に注意します. config/web/appを修正する.phpサービスプロバイダのパスを変更します. RouteServiceProviderを変更し、controllerパスとルーティングファイルパスを変更し、webを作成します.routes.php
AppHttpKernelを書き換える
各モジュールのミドルウェアはそれぞれ異なり、AppHttpKernelを書き換えてミドルウェアの初期化を上書きします.app/Http/Web/Kernelを追加します.phpクラスKernelはAppHttpKernelを継承し、middlewareとmiddlewareとrouteMiddlewareを上書きします.
artisanファイルの書き換え
artisanを作成します.Webファイル、artisanを参照して内容を変更する
初期化モデル
appディレクトリの下にModelsフォルダを作成し、Modelsクラスを初期化
これで,webエンドの構築を完了し,admin,api構築プロセスはwebエンドと一致する.ファイル、ネーミングスペース名を変更するだけです.これにより、いくつかのモジュールを1つのプロジェクトに組み合わせ、データベース・レイヤ・コードを共有できます.
プロジェクトgithubアドレス
昨年からPHP開発に移行して1年.phpベースを学習した後、Laravelフレームワークを使用し始めました.
1年間、主な仕事はLaravelを使って電子商取引システムを開発することです.開発したモジュールもほぼ同じである.フロントエンド、バックグラウンド、APIインタフェース.3つのモジュールは互いに独立しており、相互に関連するサイトもあります.以下、3つのモジュールの入口がそれぞれshoppingであるとする.cn, admin.shopping.cn, api.shopping.cn.
3つの独立したサイトとして、同じデータベースとModelクラスを共有し、3つの独立したプロジェクトとして開発すると、明らかに合理的ではありません.データベース構造を変更するには、3つのプロジェクトを同期して変更する必要があります.しかし、Laravelフレームワークは1つのエントリファイルしか提供されず、1つのサイトしか構築できません.ここでは、Laravelプロジェクトの再構築ポリシーを示し、1つのLaravelプロジェクトが複数のサイトを同期的に開発することを実現します.
プロジェクトの主な構成:
- shopping
* docs
- schema
- file:shopping.mwb
* src
* README.md
通常、データベース・スクリプトがschemaディレクトリに格納されるなど、プロジェクト・ドキュメントを格納するdocsパスがプロジェクト・パスの下に追加されます.srcパスの下にLaravelプロジェクトコードがあります.元のコード構造は公式サイトの紹介を参照することができます.
app ;
bootstrap , cache ;
config ;
database , SQLite ;
public ( 、JavaScript、CSS );
resources (LESS、SASS、CoffeeScript), ;
storage Blade 、 session、 , , app、framework logs ,app ,framework , ,logs ;
tests , PHPUnit ;
vendor Composer
プロジェクト再構築
Laravelプロジェクトはweb,admin,apiの3つのモジュールに拡張され,以下はwebを例にプロジェクトファイルと構造を修正する.
プロジェクトエントリの構成
アプリケーションの書き換え
index.phpでbootstrapディレクトリの下のファイルappを要求する.php.アプリケーションクラスを初期化しました.ここではApplicationクラスを再構築する必要があります.
// index.php
$app = require_once __DIR__.'/../bootstrap/app.php';
// app.php
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
appディレクトリにWebApplicationクラスを追加IlluminateFoundationApplicationクラスを書き換え、主な機能はプロジェクトパスに関するメソッドを上書きし、publicPath、configPath、langPath、storagePathなどのパスを変更することです.
namespace App\Web;
use Illuminate\Foundation\Application as LaravelApplication;
class Application extends LaravelApplication
{
/**
* Get the path to the public / web directory.
*
* @return string
*/
public function publicPath()
{
return $this->basePath . DIRECTORY_SEPARATOR . 'www-web';
}
/**
* Get the path to the application configuration files.
*
* @return string
*/
public function configPath()
{
return $this->basePath . DIRECTORY_SEPARATOR . 'config' .
DIRECTORY_SEPARATOR . 'web';
}
/**
* Get the path to the language files.
*
* @return string
*/
public function langPath()
{
return $this->basePath . DIRECTORY_SEPARATOR . 'resources' .
DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR . 'web';
}
/**
* Get the path to the storage directory.
*
* @return string
*/
public function storagePath()
{
return $this->storagePath ?: $this->basePath . DIRECTORY_SEPARATOR . 'storage' .
DIRECTORY_SEPARATOR . 'web';
}
/**
* Get the path to the configuration cache file.
*
* @return string
*/
public function getCachedConfigPath()
{
return $this->basePath().'/bootstrap/web/cache/config.php';
}
/**
* Get the path to the routes cache file.
*
* @return string
*/
public function getCachedRoutesPath()
{
return $this->basePath().'/bootstrap/web/cache/routes.php';
}
/**
* Get the path to the cached "compiled.php" file.
*
* @return string
*/
public function getCachedCompilePath()
{
return $this->basePath().'/bootstrap/web/cache/compiled.php';
}
/**
* Get the path to the cached services.json file.
*
* @return string
*/
public function getCachedServicesPath()
{
return $this->basePath().'/bootstrap/web/cache/services.json';
}
}
関連パスの変更
'paths' => [
realpath(base_path('resources/views/web')),
],
新しいアプリケーションをロードします.env
// index.php
$app = require_once __DIR__.'/../bootstrap/web.app.php';
// web.app.php
$app = new Web\Application(
realpath(__DIR__.'/../')
);
$app->loadEnvironmentFrom('.env.web');
サービスプロバイダの書き換え
namespace App\Providers\Web;
use Illuminate\Routing\Router;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to the controller routes in your routes file.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers\Web';
/**
* Define your route model bindings, pattern filters, etc.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
public function boot(Router $router)
{
//
parent::boot($router);
}
/**
* Define the routes for the application.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
public function map(Router $router)
{
$router->group(['namespace' => $this->namespace], function ($router) {
require app_path('Http/web.routes.php');
});
}
}
AppHttpKernelを書き換える
各モジュールのミドルウェアはそれぞれ異なり、AppHttpKernelを書き換えてミドルウェアの初期化を上書きします.app/Http/Web/Kernelを追加します.phpクラスKernelはAppHttpKernelを継承し、middlewareとmiddlewareとrouteMiddlewareを上書きします.
artisanファイルの書き換え
artisanを作成します.Webファイル、artisanを参照して内容を変更する
#!/usr/bin/env php
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader
| for our application. We just need to utilize it! We'll require it
| into the script here so that we do not have to worry about the
| loading of any our classes "manually". Feels great to relax.
|
*/
require __DIR__ . '/app/Common/helpers.php';
require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/web.app.php';
/*
|--------------------------------------------------------------------------
| Run The Artisan Application
|--------------------------------------------------------------------------
|
| When we run the console application, the current CLI command will be
| executed in this console and the response sent back to a terminal
| or another output device for the developers. Here goes nothing!
|
*/
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$status = $kernel->handle(
$input = new Symfony\Component\Console\Input\ArgvInput,
new Symfony\Component\Console\Output\ConsoleOutput
);
/*
|--------------------------------------------------------------------------
| Shutdown The Application
|--------------------------------------------------------------------------
|
| Once Artisan has finished running. We will fire off the shutdown events
| so that any final work may be done by the application before we shut
| down the process. This is the last thing to happen to the request.
|
*/
$kernel->terminate($input, $status);
exit($status);
初期化モデル
appディレクトリの下にModelsフォルダを作成し、Modelsクラスを初期化
namespace App\Models;
use Illuminate\Database\Eloquent\Model as EloquentModel;
class Model extends EloquentModel
{
const CREATED_AT = 'created';
const UPDATED_AT = 'updated';
public $timestamps = false;
public $incrementing = true;
protected $guarded = [];
protected $dates = [];
}
これで,webエンドの構築を完了し,admin,api構築プロセスはwebエンドと一致する.ファイル、ネーミングスペース名を変更するだけです.これにより、いくつかのモジュールを1つのプロジェクトに組み合わせ、データベース・レイヤ・コードを共有できます.