### Basic Event Listener Setup Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/backend-event.md In your plugin's bootstrap.php, you can access the event dispatcher to set up listeners. This is the entry point for registering event handlers. ```php use Illuminate\Contracts\Events\Dispatcher; return function (Dispatcher $events) { // ... }; ``` -------------------------------- ### Listen for the 'mounted' Event Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/frontend-event.md Use the `blessing.event.on` method to listen for frontend events. This example shows how to execute a callback when the 'mounted' event is triggered. ```javascript blessing.event.on('mounted', () => { // ... }) ``` -------------------------------- ### Applying Middleware to a Specific Route Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/route.md You can apply middleware to individual routes, not just route groups. This example applies 'web' and 'auth' middleware to a GET route. ```php Hook::addRoute(function ($routes) { $routes->get('foo/bar', 'FoobarController@showPage')->middleware(['web', 'auth']); }); ``` -------------------------------- ### Defining Basic GET and POST Routes Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/route.md Register distinct routes for different HTTP methods (e.g., GET and POST) even if they share the same URI path. ```php Hook::addRoute(function ($routes) { $routes->get('foo/bar', 'FoobarController@showPage'); $routes->post('foo/bar', 'FoobarController@foobar'); }); ``` -------------------------------- ### Get or Set Database Options Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/helpers.md The `option` function retrieves or sets configuration values stored in the database. You can provide a default value to be returned if the option does not exist. ```php $value = option('my_custom_option'); // 获取选项 `my_custom_option` 的值 ``` ```php $value = option('my_custom_option', 'default'); // 若选项不存在,则将 `'default'` 作为默认值来返回 ``` ```php option(['my_custom_option' => 'Yeah']); // 将选项 `my_custom_option` 的值设为 `'Yeah'` ``` -------------------------------- ### Define Plugin Callbacks Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/lifecycle.md The `callbacks.php` file should return an array mapping event classes to callback functions. This example shows the basic structure. ```php return [ App\Events\PluginWasEnabled::class => function (App\Services\PluginManager $manager) { // Plugin initialization logic here } ]; ``` -------------------------------- ### Get Plugin Instance Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/helpers.md Use the `plugin` function to retrieve an instance of a specified plugin by its unique identifier. ```php $plugin = plugin('example-plugin'); ``` -------------------------------- ### Get Localized Database Options Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/helpers.md Similar to `option`, `option_localized` retrieves configuration values that can differ based on the current locale. To set localized values, switch to the target language before setting the option. ```php // 假定 `greeting` 选项在中文环境为「你好」,英文环境下为 "hello" // en option_localized('greeting'); // hello // zh_CN option_localized('greeting'); // 你好 ``` -------------------------------- ### Get Plugin Asset URL Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/helpers.md The `plugin_assets` function generates a URL for a specific asset file within a plugin's directory. ```php $url = plugin_assets('example-plugin', 'assets/js/example1.js'); ``` -------------------------------- ### Trigger a Custom Event Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/frontend-event.md Use the `blessing.event.emit` method to trigger events. This example demonstrates triggering 'eventA' and passing a data variable. Consider using `Object.freeze` for read-only data. ```javascript blessing.event.emit('eventA', data) // 假设 `data` 变量已定义 ``` -------------------------------- ### Accessing User Closet Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/migration.md Access a user's closet by calling the closet() method on the User model instance and then get() to retrieve all items as a Laravel Collection. This replaces the removed Closet model. ```php $user->closet()->get(); ``` -------------------------------- ### Find Users by Score Condition Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/models.md Use the `where` and `get` static methods to find all User model instances where the score is greater than a specified value. Returns an empty collection if no users match. ```php $users = User::where('score', '>', 50)->get(); ``` -------------------------------- ### Get User's Players Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/models.md Access the `players` relationship on a User model instance to retrieve a collection of all associated Player model instances. ```php $players = $user->players; ``` -------------------------------- ### Getting Item Name from Closet Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/migration.md Retrieve the item name from a texture within a user's closet using the pivot object. This is useful when displaying closet contents. ```php $texture->pivot->item_name; ``` -------------------------------- ### Get Player's Owner Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/models.md Access the `user` relationship on a Player model instance to retrieve the associated User model instance that owns the player. ```php $owner = $player->user; ``` -------------------------------- ### Get Current User Model Instance Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/auth.md Retrieve the authenticated user's model instance using the `user` method. This method returns `null` if the user is not logged in, so no prior `check` is needed. ```php $user = auth()->user(); // 这里的 $user 就是 App\Models\User 的实例,因此 User 模型实例中的方法都能用。 ``` -------------------------------- ### Registering Routes for Any HTTP Action Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/route.md Use the 'any' method to create a route that responds to all possible HTTP actions. ```php Hook::addRoute(function ($routes) { $routes->any('foo/bar', 'FoobarController@doSomething'); }); ``` -------------------------------- ### 返回闭包 Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/bootstrap.md bootstrap.php 文件返回一个闭包,所有插件的准备代码应写在此闭包中。可以使用类型提示注入 Laravel 的服务容器依赖。 ```php return function ($plugin) { // $plugin 就是这个插件的实例。 // 可以像下面这样获取前端资源 URL: $plugin->assets('something.css'); }; ``` -------------------------------- ### Get Current User's UID Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/auth.md Access the current user's unique identifier (uid). The `id` method provides a direct and convenient way to get this value. ```php $uid = auth()->user()->uid; ``` ```php $uid = auth()->id(); ``` -------------------------------- ### Create a Form Instance Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/form.md Create a form instance using the Option Facade. The first parameter is a unique form name, the second is the display title (or OptionForm::AUTO_DETECT for multilingual support), and the third is a callback function to define form items. ```php $form = Option::form('my_form', '表单示例', function ($form) { // }); ``` -------------------------------- ### Create a Database Table Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/database.md Use the Schema facade to create a new database table. The table name does not require a prefix. A callback function defines the table's columns. ```php Schema::create('my_table', function ($table) { // $table parameter has many instance methods }); ``` -------------------------------- ### 注册中间件 Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/bootstrap.md 使用 Hook::pushMiddleware 为 Blessing Skin 的现有路由动态增加中间件。传入的是中间件类名,而非实例。 ```php class Middleware { public function handle($request, $next) { return $next($request); } } Hook::pushMiddleware(Middleware::class); ``` -------------------------------- ### Create New User Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/models.md Instantiate a new User model, set its attributes (like email and nickname), and call `save` to add it to the database. Auto-incrementing primary keys are handled automatically. ```php // 创建一个新的用户模型实例 $user = new User(); $user->email = 'a@b.c'; $user->nickname = 'abc'; $user->save(); ``` -------------------------------- ### Check and Create Database Table Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/database.md Before creating a table, check if it already exists using Schema::hasTable. If it does not exist, proceed with table creation. ```php if (! Schema::hasTable('my_table')) { Schema:::create('my_table', function ($table) {}); } ``` -------------------------------- ### Registering Routes for Multiple HTTP Actions Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/route.md Use the 'match' method to register a single route that responds to multiple specified HTTP actions. ```php Hook::addRoute(function ($routes) { $routes->match(['get', 'post'], 'foo/bar', 'FoobarController@doSomething'); }); ``` -------------------------------- ### Utils Class Replacement with Helper Functions Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/migration.md The 'App\Services\Utils' class has been removed in BS v4. Use the provided helper functions instead. Below are some common method mappings. ```php getClientIp ``` ```php get_client_ip ``` ```php isRequestSecure ``` ```php is_request_secure ``` ```php getTimeFormatted ``` ```php get_datetime_string ``` ```php getStringReplaced ``` ```php get_string_replaced ``` -------------------------------- ### 添加样式文件 Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/bootstrap.md 使用 Hook::addStyleFileToPage 向页面添加自定义的 CSS 文件。可以使用 plugin_assets 或 plugin('plugin-name')->assets 方法获取资源 URL。 ```php Hook::addStyleFileToPage(plugin_assets('example-plugin', 'assets/css/example.css')); ``` ```php Hook::addStyleFileToPage(plugin('example-plugin')->assets('assets/css/example.css')); ``` -------------------------------- ### Return a View from Controller Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/view.md Use the global 'view' helper function in your controller to return a view. Ensure the view path includes the plugin's namespace and uses dot notation for directory traversal. ```php public function showLoginPage() { return view('Author\MyPlugin::hello.world'); } ``` -------------------------------- ### 添加脚本文件 Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/bootstrap.md 使用 Hook::addScriptFileToPage 向页面添加自定义的 JavaScript 文件。可选的第二个参数可以指定仅在匹配的 URI 请求时加载该文件。 ```php Hook::addScriptFileToPage(plugin('example-plugin')->assets('assets/js/example.js'), [*]); ``` -------------------------------- ### 监听 beforeFetch 事件并修改请求数据 Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/fetch.md 在调用 Fetch API 之前,可以通过监听 beforeFetch 事件来修改请求的数据。此示例展示了如何在用户注册时添加邀请码。 ```javascript blessing.event.on('beforeFetch', request => { request.data.invitationCode = $('#invitation-code').val() }) ``` -------------------------------- ### Listening to a Specific Event Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/backend-event.md Use the `$events->listen()` method to register a callback for a specific event. The callback receives the event instance as an argument. ```php $events->listen(App\Events\RenderingFooter::class, function ($event) { // $event-> ... }); ``` -------------------------------- ### YAML 语言文件结构示例 Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/i18n.md 展示了 YAML 语言文件的基本结构,包括键值对、多行文本和层级分类。 ```yaml title: 欢迎访问 content: header: 您好,:name footer: '某些时候您可以用引号包住语言值' body: | 这是 多行 文本 ``` -------------------------------- ### 注册 JavaScript 语言文件 Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/bootstrap.md 使用 Hook::registerPluginTransScripts 注册插件的 JavaScript 语言文件。语言文件应位于插件目录下的 lang/{Language}/locale.js。 ```php Hook::registerPluginTransScripts('example-plugin'); ``` -------------------------------- ### YAML 语言文件用于前端 v5 Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/i18n.md 展示了 Blessing Skin v5 中如何使用 YAML 文件作为前端多语言文件,无需手动注册,直接通过插件名访问。 ```yaml name: kumiko ``` -------------------------------- ### 添加菜单项 Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/bootstrap.md 使用 Hook::addMenuItem 向用户中心或管理面板添加菜单项。可以指定菜单项的标题、链接、图标和是否在新标签页打开。 ```php Hook::addMenuItem('user', 0, [ 'title' => 'Blessing\ExamplePlugin::general.menu', 'link' => '/my-page', 'icon' => 'fa-cog', 'new-tab' => true, // 表示是否在浏览器新标签页中打开链接,默认为 false ]); ``` -------------------------------- ### Controller Method for Route Parameters Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/route.md In the controller method, accept parameters defined in the route URI. Provide default values for optional parameters. ```php public function doSomething($skin_id, $cape_id = null) { // } ``` -------------------------------- ### Using View and Redirect Routes (v4+) Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/route.md From Blessing Skin v4 onwards, you can directly use 'view' to render a Blade view or 'redirect' to a specified URL without needing a controller method. ```php Hook::addRoute(function ($routes) { // Example for view (assuming 'my-view' exists) $routes->view('some/path', 'my-view'); // Example for redirect $routes->redirect('old/path', '/new/path'); }); ``` -------------------------------- ### Add Description to Text Input Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/form.md Use the description method to add a descriptive text below the text input control. HTML content is allowed and will not be escaped. ```php $form->text('t', '文本框')->description('这是一行描述'); ``` -------------------------------- ### Twig 视图中调用 PHP 多语言 Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/i18n.md 演示了如何在 Twig 视图文件中使用 `trans` 函数调用 PHP 的多语言文本,包括带占位符的调用。 ```twig {{ trans('user.baby.info.content.footer') }} ``` ```twig {{ trans('user.baby.info.content.header', {name: username}) }} ``` -------------------------------- ### Declare Any Blessing Skin Version Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/dependency.md Use '*' to indicate that your plugin is compatible with any version of Blessing Skin. This is suitable for simple plugins without specific version-dependent features. ```json { "require": { "blessing-skin-server": "*" } } ``` -------------------------------- ### JavaScript v5 中调用前端多语言 Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/i18n.md 演示了在 JavaScript v5 中如何调用 `front-end.yml` 中的多语言文本,通过插件名作为命名空间。 ```javascript trans('test.name') // ==> kumiko ``` -------------------------------- ### Extracting Parameters from URI Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/route.md Define routes with dynamic parameters enclosed in curly braces. Optional parameters can be marked with a question mark. ```php Hook::addRoute(function ($routes) { $routes->any('player/{skin_id}/{cape_id?}', 'FoobarController@doSomething'); }); ``` -------------------------------- ### Applying Role Middleware (v5.0.0+) Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/route.md Use the 'role' middleware to restrict access based on user roles. Specify the required role level (e.g., 'admin', 'super-admin') as a parameter. ```php Route::middleware('role:admin')-> //... (within a route definition) Route::middleware('role:super-admin')-> //... (within a route definition) ``` -------------------------------- ### Find User by Primary Key Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/models.md Use the `find` static method to retrieve a User model instance by its primary key (uid). Returns null if the user does not exist. ```php $user = User::find(1); ``` -------------------------------- ### Accessing Plugin Instance in bootstrap.php Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/migration.md In BS v5, the plugin instance is directly available as the '$plugin' parameter in the bootstrap.php closure. This allows direct access to plugin methods like 'assets'. ```php return function ($plugin) { // $plugin is the instance of this plugin. // You can get the frontend resource URL like this: $plugin->assets('something.css'); }; ``` -------------------------------- ### Set Initial Value for Text Input Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/form.md Use the value method to pre-set the value of a text input control. This overrides default or saved values. ```php $form->text('t', '文本框')->value('预先设置的值'); ``` -------------------------------- ### JavaScript v4 语言文件结构变化 Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/i18n.md 展示了 Blessing Skin v4 中 JavaScript 多语言文件的结构变化,不再需要声明语种,直接使用 `blessing.i18n`。 ```diff - $.locales['zh_CN'].hello = { + blessing.i18n.hello = { title: '欢迎访问', content: { header: '您好,:name', footer: '没有了', body: '这是' + '多行' + '文本' } } ``` -------------------------------- ### Declare Multiple Blessing Skin Version Ranges Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/dependency.md Specify compatibility with distinct version ranges, such as '^3.2.0 || ^4.0.0'. This allows for more granular control over supported Blessing Skin versions compared to a wildcard. ```json { "require": { "blessing-skin-server": "^3.2.0 || ^4.0.0" } } ``` -------------------------------- ### 发送通知 Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/bootstrap.md 使用 Hook::sendNotification 向指定用户发送通知。通知将显示在页面右上方。可以传入单个用户实例或用户列表。 ```php Hook::sendNotification($users, $title, $content); ``` -------------------------------- ### Composer Dependency Management for Plugins Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/migration.md Plugins can now use Composer for dependency management. Package the 'vendor' directory with your plugin when distributing it. Ensure you specify the required Blessing Skin version in package.json. ```json { "require": { "blessing-skin": "^3.5.0" } } ``` -------------------------------- ### Form Lifecycle Hook: Before Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/form.md Execute a callback function before the form processes a POST request. The callback receives the form instance as an argument. ```php $form->before(function ($form) { // $form parameter is the form instance }); ``` -------------------------------- ### Route Definition with Closure (Not Recommended for Caching) Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/route.md Using closures directly in route definitions is not recommended as Laravel cannot cache them, potentially impacting performance. ```php Hook::addRoute(function ($routes) { $routes->any('foo/bar', function () { // Not recommended for caching }); }); ``` -------------------------------- ### Add Text Input with Placeholder Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/form.md Add a text input field with a placeholder. The placeholder text is provided as an argument to the placeholder method. ```php $form->text('my_text', '文本输入框')->placeholder('占位符'); ``` -------------------------------- ### JavaScript v3 语言文件示例 Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/i18n.md 展示了 Blessing Skin v3 中 JavaScript 的多语言文件结构,使用 JS 对象存储语言文本,并支持命名空间和占位符。 ```javascript $.locales['zh_CN'].hello = { title: '欢迎访问', content: { header: '您好,:name', footer: '没有了', body: '这是' + '多行' + '文本' } } ``` -------------------------------- ### Registering an Event Subscriber Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/backend-event.md After creating an event subscriber class, register it in your plugin's bootstrap.php using the `$events->subscribe()` method. ```php $events->subscribe(Example\RenderSubscriber::class); ``` -------------------------------- ### Create a Text Box Group Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/form.md Use the group method to create a labeled group for text input fields. Text and addon methods can be used in any quantity. ```php $form->group('my_group', '文本框组') ->text('input1') ->addon('至') ->text('input2'); ``` -------------------------------- ### Find User by Email Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/models.md Use the `where` and `first` static methods to find a single User model instance by email address. This is efficient for unique fields. ```php $user = User::where('email', 'a@b.c')->first(); ``` -------------------------------- ### Check if User Collection is Empty Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/models.md Use the `isEmpty` method on a collection of User models to check if it contains any results. ```php $users->isEmpty(); ``` -------------------------------- ### Specify PHP Version Requirement Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/dependency.md Declare the minimum required PHP version for your plugin. If the user's PHP version is lower than the specified requirement (e.g., '^7.4'), the plugin cannot be enabled. ```json { "require": { "php": "^7.4" } } ``` -------------------------------- ### Form Lifecycle Hook: Always Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/form.md Execute a callback function before the form is rendered. The callback receives the form instance as an argument. ```php $form->always(function ($form) { // $form parameter is the form instance }); ``` -------------------------------- ### Handle Form Submission Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/form.md Call the handle method on the form instance to enable it to receive POST requests and process data changes. ```php $form->handle(); ``` -------------------------------- ### Update Class Imports Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/migration.md In BS v5, class imports have been updated. Use fully qualified namespace imports instead of relying on implicit imports. ```php use Arr; use Str; ``` ```php use Illuminate\Support\Arr; use Illuminate\Support\Str; ``` -------------------------------- ### Format Text Input Data Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/form.md Use the format method to process user input data with a callback function before saving it to the database. The callback receives the specific field's data. ```php $form->text('t', '文本框') ->format(function ($data) { // 这里的 $data 参数就是这个表单项的值,而不是整个表单的数据。 }); ``` -------------------------------- ### Creating an Event Subscriber Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/backend-event.md Event subscribers allow you to group multiple event listeners within a single class. Implement the `subscribe` method to define which events your subscriber handles. ```php class RenderSubscriber { public function onRenderingHeader($event) { // } public function onRenderingFooter($event) { // } public function subscribe($events) { // 假设我们的插件的命名空间是 `Example` $events->listen( 'App\Events\RenderingHeader', 'Example\RenderSubscriber@onRenderingHeader' ); $events->listen( 'App\Events\RenderingFooter', 'Example\RenderSubscriber@onRenderingFooter' ); } } ``` -------------------------------- ### Add Hint to Text Input Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/form.md Use the hint method to add a question mark icon next to the text input's label. Hovering over the icon displays a tooltip. ```php $form->text('t', '文本框')->hint('您可能不知道……'); ``` -------------------------------- ### User Authentication in BS v4 Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/migration.md BS v4 refactored the authentication system using Laravel's built-in features. Use the 'auth' facade to access user information. ```php auth()->id(); // Get the current user's UID auth()->user(); // Get the current user's model instance auth()->check(); // Check if the user is logged in (consider using 'auth' middleware) ``` -------------------------------- ### Verify User Password Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/models.md Use the `verifyPassword` method with the raw password string to check if it matches the hashed password stored in the User model. ```php // Example usage: $user->verifyPassword('rawPassword123'); ``` -------------------------------- ### Frontend i18n Configuration Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/migration.md Configure frontend internationalization by adding a property to the blessing.i18n.configGenerator object. This is used for multi-language support in plugins. ```javascript blessing.i18n.configGenerator = { key1: 'text1' } ``` -------------------------------- ### JavaScript v3 中调用多语言 Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/i18n.md 演示了在 JavaScript v3 中如何使用 `trans` 函数调用多语言文本,包括传递占位符数据。 ```javascript trans('hello.content.header', { name: userName }) ``` -------------------------------- ### Defining a Route Group with Shared Properties Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/route.md Use route groups to apply common attributes like middleware, URI prefixes, and controller namespaces to a set of routes. ```php Hook::addRoute(function ($routes) { $routes->group([ 'middleware' => ['web'], 'prefix' => 'hello', 'namespace' => 'Example\Foo\Bar' ], function ($route) { $route->get('world', 'ExampleController@example'); }); }); ``` -------------------------------- ### Pass Single Data Item to View Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/view.md Use the 'with' method to pass a single key-value pair to the view. This method can be chained to the 'view' function. ```php return view('Author\MyPlugin::hello.world')->with('key', $value); ``` -------------------------------- ### Add Textarea with Rows Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/form.md Specify the number of rows for a textarea input. The number of rows is provided as an argument to the rows method. ```php $form->textarea('my_textarea', '文本域')->rows(50); ``` -------------------------------- ### Registering Routes in bootstrap.php Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/route.md All custom routes must be registered within the closure provided to Hook::addRoute in your plugin's bootstrap.php file. ```php use App\Services\Hook; return function () { Hook::addRoute(function ($routes) { // Register routes here }); }; ``` -------------------------------- ### Form Lifecycle Hook: After Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/form.md Execute a callback function after the form has processed a POST request. The callback receives the form instance as an argument. ```php $form->after(function ($form) { // $form parameter is the form instance }); ``` -------------------------------- ### Pass Multiple Data Items to View Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/view.md Pass an associative array as the second argument to the 'view' function to provide multiple data items to the view. This is a more concise way to pass several variables compared to chaining multiple 'with' calls. ```php return view('Author\MyPlugin::hello.world', ['name' => 'Secret', 'phone' => 2333]); ``` -------------------------------- ### Render Form Without Table Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/form.md Optionally, render the form without using a table for layout. This is generally not recommended. ```php $form->renderWithoutTable(); ``` -------------------------------- ### 监听 fetchError 事件处理请求错误 Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/fetch.md 当使用 Fetch API 发生错误时(网络故障或非 2xx 状态码),会触发 fetchError 事件。此示例展示了如何在页面上提示用户数据暂时无法获取。 ```javascript blessing.event.on('fetchError', error => { $('#somewhere').text('数据暂时无法获取') }) ``` -------------------------------- ### Add Select Dropdown Control Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/form.md Add a select dropdown to the form. Options are added using the 'option' method, which takes the value and display text as arguments. ```php $form->select('my_select', '下拉框') ->option('option1', '选项1') ->option('option2', '选项2'); ``` -------------------------------- ### Declare Blessing Skin Version Requirement Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/dependency.md Specify the minimum required version of Blessing Skin for your plugin. Using '^3.2.0' ensures compatibility with versions 3.2.0 and above, but not v4 or higher. For broader compatibility, consider '*'. ```json { "require": { "blessing-skin-server": "^3.2.0" } } ``` -------------------------------- ### 服务提供者示例 Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/information.md 定义了两个服务提供者,一个使用全称,另一个则根据命名空间自动补全。适用于加载自定义的服务提供者。 ```json { "namespace": "Example", "enchants": { "providers": [ "Example\\MyServiceProvider", "Our" ] } } ``` -------------------------------- ### Add Info Message to Form Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/form.md Add an informational message to the top of the form. The default message type is 'info'. ```php $form->addMessage('写文档不容易啊'); ``` -------------------------------- ### Add Checkbox with Label Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/form.md Add a checkbox with an additional label displayed next to it. The label text is provided as an argument to the label method. ```php $form->checkbox('my_checkbox', '复选框')->label('标签'); ``` -------------------------------- ### Adding Content to Page Header Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/backend-event.md The `RenderingHeader` event allows you to add content, such as CSS styles or meta tags, to the page's header before the main content is rendered. ```php $event->addContent(''); ``` -------------------------------- ### Set Texture Privacy Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/models.md Use the `setPrivacy` method on a Texture model instance to change its public visibility. Pass `true` for public and `false` for private. ```php $texture->setPrivacy(true); // 材质已被设置成公开 $texture->setPrivacy(false); // 材质已被设置成私有 ``` -------------------------------- ### 显示用户 badge Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/bootstrap.md 使用 Hook::addUserBadge 向用户中心和管理面板的用户信息面板添加一个 badge。可以自定义 badge 的文本和颜色。 ```php Hook::addUserBadge('text', 'green'); ``` -------------------------------- ### Adding Content to Page Footer Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/backend-event.md The `RenderingFooter` event provides an `addContent` method to inject HTML or script tags into the page's footer. ```php $event->addContent(''); ``` -------------------------------- ### Set Form Border Type Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/form.md Set the border style of the form using the 'type' method. Available types include 'info', 'success', 'warning', 'danger', and 'default'. ```php $form->type('info'); ``` ```php $form->type('success'); ``` ```php $form->type('warning'); ``` ```php $form->type('danger'); ``` ```php $form->type('default'); ``` -------------------------------- ### Add Textarea Control Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/form.md Add a textarea input field to the form. The first parameter is the input name, and the second is the display label. ```php $form->textarea('my_textarea', '文本域'); ``` -------------------------------- ### Declare Dependency on Another Plugin Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/dependency.md Specify a dependency on another plugin by its 'name' value and the required version range. If the dependent plugin is not enabled or does not meet the version requirements, the current plugin cannot be enabled. ```json { "require": { "b": "^1.0.0" } } ``` -------------------------------- ### Render Form HTML Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/form.md Render the form's HTML output. Use Laravel's {!! !!} syntax to prevent HTML escaping. This method should be called in your view. ```html {!! $form->render() !!} ``` -------------------------------- ### Check if User is Admin Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/models.md Use the `isAdmin` method on a User model instance to determine if the user has administrator privileges. This includes super administrators. ```php // Example usage would be within a conditional statement, e.g., if ($user->isAdmin()) { ... } ``` -------------------------------- ### Add Warning Message to Form Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/form.md Add a warning message to the top of the form. Other available types include 'success' and 'danger'. ```php $form->addMessage('写文档不容易啊', 'warning'); ``` -------------------------------- ### Add Text Input Control Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/form.md Add a text input field to the form. The first parameter is the input name (key in the options table), and the second is the display label. ```php $form->text('my_text', '文本输入框'); ``` -------------------------------- ### Twig Template Inheritance Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/view.md Extend a base template to maintain consistent styling across your plugin's pages. Use 'block' directives to define areas that can be overridden by child templates. Common base templates include 'user.base' and 'admin.master'. ```twig {% extends '要继承的母页面' %} {% block title %}页面标题{% endblock %} {% block content %} {% endblock %} ``` -------------------------------- ### Declare Plugin Conflicts in package.json Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/conflicts.md Use the 'conflicts' field under 'enchants' to specify plugin name and version conflicts. The value can be a semantic version range. ```json { "enchants": { "conflicts": { "a": "*", "b": "^3.0.0" } } } ``` -------------------------------- ### Check if User is Logged In Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/auth.md Use the `check` method to determine if a user is currently authenticated. This is useful for conditional logic within controllers or other parts of your application. ```php if (auth()->check()) { // 用户已登录 } ``` -------------------------------- ### JSON Helper Function Changes (v4.0.0 to v4.1.0) Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/migration.md The 'json' helper function's behavior has changed. The three-argument usage now nests the third argument under a 'data' key. ```json { "errno": 0, "msg": "" } ``` ```json { "code": 0, "message": "" } ``` ```php json('text', 0, ['abc' => 123]); ``` ```json { "errno": 0, "msg": "text", "abc": 123 } ``` ```json { "code": 0, "message": "text", "data": { "abc": 123 } } ``` -------------------------------- ### Return JSON Response Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/helpers.md Use the `json` function to return a JSON response. Remember to return the function's output from your controller method. It supports returning data, status codes, messages, and custom data payloads. ```php json(['a' => 1, 'b' => 2]); // {"a":1,"b":2} ``` ```php json('Hello.', 0, ['a' => 1]); // {"code":0,"message":"Hello.","data":{"a":1}} ``` ```php json('Success', 0); // {"code":0,"message":"Success"} ``` ```php json('Failed'); // {"code":1,"message":"Failed"} ``` -------------------------------- ### Render Input Tags Only Source: https://github.com/bs-community/blessing-skin-plugin-docs/blob/master/guide/form.md Render only the input fields of the form, omitting the labels. Useful for specific layout requirements. ```php $form->renderInputTagsOnly(); ```