Application Plugin
Each application plugin is a complete application, with the source code placed in the {main project}/plugin
directory.
Tip
Use the commandphp webman app-plugin:create {plugin name}
to create an application plugin locally.
For example,php webman app-plugin:create cms
will create the following directory structure:
plugin/
└── cms
├── app
│ ├── controller
│ │ └── IndexController.php
│ ├── exception
│ │ └── Handler.php
│ ├── functions.php
│ ├── middleware
│ ├── model
│ └── view
│ └── index
│ └── index.html
├── config
│ ├── app.php
│ ├── autoload.php
│ ├── container.php
│ ├── database.php
│ ├── exception.php
│ ├── log.php
│ ├── middleware.php
│ ├── process.php
│ ├── redis.php
│ ├── route.php
│ ├── static.php
│ ├── thinkorm.php
│ ├── translation.php
│ └── view.php
└── public
We see that an application plugin has the same directory structure and configuration files as webman. In fact, developing an application plugin provides an experience that is basically the same as developing a webman project, with a few points to pay attention to.
Namespace
The directory and naming of the plugin follow the PSR4 standard. Since plugins are placed in the plugin directory, their namespaces start with plugin
, for example, plugin\cms\app\controller\UserController
, where cms is the main directory of the plugin's source code.
URL Access
The URL address paths of application plugins all start with /app
, for example, the URL for plugin\cms\app\controller\UserController
is http://127.0.0.1:8787/app/cms/user
.
Static Files
Static files are placed under plugin/{plugin}/public
. For example, accessing http://127.0.0.1:8787/app/cms/avatar.png
actually retrieves the file plugin/cms/public/avatar.png
.
Configuration Files
The configuration of the plugin is the same as that of an ordinary webman project. However, the configuration of the plugin generally only affects the current plugin and has no effect on the main project.
For instance, the value of plugin.cms.app.controller_suffix
only affects the controller suffix of the plugin and does not impact the main project.
Similarly, the values for plugin.cms.app.controller_reuse
, plugin.cms.middleware
, plugin.cms.view
, plugin.cms.container
, and plugin.cms.exception
only affect their respective aspects in the plugin and do not influence the main project.
However, since routing is global, the routes configured in the plugin also affect the global settings.
Accessing Configuration
To access a specific plugin configuration, use the method config('plugin.{plugin}.{specific configuration}');
, for example, to get all configurations from plugin/cms/config/app.php
, use config('plugin.cms.app')
.
Likewise, the main project or other plugins can use config('plugin.cms.xxx')
to access configurations of the cms plugin.
Unsupported Configurations
Application plugins do not support configurations for server.php and session.php, and do not support app.request_class
, app.public_path
, and app.runtime_path
configurations.
Database
Plugins can configure their own databases. For example, the content of plugin/cms/config/database.php
is as follows:
return [
'default' => 'mysql',
'connections' => [
'mysql' => [ // mysql is the connection name
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => 3306,
'database' => 'database',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_general_ci',
],
'admin' => [ // admin is the connection name
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => 3306,
'database' => 'database',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_general_ci',
],
],
];
The way to reference it is Db::connection('plugin.{plugin}.{connection name}');
, for example:
use support\Db;
Db::connection('plugin.cms.mysql')->table('user')->first();
Db::connection('plugin.cms.admin')->table('admin')->first();
If you want to use the database of the main project, you can do so directly, for example:
use support\Db;
Db::table('user')->first();
// Assuming the main project has also configured an admin connection.
Db::connection('admin')->table('admin')->first();
Tip
Thinkorm has a similar usage.
Redis
The usage of Redis is similar to that of databases. For example, plugin/cms/config/redis.php
:
return [
'default' => [
'host' => '127.0.0.1',
'password' => null,
'port' => 6379,
'database' => 0,
],
'cache' => [
'host' => '127.0.0.1',
'password' => null,
'port' => 6379,
'database' => 1,
],
];
Usage is as follows:
use support\Redis;
Redis::connection('plugin.cms.default')->get('key');
Redis::connection('plugin.cms.cache')->get('key');
Similarly, if you want to reuse the Redis configuration of the main project:
use support\Redis;
Redis::get('key');
// Assuming the main project has also configured a cache connection.
Redis::connection('cache')->get('key');
Logs
The usage of the log class is also similar to that of the database:
use support\Log;
Log::channel('plugin.admin.default')->info('test');
If you want to reuse the main project's log configuration, just use it directly:
use support\Log;
Log::info('log content');
// Assuming the main project has a test log configuration
Log::channel('test')->info('log content');
Installing and Uninstalling Application Plugins
To install an application plugin, simply copy the plugin directory to the {main project}/plugin
directory. It will take effect after a reload or restart.
To uninstall, simply delete the corresponding plugin directory from {main project}/plugin
.