Static resource loading

Dcat Admin supports loading of js scripts on demand.

Changing the domain name of a static resource

Open the configuration file config/admin.php, find the parameter assets_server and change it; you can also add .env to the `.

  1. ADMIN_ASSETS_SERVER=http://xxx.com

Registration path alias

Open app/Admin/bootstrap.php and add the following code

  1. Admin::asset()->alias('@my-name1', 'assets/admin1');
  2. Admin::asset()->alias('@my-name2', 'assets/admin2');
  3. // Bulk registration is also possible.
  4. Admin::asset()->alias([
  5. '@my-name1' => 'assets/admin1',
  6. '@my-name2' => 'assets/admin2',
  7. ]);

Use of aliases

  1. Admin::js('@my-name1/index.js');
  2. Admin::css('@my-name1/index.css');

Registration component

When a component has more js and css files, we can register these static resource files as a single component to make it easier to use. Open app/Admin/bootstrap.php, then add the following code

  1. Admin::asset()->alias('@editor-md', [
  2. 'js' => [
  3. // Support for using path aliases
  4. '@admin/dcat/plugins/editor-md/lib/raphael.min.js',
  5. '@admin/dcat/plugins/editor-md/lib/marked.min.js',
  6. '@admin/dcat/plugins/editor-md/lib/prettify.min.js',
  7. '@admin/dcat/plugins/editor-md/lib/jquery.flowchart.min.js',
  8. '@admin/dcat/plugins/editor-md/editormd.min.js',
  9. ],
  10. 'css' => [
  11. '@admin/dcat/plugins/editor-md/css/editormd.preview.min.css',
  12. '@admin/dcat/extra/markdown.css',
  13. ],
  14. ]);

usage

  1. Admin::requireAssets(['@editor-md']);

If you only need to load the js or css of this component and don’t want to load all the files, then you can use the following method

  1. // Load js files only
  2. Admin::js('@editor-md');
  3. // Load only css files.
  4. Admin::css('@editor-md');

Using Dynamic Parameters

  1. use Dcat\Admin\Admin;
  2. // Register front-end component aliases
  3. // {lang} is a dynamic parameter
  4. Admin::asset()->alias('@test', [
  5. 'js' => ['/vendor/test/js/{lang}.min.js'],
  6. ]);
  7. // {lang} will be replaced with zh_CN
  8. Admin::requireAssets('@test', ['lang' => 'zh_CN']);
  9. // It can also be used like this
  10. Admin::requireAssets('@test?lang=zh_CN');

Loading js scripts

The Admin::js method can introduce the js script, using the following:

  1. class UserController extend Controller
  2. {
  3. public function index()
  4. {
  5. Admin::js('/assets/js/index.js');
  6. Admin::js([
  7. '/assets/js/index2.js'
  8. ]);
  9. }
  10. }

Loading css scripts

The Admin::css method can introduce the css script, using the following:

  1. class UserController extend Controller
  2. {
  3. public function index()
  4. {
  5. Admin::css('/assets/css/index.css');
  6. Admin::css([
  7. '/assets/css/index2.css'
  8. ]);
  9. }
  10. }

Adding js code dynamically

The Admin::script method can dynamically add js code, using the following:

  1. public function index()
  2. {
  3. Admin::script(
  4. <<<JS
  5. console.log('Hello world!');
  6. JS
  7. );
  8. }

Adding css code dynamically

The Admin::style method can dynamically add css code, using the following:

  1. public function index()
  2. {
  3. Admin::style(
  4. <<<CSS
  5. body {
  6. color: #333;
  7. }
  8. CSS
  9. );
  10. }

Introducing static resources into a template

Manual introduction of static resources into a template requires the admin_asset function:

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

Adding js code to a template

The js code to be added to the template needs to be executed within the Dcat.ready method to ensure that your js code executes after all js scripts have been loaded:

  1. <script>
  2. Dcat.ready(function () {
  3. console.log('All the js are loaded');
  4. });
  5. </script>