Required reading before development

Please turn on debug mode for development environment.

Dcat Admin provides some development tools (such as code generator) that need to be in debug mode to use. It is recommended that developers turn on debug mode in their development environment and set APP_DEBUG to true in the .env configuration file.

Introduce JS scripts on demand

Dcat Admin uses jquery-pjax to build refresh-free pages (single-page applications) and supports loading JS scripts on-demand, allowing JS scripts to be introduced in any page method (except template files) and each page to load only the js scripts needed for the current page.

Example:

To write a custom page, this page component needs to introduce some front-end static resource files

{tip} The Dcat Admin build is a single page application, and the JS script that is loaded will only be executed once, so the initialization operation cannot be placed directly in the JS script, it should be loaded using the Admin::script method.

  1. <?php
  2. use Illuminate\Contracts\Support\Renderable;
  3. use Dcat\Admin\Admin;
  4. class Card implements Renderable
  5. {
  6. public static $js = [
  7. // The js script cannot contain the initialization operation directly, otherwise the page refresh is invalid
  8. 'xxx/js/card.min.js',
  9. ];
  10. public static $css = [
  11. 'xxx/css/card.min.css',
  12. ];
  13. public function script()
  14. {
  15. return <<<JS
  16. console.log('All JS scripts are loaded.');
  17. // Initialization operations
  18. $('xxx').card();
  19. JS;
  20. }
  21. public function render()
  22. {
  23. // This is where you can bring in your js or css files
  24. Admin::js(static::$js);
  25. Admin::css(static::$css);
  26. // JS code that needs to be executed on the page, such as initialization code.
  27. // The JS code set by Admin::script will be executed automatically when all JS scripts are loaded.
  28. Admin::script($this->script());
  29. return view('...')->render();
  30. }
  31. }

Using this component in the controller

  1. use Dcat\Admin\Layout\Content;
  2. use Card;
  3. class HomeController
  4. {
  5. public function index(Content $content)
  6. {
  7. // Use the card component above.
  8. // Static files required by the Card component will only be loaded on the current request.
  9. // Other requests will not load
  10. return $content->body(new Card());
  11. }
  12. }

Adding JS code to the page

Due to the addition of the ability to load JS scripts on demand, the JS code added in this project must use the Dcat.ready method to listen to the JS script load completion event, in which the JS code will be executed after all JS scripts have been loaded.

The JS code added using the Dcat\Admin\Admin::script method is automatically placed inside the Dcat.ready method for execution.

  1. <?php
  2. use Dcat\Admin\Admin;
  3. use Dcat\Admin\Layout\Content;
  4. class UserController
  5. {
  6. public function index(Content $content)
  7. {
  8. Admin::script(
  9. <<<JS
  10. (function () {
  11. // If there is a need to define local variables, it is better to put them inside an anonymous function to prevent variable contamination.
  12. var name = 'test';
  13. console.log('All JS scripts are loaded.~~', name)
  14. })()
  15. JS
  16. );
  17. return $content->header(...)->body(...);
  18. }
  19. }

If you are adding JS code to a template file, you need to execute the code in Dcat.ready

  1. <script>
  2. // Replace $() with Dcat.ready()
  3. // This method will be executed after all js scripts are loaded
  4. Dcat.ready(function () {
  5. // Writing your js code
  6. console.log('All js scripts are loaded!~~');
  7. });
  8. </script>

Page content and layout

{tip} The page content layout function is the cornerstone of Dcat Admin, mastering this function of the Usages, you can easily use Dcat Admin to build pages or extend functionality, so please read carefully.

The layout of Dcat Admin can be referred to the index() method of the app/Admin/Controllers/HomeController.php layout file on the backend home page.

The Dcat\Admin\Layout\Content class is used to implement the layout of the content area. The Content::body($content) method is used to add page content.

A simple backstage page code would be as follows:

  1. use Dcat\Admin\Layout\Content;
  2. public function index(Content $content)
  3. {
  4. // optional filling
  5. $content->header('Fill page header TITLE');
  6. // optional filling
  7. $content->description('Fill page descriptions with small TITLE');
  8. // Adding breadcrumb navigation
  9. $content->breadcrumb(
  10. ['text' => 'first page', 'url' => '/admin'],
  11. ['text' => 'user management', 'url' => '/admin/users'],
  12. ['text' => 'Edit User']
  13. );
  14. // Fill the body part of the page with any objects that can be rendered.
  15. return $content->body('hello world');
  16. }

