Use ThinkPHP6 to implement a beautiful 404 page
Jun 20, 2023 am 11:06 AMWith the increasing development of the Internet, many websites or applications have gradually become more complex. When users use it, they often encounter error pages, the most common of which is the 404 page. The 404 page means that the page being accessed does not exist and is a common error page. For websites or applications, a beautiful 404 page can greatly improve the user experience. In this article, we will introduce how to use ThinkPHP6 to quickly implement a beautiful 404 page.
- Create route
First, we need to create an error.php file in the route folder, define a route in it, and point the error page to our new Error index method in the controller. The code is as follows:
<?php use thinkacadeRoute; Route::rule('/404', 'error/index')->name('404');
It should be noted that here we name the route 404 to facilitate calling later.
- Create controllers and views
Create a new Error.php file in the controller folder, the code is as follows:
<?php namespace appindexcontroller; use thinkController; class Error extends Controller { public function index() { return $this->fetch('error/404'); } }
Here we inherit the Controller class , and return the newly created 404 view in the index method.
Create a new error folder in the view folder and create the 404.html file in it. The code is as follows:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>404 Not Found</title> <style> body { background-color: #eee; text-align: center; font-family: 'Microsoft YaHei', sans-serif; } .container { margin-top: 10%; } h1 { font-size: 8em; color: #ddd; margin-bottom: 0; } p { font-size: 3em; color: #777; margin-top: 0; } a { text-decoration: none; color: #000; border: 1px solid #000; padding: 10px 20px; margin-top: 30px; display: inline-block; } a:hover { border-color: #777; } </style> </head> <body> <div class="container"> <h1>404</h1> <p>Page not found</p> <a href="<?php echo url('/') ?>">Go back home</a> </div> </body> </html>
Here we use a simple and beautiful design to display the theme of the 404 page and buttons.
- Test
We can access a non-existent route at will, for example, visit http://yourdomain.com/abc, and you can see our newly created one. Beautiful 404 page.
- Customize the 404 page
If you want to add more content or adjust the style of the 404 page, you only need to modify the view file. We can customize the 404 page according to actual needs, such as adding search functions, contact information, etc. At the same time, if you are worried about browser caching affecting the display effect, you can solve it by introducing CSS and JS files into the view page.
- Summary
In this article, we introduced how to use ThinkPHP6 to implement a beautiful 404 page. By creating routes, controllers, and views, we can easily build a 404 page that meets our needs. In actual development, the 404 page is an important user experience factor, and we should pay attention to its design and beautification.
The above is the detailed content of Use ThinkPHP6 to implement a beautiful 404 page. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Want to copy a page in Microsoft Word and keep the formatting intact? This is a smart idea because duplicating pages in Word can be a useful time-saving technique when you want to create multiple copies of a specific document layout or format. This guide will walk you through the step-by-step process of copying pages in Word, whether you are creating a template or copying a specific page in a document. These simple instructions are designed to help you easily recreate your page without having to start from scratch. Why copy pages in Microsoft Word? There are several reasons why copying pages in Word is very beneficial: When you have a document with a specific layout or format that you want to copy. Unlike recreating the entire page from scratch

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

Page refresh is very common in our daily network use. When we visit a web page, we sometimes encounter some problems, such as the web page not loading or displaying abnormally, etc. At this time, we usually choose to refresh the page to solve the problem, so how to refresh the page quickly? Let’s discuss the shortcut keys for page refresh. The page refresh shortcut key is a method to quickly refresh the current web page through keyboard operations. In different operating systems and browsers, the shortcut keys for page refresh may be different. Below we use the common W

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.
