# start of plugin
[[toc]]
bootstrap.php
is the entry file of the entire plug-in. When the
plug-in is running, the code in bootstrap.php
will be executed first.
For the modularization of the code, the business logic of the plug-in
should not be placed here. bootstrap.php
should only contain the
preparation code before the real logic code, such as judging and
processing the request, adding menu items, routing, event monitoring,
etc. .
# return closure
bootstrap.php
is actually a PHP file that returns a closure. You can
understand that it returns an anonymous function, and all codes must be
written in this anonymous function. In addition, you can use type hints
in the parameter list of this closure (that is, this anonymous
function), and Laravel will automatically resolve the corresponding
dependencies from the container and inject them automatically. (Before
using dependency injection, it is recommended that you first understand
Laravel's service
container (opens new window) )
Commonly used dependencies are:
Illuminate\Contracts\Events\Dispatcher
event listener requires this dependency. Read the Events section for details .Illuminate\Http\Request
can be used to process user requests, such as judging the HTTP verb of the request, obtaining the path of the request, obtaining the data contained in the request body, obtaining Cookies, etc. For details, please read the Laravel documentation (opens new window) .
Also, since 5.0.0, you can get the current plugin instance via the
$plugin
parameter:
return function ( $plugin ) {
// $plugin is the instance of this plugin.
// You can get the front-end resource URL like this:
$plugin- >assets( 'something.css' ) ;
} ;
# hook
Blessing Skin Server provides some hooks for plugins to use, such as adding style files to pages, adding script files, adding menu items, adding routes, etc.
Before calling the hook, you should use App\Services\Hook;
to load the
Hook
class. All hooks are static methods in the Hook
class.
# Add menu item
Through this hook, you can add menu items to the user center of the skin station and the left menu bar of the admin panel, such as:
Hook::addMenuItem( ' user' , 0 , [
'title' => 'Blessing\ExamplePlugin::general.menu' ,
'link' => '/my-page' ,
'icon' => 'fa-cog' ,
'new-tab' => true , // indicates whether to open the link in the new tab of the browser, the default is false
]) ;
The first parameter can only be user
or explore
(that is, the
"Browse" menu under "User Center") or admin
, which means adding menu
items to the left menu bar of "User Center" and "Admin Panel".
The second parameter is used to define the position of the menu item, counting from 0, if this value is too large, the menu item will not be displayed.
The third parameter passed is an array. The array contains title
,
link
, icon
and new-tab
.
title
title
is the display name of the menu item, which will be translated
by the translator (that is, the language file is called), and you also
need to indicate the namespace of the plugin where the language file is
located. In the example, it means to search for the language file from
the Blessing\ExamplePlugin
namespace. Then look for the corresponding
translation value from the namespace, please read the
multilingual section for multilingual.
Also, if you're not using the multilingual feature, it will be the value
of title
itself.
link
link
specifies the link corresponding to the menu item, which can be a
relative path or an absolute path.
icon
If you look carefully, you will find an icon in front of each menu item.
The icon is the icon
that defines the menu item. It uses the icon of
Font Awesome. For a complete list of available icons, please refer to
the official website of Font Awesome (opens new window)
# add route
Please read the routing section.
# Add a style file to the page
Sometimes you may want to add a custom CSS file to the page, you can add
it through addStyleFileToPage
, such as:
Hook::addStyleFileToPage(plugin_assets( ' example-plugin' , 'assets/css/example.css' )) ;
or:
H ook::addStyleFileToPage(plugin( 'example-plugin' )->assets( 'assets/css/example.css' )) ;
# Add a script file to the page
Similarly, you may want to add a custom JavaScript file to the page, you
can add it through addScriptFileToPage
, such as:
H ook::addScriptFileToPage(plugin( 'example-plugin' )->assets( 'assets/js/example.js' ) , [ * ]) ;
What should be explained here is that static resources can also be
obtained through the assets method of the ``plugin .
The first
parameter is the URL to be loaded; the second parameter is optional. The
parameter indicates that the resource file will be loaded only when the
requested URI matches the parameter. Wildcards can be used. This
parameter is optional.
# Register language files for JavaScript
Registering JavaScript language files is quite simple, you just need to
pass the name
value of the plugin as a parameter to the
registerPluginTransScripts
method, such as:
Hook ::registerPluginTransScripts( 'example-plugin' ) ;
Note that the path of the language file must be
lang/{Language}/locale.js in the plugin directory
, where
{Language}
is en
or zh_CN
.
# Show user badge
Calling the addUserBadge
method will add a badge to the user
information panel in the upper left corner of the user center and admin
panel. The usage is as follows:
Hook::addUserBadge( ' text' , 'green' ) ;
The first parameter is the text to be displayed on the badge; the second
parameter is the color, which is optional and defaults to primary
.
# send notification
Use this API to send a notification to the user, which will appear at the top right of the page.
Hook::sendNotification( $ users , $title , $content ) ;
Where $users
is the user to receive this notification. Since 5.0.0, a
single user instance can be passed in.
# Register middleware
Use this API to dynamically add a middleware to Blessing Skin's existing
routes. Note that for routes registered by the plug-in itself, this API
does not need to be used, and only the middleware
method needs to be
called in the registered route .
class M middleware
{
public function handle( $request , $next )
{
return $next ( $request ) ;
}
}
H ook::pushMiddleware( M middleware:: class ) ;
It should be noted that the input to this API must be the middleware itself, not an instance of the middleware class.