Help function

admin_redirect

Since v2.1.7-beta

Jump to the specified url, this function can be used instead of the redirect function to support pjax and ajax requests

  1. // no need to add the admin prefix
  2. return admin_redirect('auth/users');
  3. // If you want to jump to a link that is not prefixed with admin, you need to pass the full url link
  4. return admin_redirect(url('users'));

admin_exit

admin_exit is used to interrupt program execution and respond to data displayed to the browser, instead of exit and die, the following is a brief description of usage

Usage 1, return Content layout object, this usage can be used to return error messages to the front end

  1. use Dcat\Admin\Widgets\Alert;
  2. use Dcat\Admin\Layout\Content;
  3. // Interrupt the program and display a custom page to the front end
  4. admin_exit(
  5. Content::make()
  6. ->title('title')
  7. ->description('description')
  8. ->body('Page content 1')
  9. ->body(Alert::make('Server error~', 'Error')->danger())
  10. );

The effect is as follows

!

Usage 2, returning json formatted data, often used for api request interception for form submissions, or api request interception for Action

  1. use Dcat\Admin\Admin;
  2. admin_exit(
  3. Admin::json()
  4. ->success('succeeded')
  5. ->refresh()
  6. ->data([
  7. ...
  8. ])
  9. );
  10. // Of course you can also respond directly to the array
  11. admin_exit([
  12. ...
  13. ]);

Usage 3, direct corresponding Response object or string

  1. admin_exit('Hello world');
  2. admin_exit(response('Hello world', 500));

admin_color

Get the built-in color. For more information on the use of theme colors, please refer to the Theme - Colors section.

  1. // Three ways to get the theme color
  2. $primary = admin_color('primary');
  3. $primary = admin_color()->get('primary');
  4. $primary = admin_color()->primary();
  5. $color = admin_color();
  6. $color->lighten('primary', 10);

admin_js

You can include js files anywhere, see the static resources chapter for more information.

  1. admin_js(['@admin/xxx.js']);

admin_css

You can include css files anywhere, see the static resources chapter for more information.

  1. admin_css(['@admin/xxx.css']);

admin_require_assets

You can include static resource components anywhere, see the Static Resources chapter for more information.

  1. admin_require_assets(['@datime']);

admin_path

Get the application path where Dcat Admin is installed, the default directory is app/Admin:

  1. $bootstrap = admin_path('bootstrap.php');

admin_url

Get the full url of the route for the Dcat Admin application:

  1. // returns: http://localhost/admin/auth/users
  2. $url = admin_url('auth/users');

admin_route

Get URL by alias

The app/Admin/routes.php route is registered as follows

  1. Route::group([
  2. 'prefix' => config('admin.route.prefix'),
  3. 'namespace' => config('admin.route.namespace'),
  4. 'middleware' => config('admin.route.middleware'),
  5. ], function (Router $router) {
  6. // Set aliases
  7. $router->resource('users', 'UserController', [
  8. 'names' => ['index' => 'my-users'],
  9. ]);
  10. });

Get URL by alias

  1. // Get the url
  2. $url = admin_route('my-users');
  3. // Determine the route
  4. $isUsers = request()->routeIs(admin_route_name('users'));

admin_base_path

Get the routing path for the Dcat Admin application.

  1. // returns: /admin/auth/users
  2. $path = admin_base_path('auth/users');

admin_toastr

A toastr prompt pops up after the page is refreshed with the following parameters:

  • $message Tip window content
  • $type Prompt window type, default success, support for success, info, warning, error
  • $options toastr configuration parameters
  1. admin_alert('Updating Success', 'success');

admin_success

Displays a success message at the top of the page after a page refresh:

  1. admin_success('TITLE', 'success了');

admin_error

An error message is displayed at the top of the page after a page refresh:

  1. admin_error('TITLE', 'It failed.');

admin_warning

Displays a warning message at the top of the page after a page refresh:

  1. admin_warning('TITLE', 'warning');

admin_info

After a page refresh a message is displayed at the top of the page stating:

  1. admin_info('TITLE', 'content');

admin_asset

Full link to static resources:

{tip} This function supports aliases

  1. // Include css
  2. <link rel="stylesheet" href="{{ admin_asset("@admin/dcat-admin/main.min.css") }}">
  3. // Include js
  4. <script src="{{ admin_asset('@admin/dcat-admin/main.min.js')}}"></script>

admin_trans_field

To translate the current controller field, remove the Controller suffix from the controller name and convert it to a lowercase underscore, which is the name of the language package, e.g.: the controller name is UserProfileController, then the corresponding language package name is user-profile.php.

{tip} If the field translation does not exist in the language package corresponding to the current controller, it is looked up in the public translation file global.php

  1. $name = admin_trans_field('name');
  2. $createdAt = admin_trans_field('created_at');

The contents of the language package are as follows:

  1. return [
  2. 'fields' => [
  3. 'name' => '名称',
  4. 'created_at' => '创建时间',
  5. ],
  6. ];

admin_trans_label

To translate the current controller’s custom content, remove the Controller suffix from the controller name and convert it to a lowercase, middle-arrow language package name.

{tip} If the field translation does not exist in the language package corresponding to the current controller, it is looked up in the public translation file global.php.

  1. $user = admin_trans_label('User');

The contents of the language package are as follows:

  1. return [
  2. 'labels' => [
  3. 'User' => '管理员',
  4. ],
  5. ];

admin_trans_option

To translate the current controller’s field options, remove the Controller suffix from the controller name and convert it to a lowercase, middle-arrow language package name, e.g.: the controller name is UserProfileController, the corresponding language package name is user-profile.php.

{tip} If the field translation does not exist in the language package corresponding to the current controller, it is looked up in the public translation file global.php.

  1. $status = admin_trans_option(1, 'status');

The contents of the language package are as follows:

  1. return [
  2. 'options' => [
  3. 'status' => [
  4. 1 => '启用',
  5. 0 => '禁用'
  6. ],
  7. ],
  8. ];