Actions and form responses

The response methods for [action] (action.md), [data form] (model-form.md), and [tool form] (widgets-form.md) are all the same set of methods.

You can get the Dcat\Admin\Http\JsonResponse object in the class by using $this->response() and respond to the data to the front end with the

  1. return $this->response()->success('success!');
  2. // equal to
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Http\JsonResponse;
  5. return JsonResponse::make()->success('success!');
  6. return Admin::make()->success('Success!') ;

If used in a controller, you need to add the send method

  1. public function index()
  2. {
  3. return JsonResponse::make()->success('成功!')->send();
  4. }

Function

The following describes the main Usages of JsonResponse.

showcasesuccess information

This method takes an argument of type string

  1. $this->response()->success('success!');

displays error messages

This method takes an argument of type string.

  1. $this->response()->error('出错了!');

Display warning information

This method takes an argument of type string.

  1. $this->response()->warning('警告');

Redirect

This method takes a parameter of type string and can be used with methods such as success, error, warning, etc.

  1. $this->response()->redirect('auth/users');

Redirect (location)

An automatic redirect ( non-partial refresh ) after 1 seconds, this method takes a string type parameter

  1. $this->response()->success('operation successful')->location('auth/users');

Refresh the current page if no parameters are passed

  1. $this->response()->success('operation successful')->location();

Refresh current page

This method can be used with methods such as success, error, warning etc.

  1. $this->response()->success('xxx')->refresh();

Download ####

This method takes an argument of type string.

  1. $this->response()->download('auth/users?_export_=1');

show confirmation pop-up

  1. // success
  2. $this->response()->alert(true)->success('...')->detail('Details');
  3. // error
  4. $this->response()->alert(true)->error('...')->detail('Details');
  5. // warning
  6. $this->response()->alert(true)->warning('...')->detail('Details');
  7. // info
  8. $this->response()->alert(true)->info('...')->detail('Details');

Return to HTML

This method accepts a string, Renderable, Htmlable type parameter and can be used with success, error, warning methods.

{tip} The HTML character will be placed on the action button element by default. If you want to control it, override the handleHtmlResponse method.

  1. $this->response()->html('<a>a tag</a>');
  2. $this->response()->html(view('...'));

Execute JS code

This method takes a parameter of type string and can be used with methods such as success, error, warning, etc.

  1. $this->response()->script(
  2. <<<JS
  3. console.log('response', response, target);
  4. JS
  5. );

Determine whether to call or not based on conditions

All of the above functional interfaces support the if mode, such as the

  1. // If the value of $condition is true, then the refresh method is called
  2. $this->response()->success(...)->ifRefresh($condition);
  3. $this->response()->success(...)->ifLocation($condition, 'auth/users');
  4. // $condition can also be a closure
  5. $this->response()->success(...)->ifRefresh(function () {
  6. return true;
  7. });