模塊是 Notadd 的功能實體,是區(qū)別于 notadd/framework
來說的,notadd/framework
僅是承載 Notadd 體系的邏輯實現(xiàn),并沒有包含功能性代碼。
模塊位于目錄 modules 下,每個模塊在一個獨立的文件夾內,模塊內部的目錄結構如下:
module | 模塊目錄 |
---|---|
-resources | 資源目錄 |
--translations | 翻譯文件目錄 |
--views | 視圖目錄 |
-src | 源碼目錄 |
--ModuleServiceProvider.php | 模塊服務提供者定義文件 |
-composer.json | Composer 配置文件 |
Resources 目錄是 Module 的資源類文件放置的目錄,包含如下幾個類型目錄:
-ModuleServiceProvider 是 Module 的模塊入口文件,也 Module 的所有功能容器示例注冊、路由注入等一系列功能注冊及組件啟動的服務提供者。
namespace Notadd\Content;
use Illuminate\Events\Dispatcher;
use Illuminate\Support\ServiceProvider;
use Notadd\Content\Events\RegisterArticleTemplate;
use Notadd\Content\Events\RegisterArticleType;
use Notadd\Content\Events\RegisterCategoryTemplate;
use Notadd\Content\Events\RegisterCategoryType;
use Notadd\Content\Events\RegisterPageTemplate;
use Notadd\Content\Events\RegisterPageType;
use Notadd\Content\Listeners\CsrfTokenRegister;
use Notadd\Content\Listeners\RouteRegister;
use Notadd\Content\Managers\ArticleManager;
use Notadd\Content\Managers\CategoryManager;
use Notadd\Content\Managers\PageManager;
/**
* Class Module.
*/
class ModuleServiceProvider extends ServiceProvider
{
/**
* Boot service provider.
*/
public function boot()
{
$this->app->make(Dispatcher::class)->subscribe(CsrfTokenRegister::class);
$this->app->make(Dispatcher::class)->subscribe(RouteRegister::class);
$this->loadMigrationsFrom(realpath(__DIR__ . '/../databases/migrations'));
$this->loadTranslationsFrom(realpath(__DIR__ . '/../resources/translations'), 'content');
}
/**
* Register services.
*/
public function register()
{
$this->app->alias('article.manager', ArticleManager::class);
$this->app->alias('category.manager', CategoryManager::class);
$this->app->alias('page.manager', PageManager::class);
$this->app->singleton('article.manager', function ($app) {
$manager = new ArticleManager($app, $app['events']);
$this->app->make(Dispatcher::class)->fire(new RegisterArticleTemplate($app, $manager));
$this->app->make(Dispatcher::class)->fire(new RegisterArticleType($app, $manager));
return $manager;
});
$this->app->singleton('category.manager', function ($app) {
$manager = new CategoryManager($app, $app['events']);
$this->app->make(Dispatcher::class)->fire(new RegisterCategoryTemplate($app, $manager));
$this->app->make(Dispatcher::class)->fire(new RegisterCategoryType($app, $manager));
return $manager;
});
$this->app->singleton('page.manager', function ($app) {
$manager = new PageManager($app, $app['events']);
$this->app->make(Dispatcher::class)->fire(new RegisterPageTemplate($app, $manager));
$this->app->make(Dispatcher::class)->fire(new RegisterPageType($app, $manager));
return $manager;
});
}
}
ModuleServiceProvider 是 Module 的模塊入口文件,也 Module 的所有功能容器示例注冊、路由注入等一系列功能注冊及組件啟動的服務提供者。
namespace Notadd\Content;
use Illuminate\Events\Dispatcher;
use Illuminate\Support\ServiceProvider;
use Notadd\Content\Events\RegisterArticleTemplate;
use Notadd\Content\Events\RegisterArticleType;
use Notadd\Content\Events\RegisterCategoryTemplate;
use Notadd\Content\Events\RegisterCategoryType;
use Notadd\Content\Events\RegisterPageTemplate;
use Notadd\Content\Events\RegisterPageType;
use Notadd\Content\Listeners\CsrfTokenRegister;
use Notadd\Content\Listeners\RouteRegister;
use Notadd\Content\Managers\ArticleManager;
use Notadd\Content\Managers\CategoryManager;
use Notadd\Content\Managers\PageManager;
/**
* Class Module.
*/
class ModuleServiceProvider extends ServiceProvider
{
/**
* Boot service provider.
*/
public function boot()
{
$this->app->make(Dispatcher::class)->subscribe(CsrfTokenRegister::class);
$this->app->make(Dispatcher::class)->subscribe(RouteRegister::class);
$this->loadMigrationsFrom(realpath(__DIR__ . '/../databases/migrations'));
$this->loadTranslationsFrom(realpath(__DIR__ . '/../resources/translations'), 'content');
}
/**
* Register services.
*/
public function register()
{
$this->app->alias('article.manager', ArticleManager::class);
$this->app->alias('category.manager', CategoryManager::class);
$this->app->alias('page.manager', PageManager::class);
$this->app->singleton('article.manager', function ($app) {
$manager = new ArticleManager($app, $app['events']);
$this->app->make(Dispatcher::class)->fire(new RegisterArticleTemplate($app, $manager));
$this->app->make(Dispatcher::class)->fire(new RegisterArticleType($app, $manager));
return $manager;
});
$this->app->singleton('category.manager', function ($app) {
$manager = new CategoryManager($app, $app['events']);
$this->app->make(Dispatcher::class)->fire(new RegisterCategoryTemplate($app, $manager));
$this->app->make(Dispatcher::class)->fire(new RegisterCategoryType($app, $manager));
return $manager;
});
$this->app->singleton('page.manager', function ($app) {
$manager = new PageManager($app, $app['events']);
$this->app->make(Dispatcher::class)->fire(new RegisterPageTemplate($app, $manager));
$this->app->make(Dispatcher::class)->fire(new RegisterPageType($app, $manager));
return $manager;
});
}
}
通過對 Composer 的自定義,可以實現(xiàn) Notadd 風格的目錄結構。
配置 type 屬性為 notadd-module
,會告訴 Composer Installer 將該 Package 安裝到目錄 modules 下,而非默認目錄 vendor 下。
添加 notadd/installers
的 Package,才能調整 Composer 對該類型 Package 的默認處理邏輯,實現(xiàn)重定向安裝目錄的特性。
介于,模塊的安裝方式有兩種,一種方式是:將 Composer Package 寫入程序根目錄的 composer.json
文件,另一種方法是,單獨初始化模塊 Package,并以文件夾的形式放到 modules 目錄,因此,包 notadd/installers
應放置在 require-dev
中。
{
"name": "notadd/content",
"description": "Notadd's Content Module.",
"keywords": [
"notadd",
"cms",
"framework",
"content"
],
"homepage": "https://notadd.com",
"license": "Apache-2.0",
"type": "notadd-module",
"authors": [
{
"name": "twilroad",
"email": "heshudong@ibenchu.com"
}
],
"require": {
"php": ">=7.0"
},
"require-dev": {
"notadd/installers": "0.5.*"
},
"autoload": {
"psr-4": {
"Notadd\\Content\\": "src/"
}
}
}
更多建議: