How to add documentation to your Laravel app with Vuepress

How to add documentation to your Laravel app with Vuepress

Written by Supernova3339 on Apr 25th, 2023 Views Report Post

Developers spend most of their time writing code, debugging, and testing their programs to ensure that they function correctly. However, they often overlook the importance of creating and maintaining documentation for their work.

As a developer, documentation is an integral part of your work that can make a big difference not just for you but for your team, your clients, or your users. Proper documentation can save time, effort, and resources and can help you avoid common development pitfalls such as reinventing the wheel, repeating mistakes, and more.

Documentation can also help you ensure continuity in your work. When developers leave the team or move to other projects, documentation can help others easily understand and maintain the code, ensuring that the software product remains up-to-date and error-free.

In this blog post, we will explore the importance of documentation for developers and how using a static site generator like VuePress can make creating and maintaining documentation easier and more efficient. We will also showcase how VuePress can be specifically useful for Laravel web developers looking for an elegant and user-friendly way to deliver documentation for their web applications.

Laravel is a popular open-source PHP web application framework designed for building robust, scalable web applications. It provides a clean, elegant syntax and a wide range of features that make building web applications faster and easier. Laravel offers features such as a powerful ORM, routing system, and authentication system that are great for developing large scale web applications.

VuePress, on the other hand, is a static site generator powered by Vue.js. It is designed to help developers create well-designed, responsive documentation sites with ease. VuePress comes with built-in support for Markdown, which makes writing content easy and straightforward. VuePress is especially great for creating elegant and user-friendly documentation sites, which can complement complex web applications developed using Laravel.

Introduction

In this tutorial, we will be:

  • Learning the basics of VuePress
  • Integrating VuePress into our Laravel application
  • Deploying our Vuepress documentation to our Laravel application

The basics of VuePress

VuePress is a modern static site generator that allows you to quickly create documentation and websites with a clean and minimalistic design. It's built on top of Vue.js, which means that you can easily create custom components and interactions to enhance the user experience.

Installing VuePress is as simple as running the following command:

npm install -D vuepress

What this command will do is install VuePress as a dependency under your package.json.

Next, you'll add the following to your scripts:

"docs:dev": "vuepress dev docs",
"docs:build": "vuepress build docs"

Configuring VuePress

Configuring VuePress has been made simple. In this tutorial, we will be using the following in our configuration:

  • sidebar
  • prefix [CUSTOM]

VuePress renders your pages in markdown, so it's easy to pick up where you've left off, or write from scratch. Examples can include a GitHub repo, mkdocs, and even Gitbook!

To get started, we will be configuring our config.js file. In your Laravel application, create a docs directory. Once inside, create a .vuepress directory then create your config.js file. Once done, paste the following content:

module.exports = {
  base: '/docs/',
  dest: 'public/docs',
  title:'Your App Docs',
  description:'Your App official documentation',
  head: [
    ['link', { rel: 'icon', href: 'https://yourapp.domain/icon.ico/svg/png/jpg/jxl/etc' }],
]
],
  themeConfig: {
    nav: [
      { text: 'Home', link: 'https://yourapp.domain/', target: '_self' },
      // versions
      
    ],
        logo: 'https://yourapp.domain/yourappheaderlogo.svg/png/jpg/jpeg/jxl/etc',
        displayAllHeaders: true,
        activeHeaderLinks: false,
        searchPlaceholder: 'Press / to search',
        lastUpdated: false, // string | boolean
    sidebar: [
      {
        title: "Getting Started",
        collapsable: false,
        children: [
            'introduction',
            'installation',
            'configuration',
            'providers',
        ],
    },
     {
        title: "Features",
        collapsable: false,
        children: prefix('features', [
            'multiple-users',
            'two-factor-authentication',
            `cli`,
        ]),
    },
    ]
  }
}

function prefix(prefix, children) {
    return children.map(child => `${prefix}/${child}`)
}

This configuration file has:

  • Navigation
  • Prefix

Now, create introduction, installation, configuration, README and providers.md. In your README.md add the following content:

<meta http-equiv="refresh" content="0;url=/docs/introduction.html" />

This will redirect /docs to /docs/introduction.md. If you don't create a README.md, you will receive an error. README.md acts as your index.html, weather it's for your features or base.

If you want to add a table of contents to your page, use the [[toc]]. Pages are formatted like this:

# Introduction

[[toc]]

### Welcome to Your App
Thanks for using our application.

::tip Need help?
Chat with our support team at [support@devdojo.com](mailto:support@devdojo.com)
:::

How to use prefix

Prefix defines a bunch of markdown files in a new directory. Examples can include: features, kb, support, etc.

Adding VuePress to your Laravel application

Adding VuePress to your Laravel application is surprisingly easy. We will be:

  • Creating a DocsController
  • Adding to our routes/web.php
  • Clearing the route cache

Adding our DocsController

Now we will be creating our DocsController. Run the following in your terminal:

php artisan make:controller DocsController

Paste the following content into the new controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use File;

class DocsController extends Controller
{
    public function index(Request $request, $file = 'index')
    {
        if ($file != 'index') {
            $file = $file . '/index';
        }
        return File::get(public_path() . '/docs/' . $file . '.html');
    }
}

Adding our /docs route

Open your routes/web.php and add the following content:

use App\Http\Controllers\DocsController;

/*
|--------------------------------------------------------------------------
| Documentation Routes
|--------------------------------------------------------------------------
*/

Route::get('/docs/{file?}', [DocsController::class, 'index'])->name('docs.index');

Clearing the route cache

Now that you've added your route, run the following command in your terminal:

php artisan route:clear

This command will clear your route cache.

Deploying our Vuepress documentation to Laravel

Deployment has already been made easy. Remember that docs:build command you added earlier? In your terminal, run the following command:

npm run docs:build

This will build your VuePress application under /public/docs. If you would like a live example, please visit easyshortener.cloud.

Conclusion

In conclusion, documentation is an essential part of a developer's job, allowing for efficient communication within teams, satisfying clients, and improving software maintenance. By using VuePress, developers can quickly create user-friendly and elegant documentation to complement their web applications. With the steps presented in this tutorial, integrating VuePress into Laravel is painless, as well as the configuration process. VuePress is a great tool that should be considered by developers, and by creating SEO-friendly optimized pages on your app, we can get the most out of it.

TLDR

VuePress is a dynamic static site generator that streamlines the process of creating informative documentation. Plus, it even supports Laravel for seamless integration. Don't overlook the power of documentation and take advantage of VuePress today.

Comments (2)