Home > PHP Framework > ThinkPHP > A brief analysis of how to use ThinkPHP process message prompts

A brief analysis of how to use ThinkPHP process message prompts

PHPz
Release: 2023-04-13 18:49:31
Original
884 people have browsed it

ThinkPHP is a popular PHP development framework in China. Its development method is simple and efficient, and it provides many practical functions and tools. During the development process, it is often necessary to prompt users with information such as successful or failed operations. This article will introduce how to use ThinkPHP process message prompts.

1. Set prompt information in the controller

ThinkPHP provides two ways to set prompt information in the controller, using the success and error methods.

  1. success method

The success method is used to set the prompt information for successful operation. Specifically, it can be set as follows:

$this->success('操作成功');
Copy after login

This will be displayed on the page "Operation successful" message.

In addition to passing strings as prompt information, the success method can also pass arrays, so that more information can be passed, such as jump target addresses, etc.

$this->success('操作成功', 'index/index');
Copy after login

After setting like this, the prompt message "Operation Successful" will be displayed on the page, and it will automatically jump to the index operation of the index controller.

  1. error method

The error method is used to set the prompt information for operation failure. Specifically, it can be set as follows:

$this->error('操作失败');
Copy after login

This will be displayed on the page "Operation failed" message.

Similar to the success method, the error method can also pass array parameters in order to pass more information.

2. Display prompt information in the view file

When the prompt information is set in the controller, we generally need to display the information in the view file. ThinkPHP provides two ways to implement this function, using the success and error methods.

  1. success method

The prompt information set by using the success method in the controller can be output in the view file in the following ways:

<div class="alert alert-success"><?php echo $msg;?></div>
Copy after login

Among them, $msg is the prompt information set in the controller. This will display the prompt information on the page with a green background.

  1. error method

The prompt information set by using the error method in the controller can be output in the view file in the following ways:

<div class="alert alert-danger"><?php echo $error;?></div>
Copy after login

Similar to the success method, $error is the prompt information set in the controller. This will display the prompt information on the page with a red background.

3. Customize the prompt information style

If you need to customize the prompt information style, you can add the corresponding style rules in the CSS file. For example:

.alert {
    border: 1px solid #ddd;
    padding: 10px;
    margin-bottom: 15px;
}
.alert-success {
    background-color: #dff0d8;
}
.alert-danger {
    background-color: #f2dede;
}
Copy after login

Here, we define some basic styles for the alert class, and then set different background colors for success and failure prompts. This can make the prompt information more prominent.

4. Use Flash message prompts

In addition to the above methods, ThinkPHP also provides the function of Flash message prompts. Flash messages are temporary messages that can be shared between the current request and the next request.

You can implement Flash message prompts through the following steps:

  1. Set Flash message in the controller
$this->success('操作成功', '', ['type' => 'notice', 'bgColor' => '#ff9999']);
Copy after login
  1. Display Flash in the next request Message

In the next request, you can display the Flash message by:

use think\facade\Session;
use think\facade\View;

View::assign('flashMsgs', Session::flash());
Copy after login

Then, in the view, you can output the Flash message by:

<?php if(isset($flashMsgs[&#39;notice&#39;])): ?>
    <div class="alert alert-success" style="background-color:<?php echo $flashMsgs[&#39;notice&#39;][&#39;bgColor&#39;];?>"><?php echo $flashMsgs[&#39;notice&#39;][&#39;msg&#39;];?></div>
<?php endif;?>
Copy after login

This way the Flash message can be displayed on the next request.

In short, during the application process of prompt information, it is necessary to select an appropriate prompt method for the application scenario and customize the prompt information according to actual needs.

The above is the detailed content of A brief analysis of how to use ThinkPHP process message prompts. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template