How to build a documentation website using PHP and Docusaurus

王林
Release: 2023-06-19 20:32:01
Original
1289 people have browsed it

In the digital era, with the continuous innovation of various business systems and applications, users need not only manuals limited to operating functions, but also a comprehensive documentation website to guide and support their business. need. In response to this need, this article will share how to use PHP and Docusaurus to build a comprehensive documentation website.

1. Docusaurus

Docusaurus is an open source tool maintained and developed by the Facebook community. It is a static site generation tool. Based on React and Markdown tools, it can be used to help B2B companies and various WEB companies quickly build document websites, and can also be used to build personal sites.

Docusaurus offers built-in Markdown and a built-in search engine, customizable themes and plugins, and even customizable features through thousands of React and NPM packages. More importantly, it makes your documentation website highly maintainable and scalable.

2. PHP

PHP is a very popular server-side scripting language, widely used in the field of Web development. It is easy to get started, has low development cost, supports a large number of databases, and is popular among developers. The reader’s love. The generated files of Docusaurus can be dynamically generated through PHP or other server-side languages, so this article will focus on how to use PHP as middleware to build the ultimate documentation website.

3. How to use PHP and Docusaurus to build a documentation website

1. Install Docusaurus

First, you need to install Docusaurus locally and enter the project directory.

2. Initialize documents

In Docusaurus, documents are written in Markdown, and each document has a top-level title. Run the following command to generate Hello.md under /example-site/docs/:

cd /example-site/
npx @docusaurus/init init my-website classic
cd my-website
Copy after login

3. Create the PHP file

and then create a new index.php file under example-site/, The content is as follows:

<?php
$page = $_GET['page'];
if (file_exists('build/'.$page.'.html')) {
    // 如果文件存在,则返回文件内容
    echo file_get_contents('build/'.$page.'.html');
} else {
    // 否则,返回默认内容
    echo file_get_contents('default.html');
}
?>
Copy after login

4. Create the default.html file

Create a new default.html file under example-site with the following content:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Copy after login

The above code is used The default text returned when a 404 error is displayed. If the visited page is not generated, Hello World is returned by default.

4. Start the PHP server

Go to the example-site directory, start the PHP server, and run the following command:

php -S localhost:8000 index.php
Copy after login

5. Generate documents

After using Docusaurus to generate documents, copy the generated files in the build/static/ directory to example-site/build/.

6. Completion

At this point, the document has been successfully generated and deployed through the PHP server. You can enter http://localhost:8000/?page=Hello in the browser to view the Hello.md file you just wrote.

4. Summary

Using Docusaurus and PHP to build a documentation website is very easy to operate, making your documentation website go online quickly, which can help your users quickly solve problems and improve their user experience. I hope that by sharing this article, you can try to build a documentation website that is lightweight, easy to use, but still scalable.

The above is the detailed content of How to build a documentation website using PHP and Docusaurus. 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