The $content->body() method is an alias of $content->row(), which can accept any stringable object as arguments, including strings, numbers, objects with the __toString method, and implements the Renderable and Htmlable interfaces. objects, including laravel views.

Layout

The layout of dcat-admin uses a bootstrap grid system with each line being 12 in length, and the following are a few simple Examples:

Add a line of content:

  1. $content->row('hello')
  2. ---------------------------------
  3. |hello |
  4. | |
  5. | |
  6. | |
  7. | |
  8. | |
  9. ---------------------------------

Add multiple columns within rows:

  1. $content->row(function(Row $row) {
  2. $row->column(4, 'foo');
  3. $row->column(4, 'bar');
  4. $row->column(4, 'baz');
  5. });
  6. ----------------------------------
  7. |foo |bar |baz |
  8. | | | |
  9. | | | |
  10. | | | |
  11. | | | |
  12. | | | |
  13. ----------------------------------
  14. $content->row(function(Row $row) {
  15. $row->column(4, 'foo');
  16. $row->column(8, 'bar');
  17. });
  18. ----------------------------------
  19. |foo |bar |
  20. | | |
  21. | | |
  22. | | |
  23. | | |
  24. | | |
  25. ----------------------------------

Adding rows to columns:

  1. $content->row(function (Row $row) {
  2. $row->column(4, 'xxx');
  3. $row->column(8, function (Column $column) {
  4. $column->row('111');
  5. $column->row('222');
  6. $column->row('333');
  7. });
  8. });
  9. ----------------------------------
  10. |xxx |111 |
  11. | |---------------------|
  12. | |222 |
  13. | |---------------------|
  14. | |333 |
  15. | | |
  16. ----------------------------------

Add rows to columns, and columns within rows.:

  1. $content->row(function (Row $row) {
  2. $row->column(4, 'xxx');
  3. $row->column(8, function (Column $column) {
  4. $column->row('111');
  5. $column->row('222');
  6. $column->row(function(Row $row) {
  7. $row->column(6, '444');
  8. $row->column(6, '555');
  9. });
  10. });
  11. });
  12. ----------------------------------
  13. |xxx |111 |
  14. | |---------------------|
  15. | |222 |
  16. | |---------------------|
  17. | |444 |555 |
  18. | | | |
  19. ----------------------------------

Equal Width Layout

When the column width is set to 0, the equal-width layout is used

  1. use Dcat\Admin\Layout\Row;
  2. use Dcat\Admin\Layout\Content;
  3. return Content::make()
  4. ->body(function (Row $row) {
  5. $row->column(0, 'foo');
  6. $row->column(0, 'bar');
  7. $row->column(0, 'baz');
  8. });
  9. ----------------------------------
  10. |foo |bar |baz |
  11. | | | |
  12. | | | |
  13. | | | |
  14. | | | |
  15. | | | |
  16. ----------------------------------

no-gutters

.row with a margin-left: -15px;margin-right: -15px; attribute, you can eliminate this attribute by defining the .no-gutters attribute on .row so that the page is not an extra 30px wide, i.e. <div class="row no- gutters"...

  1. $content->row(function (Row $row) {
  2. // Enable no-gutters
  3. $row->noGutters();
  4. $row->column(9, function (Column $column) {
  5. $column->row($this->card(['col-md-12', 20], '#4DB6AC'));
  6. $column->row(function (Row $row) {
  7. // Enable no-gutters
  8. $row->noGutters();
  9. $row->column(4, $this->card(['col-md-4', 30], '#80CBC4'));
  10. $row->column(4, $this->card(['col-md-4', 30], '#4DB6AC'));
  11. $row->column(4, function (Column $column) {
  12. $column->row(function (Row $row) {
  13. // Enable no-gutters
  14. $row->noGutters();
  15. $row->column(6, $this->card(['col-md-6', 30], '#26A69A'));
  16. $row->column(6, $this->card(['col-md-6', 30], '#26A69A'));
  17. });
  18. });
  19. });
  20. });
  21. });

The result is as follows

Required reading before development - 图1

Build a page without a menu bar (full)

The page built in the above way defaults to a page with a left menu bar and a top navigation bar. But sometimes we’ll need to build a full page without a menu bar and top navigation bar, such as a landing page, or a page that needs to be loaded in IFRAME, and so on.

