项目github地址
自去年开始转为PHP开发,已经有一年。学习php基础之后,便开始使用Laravel框架。
一年来,主要工作是使用Laravel开发电商系统。开发的模块也大体相同。前端,后台,API接口。三个模块是相互独立,又有互相关联的站点。以下,假设三个模块的入口分别为 shopping.cn, admin.shopping.cn, api.shopping.cn。
作为三个独立站点,共享相同的数据库和Model类,若作为三个独立项目开发,显然不够合理。若对数据库结构进行更改,需要三个项目同步修改。然而Laravel框架只提供了一个入口文件,只能搭建一个站点。这里将给出Laravel项目重构策略,实现一个Laravel项目同步开发多个站点。
项目主要结构:
- shopping
* docs
- schema
- file:shopping.mwb
* src
* README.md
通常会在项目路径下添加一个docs路径,存放项目文档,如数据库脚本存放在schema目录下。
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三个模块,以下将以web为例,对项目文件及结构进行修改。
index.php中请求bootstrap目录下的文件app.php。初始化了Application类。这里需要对Application类进行重构。
// index.php
$app = require_once __DIR__.'/../bootstrap/app.php';
// app.php
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
在app目录下新增web\Application类重写Illuminate\Foundation\Application类,主要功能是覆盖项目路径相关的方法,修改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')),
],
// 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');
});
}
}
每个模块的中间件各不相同,重写App\Http\Kernel对中间件初始化进行覆盖。
添加app/Http/Web/Kernel.php类Kernel继承App\Http\Kernel,覆盖middleware和middleware和routeMiddleware。
创建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端一致。只需对其中的文件,命名空间名称进行修改。这样就可以将几个模块组合到一个项目中,并且共享数据库层代码。