The page is rendered without menu bar and top navigation bar, and all the features and components of Dcat Admin can be used, which can significantly improve the efficiency.

The following is an implementation of the login page that demonstrates this function of the Usage

controller

  1. use Dcat\Admin\Layout\Content;
  2. class AuthController extends Controller
  3. {
  4. public function getLogin(Content $content)
  5. {
  6. if ($this->guard()->check()) {
  7. return redirect($this->redirectPath());
  8. }
  9. // Building landing pages using the full method
  10. return $content->full()->body(view($this->view));
  11. }
  12. ...
  13. }

The following is the login function of the template content, because the controller uses the Content::full method to build the page, so there is no need to write head in the template, and do not care what static resources are introduced, just write the HTML of the current page can be, and can also use the Dcat Admin in all the functions, such as the following form submission function used.

  1. <style>
  2. html body {background: #fff;}
  3. </style>
  4. <link rel="stylesheet" href="{{ admin_asset('@admin/css/pages/authentication.css') }}">
  5. <section class="row flexbox-container">
  6. <!-- Here is the HTML code for your landing page -->
  7. ...
  8. </section>
  9. <script>
  10. Dcat.ready(function () {
  11. // ajax form submission
  12. $('#login-form').form({
  13. validate: true,
  14. });
  15. });
  16. </script>

This landing page uses the ajax form submission function, and comes with a button loading result, which is better than the original landing page.

Event

The following two events are triggered when the Dcat\Admin\Layout\Content class is instantiated and when the render() method is called, and the developer can change or add some behavior in both events.

instantiate (resolve)

The callback function set through the Content::resolving method is triggered when the Dcat\Admin\Layout\Content class is instantiated.

  1. use Dcat\Admin\Layout\Content;
  2. Content::resolving(function (Content $content) {
  3. $content->view('app.admin.content');
  4. });

building pages (composing)

The callback function set by the Content::composing method is triggered when the Dcat\Admin\Layout\Content::render method is called.

  1. use Dcat\Admin\Layout\Content;
  2. Content::composing(function (Content $content) {
  3. $content->view('app.admin.content');
  4. });

build is complete (composed)

The callback function set by the Content::composed method is triggered when all the content set by the Content::row or Content::body method has been constructed.

  1. use Dcat\Admin\Layout\Content;
  2. class IndexController
  3. {
  4. public function index(Content $content)
  5. {
  6. Content::composed(function (Content $content) {
  7. // Grid has executed the render method.
  8. });
  9. return $content->body(function ($row) {
  10. $grid = new Grid(...);
  11. ...
  12. $row->column(12, $grid);
  13. });
  14. }
  15. }

AdminController

Through the above page layout of the relevant content of the learning, we understand the Dcat Admin page composition of the construction of the method. So how does an add, delete, and check feature actually work? We can see an add/drop controller code generated by the code generator as follows

  1. use App\Admin\Repositories\User;
  2. use Dcat\Admin\Form;
  3. use Dcat\Admin\Grid;
  4. use Dcat\Admin\Show;
  5. use Dcat\Admin\Http\Controllers\AdminController;
  6. class UserController extends AdminController
  7. {
  8. // Data table
  9. protected function grid()
  10. {
  11. return Grid::make(new User(), function (Grid $grid) {
  12. ...
  13. });
  14. }
  15. // Data details
  16. protected function detail($id)
  17. {
  18. return Show::make($id, new User(), function (Show $show) {
  19. ...
  20. });
  21. }
  22. // Form
  23. protected function form()
  24. {
  25. return Form::make(new User(), function (Form $form) {
  26. ...
  27. });
  28. }
  29. }

The above code mainly contains grid, detail and form, from these codes, we do not have a way to change the layout of a page, so how is this page actually built? And how do we change the layout of the page? Let’s take a look at the AdminController

  1. <?php
  2. namespace Dcat\Admin\Controllers;
  3. use Dcat\Admin\Layout\Content;
  4. use Illuminate\Routing\Controller;
  5. class AdminController extends Controller
  6. {
  7. // Page TITLE
  8. protected $title;
  9. // Page Description Information
  10. protected $description = [
  11. // 'index' => 'Index',
  12. // 'show' => 'Show',
  13. // 'edit' => 'Edit',
  14. // 'create' => 'Create',
  15. ];
  16. // Specify the language pack name, the default corresponds to the current controller name
  17. protected $translation;
  18. // Back to page TITLE
  19. protected function title()
  20. {
  21. return $this->title ?: admin_trans_label();
  22. }
  23. // Back to description information
  24. protected function description()
  25. {
  26. return $this->description;
  27. }
  28. // list page
  29. public function index(Content $content)
  30. {
  31. return $content
  32. ->title($this->title())
  33. ->description($this->description()['index'] ?? trans('admin.list'))
  34. ->body($this->grid());
  35. }
  36. // detail page
  37. public function show($id, Content $content)
  38. {
  39. return $content
  40. ->title($this->title())
  41. ->description($this->description()['show'] ?? trans('admin.show'))
  42. ->body($this->detail($id));
  43. }
  44. // edit page
  45. public function edit($id, Content $content)
  46. {
  47. return $content
  48. ->title($this->title())
  49. ->description($this->description()['edit'] ?? trans('admin.edit'))
  50. ->body($this->form()->edit($id));
  51. }
  52. // create page
  53. public function create(Content $content)
  54. {
  55. return $content
  56. ->title($this->title())
  57. ->description($this->description()['create'] ?? trans('admin.create'))
  58. ->body($this->form());
  59. }
  60. // Modify interface
  61. public function update($id)
  62. {
  63. return $this->form()->update($id);
  64. }
  65. // New interface
  66. public function store()
  67. {
  68. return $this->form()->store();
  69. }
  70. // Delete/Batch Delete Interface
  71. public function destroy($id)
  72. {
  73. return $this->form()->destroy($id);
  74. }
  75. }

Is it now possible to understand the components of the entire page? In fact, a lot of code in the system are known by name, easy to understand, many times we only need to read the code to know the Usage. For example, if we want to change the page TITLE, by reading this code, we can know that it can be achieved by rewriting the title method or changing its entry in the translation file. Very simple, isn’t it?

Let’s demonstrate the practical application of changing the page layout by implementing a list page of data tables + statistics cards

  1. use App\Admin\Metrics\Examples\NewDevices;
  2. use App\Admin\Metrics\Examples\NewUsers;
  3. use App\Admin\Metrics\Examples\TotalUsers;
  4. use Dcat\Admin\Layout\Content;
  5. use Dcat\Admin\Layout\Row;
  6. public function index(Content $content)
  7. {
  8. return $content
  9. ->title($this->title())
  10. ->description($this->description()['index'] ?? trans('admin.list'))
  11. ->body(function (Row $row) {
  12. $row->column(4, new TotalUsers());
  13. $row->column(4, new NewUsers());
  14. $row->column(4, new NewDevices());
  15. })
  16. ->body($this->grid());
  17. }

The result is as follows

Required reading before development - 图2

Bootstrap4公共样式

Dcat Admin uses the bootstrap4 grid system for page layout, which is both simple and powerful, and you need to know it before you start development! In addition, bootsrap4 provides a number of very useful public styles, which are very helpful for writing page components and can significantly improve development efficiency. It is recommended that you consult the documentation before writing components, and the following is a list of recommended styles to learn.

  • Grid layout
  • Display Properties With our display utility for quickly and efficiently toggling component display values and more, including support for some of the more common values, this list of styles is very helpful for responsive layouts.
  • flex layout The introduction of the new Flex Flexible Layout allows for quick management of the layout, alignment and size of grid columns, navigation, components, etc. through a set of responsive and flexible utilities. More complex presentation styles are also possible through the progressive definition of CSS.
  • Color There are a number of ways to define this, including support for linking, hovering, selecting, and other state-related style sets.
  • Float property Use our responsive float float universal style to toggle the float on any device breakpoint (browser size).
  • sizing Easily define the width or height of any element (relative to its parent) using the system width and height styles
  • spacing A variety of quick indent, isolate, and fill spacing tools are built in, responding to margin and fill utility classes to modify the appearance of elements.
  • text handling Used to control text alignment, grouping, word weight, and other Examples as well as to work with documents.
  • vertical alignment Easily change the vertical alignment of inline, inline block, inline table and table cell elements.

Built-in style

In addition to the previously mentioned `bootstrap4 public styles, the system also has the following common styles built in:

Colors

Please refer to color chart styles.

shadows

.shadow
.shadow-100
.shadow-200