Documentation in Laravel 8 with Enlighten

The importance of software documentation, even in an agile world, is undisputed. Keeping documentation, especially for APIs, up-to-date however can be an additional burden on a development team, and thus automatically generated documentation can help supplement this by producing reference materials. This is where Enlighten comes in.

In this guide, we’ll get Laravel Enlighten up and running, which utilises your existing PHPUnit tests (because you should be writing tests) to produce and publish beautiful, customisable documentation, without needing to run any additional commands.

System Requirements

Enlighten does require both PHP 7.4 and Laravel 8. I don’t know whether it works with Lumen. 

If you’re running anything earlier, such as Laravel 6 LTS, I would instead recommend Scribe. For other frameworks or vanilla PHP applications, try phpDocumentor.

You will also need a secondary database. It does not need to be a different kind of database, but it does need to be persistent.

You also need an up-to-date version of Git. This is unlikely to be a problem on macOS, Linux or Windows Subsystem for Linux, but it gave me some teething problems directly running on Windows 10 under Laragon.

Please also note that these are as simple installation instructions as possible. For troubleshooting and optional installation steps, please check the project’s repository.

Installation

Composer installation

Do not use sudo for any of these commands. That will open a whole Pandora’s box you don’t want to deal with.

Using Composer, require Enlighten into your Laravel project using the following command. If you somehow don’t have it, grab it here.

composer require styde/enlighten --dev

Registering the service provider

Next, add the following to your config/app.php file to register Enlighten’s service provider. If you’re not sure where this needs to go, put it underneath the package service providers comment, like thus:

[
  'providers' => [
    // ...
    /*
     * Package Service Providers...
     */
     Styde\Enlighten\Providers\EnlightenServiceProvider::class,
    // ...
  ]
];

Important: Be sure to end the line in a comma, not a semicolon.

Publish assets to the public directory

Run the following command to publish Enlighten’s assets to your public directory. Without this, any documents you generate will lack any CSS formatting or JavaScript.

php artisan vendor:publish --tag=enlighten-build

Integrating Enlighten into your tests

In order for Enlighten to activate during testing and to have access to test data, it will need to be imported and initialised during the setup phase of your tests. You can do this in your TestCase.php abstract class, or in individual test files.

For example:

<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Styde\Enlighten\Tests\EnlightenSetup; // import this trait

abstract class TestCase extends BaseTestCase
{
  use CreatesApplication;
  use EnlightenSetup; // and use said trait here

  protected function setUp(): void
  {
    parent::setUp();

    $this->setUpEnlighten(); // the final piece of the puzzle
  }
}

Important: I believe you need to still call parent::setUp() in your setUp() methods within your tests.

Setting up the secondary database

As aforementioned, Enlighten needs another database to record information. The easiest way to do this is to create another database with the same name of your existing one, except with an _enlighten suffix.

For example, if you have used a MySQL database named laravel, you can create another one called laravel_enlighten which will be used automatically. This assumes you’re using the same database connection, credentials, and driver.

Once you’ve made it, run your migrations to get it set up:

php artisan migrate

Optional: Custom database setup

If you need to use something else, add a new entry to your config/database.php file with the name enlighten. This can support a different driver, credentials, database name, etc. You can add this information as custom entries in your .env file – possibly with an _ENLIGHTEN suffix for clarity.

For example:

'enlighten' => [
  'driver' => 'mysql',
  'host' => env('DB_HOST_ENLIGHTEN', '127.0.0.1')
  'port' => env('DB_PORT_ENLIGHTEN', '3306')
  'database' => env('DB_DATABASE_ENLIGHTEN', 'laravel_enlighten')
  'username' => env('DB_USERNAME_ENLIGHTEN', 'root')
  'password' => env('DB_PASSWORD_ENLIGHTEN', '')
// …
],

I think that you could use an SQLite database in theory, but I haven’t tested this and generally stick to the defaults.

Don’t forget to run your migrations if you choose this route.

Running Enlighten

Once you’ve set everything up, Enlighten will run when you run your test suite:

php artisan test

You can then find your new documentation at the yourwebsite.test/enlighten/ URL.

What’s next?

This guide was designed to get Enlighten up and running as easily as possible. More customisation and configuration options are available to make your documentation as detailed and organised as possible. You can read more on the project’s repository.

I hope that you’ve found this guide useful and I encourage you to check it out with your Laravel API projects! It goes without saying, but Enlighten wasn’t written by me – it’s an open-source project headed by Duilio Palacios and Jeff Ochoa.

Thank you so much for reading.

PHP and Composer on the Windows Subsystem for Linux

In this guide, I’ll be showing you step-by-step what you need to do to install the Windows Subsystem for Linux, and then both PHP and the Composer package manager.

As a prerequisite, this requires an up-to-date version of Windows 10. Windows 8.1, 7, and below are unsupported. I will write about setting up PHP environments under legacy Windows versions in a later article.

Step One – Installing WSL

First, use your start menu or Cortana search bar to search for “windows features” – the first option provided will be a tool to add or remove features to your Windows installation. Select this.

Once the dialogue appears, scroll down until you can find Windows Subsystem for Linux. Tick the box and click OK. Windows will download and install this functionality, and may require a system restart afterwards.

Step Two – Installing a Linux distribution

After a restart, we can now choose a Linux distribution. Open the Microsoft Store app that you can find in your start menu. On the app, search for linux to bring up a list of available distributions.

You’re free to choose whatever distribution I want, but for a beginner, it’s my personal recommendation that you install Ubuntu as it’s well supported, easy-to-use, and does not require any complex configuration. Click on the one you want, and choose either Get or Install – depending on your system’s locale. Once it’s downloaded, the button’s text should change to Launch and Ubuntu will appear in your start menu. Fire it up.

Step Three – Installation and UNIX username

After opening the app, it will take a few minutes to install. This can take a while on older systems, but fret not, it will finish!

After a short while, it will prompt you to set a UNIX username. This should be a single word, all lowercase, with no spaces or symbols. My name is Oliver, so oliver is fine. Then set a password. This does not need to be the same as the password you use to log in to Windows. Don’t forget, your password won’t actually appear as asterisks as you type it, but it’s definitely there – it’s normal on these platforms.

Once that’s done, you’ll be presented with a command line and information regarding the system information.

Step Four – Installation of PHP

Before you install PHP, you might need to refresh Ubuntu’s repositories. I know this sounds complicated but think of it like Ubuntu checking an app store for what’s available. Type the following command to do this:

sudo apt update

It might prompt you that there are updates available, but you can take care of that later. Once the update is done, we can install PHP. Type the following command:

sudo apt install php

Sudo (short for superuser do) allows you to run commands as an administrator (root) – which is necessary for installing new software. Apt is Ubuntu’s package manager – necessary for pulling in said software. It will prompt you for the password you set earlier.

The package manager may be different if you installed a different Linux distribution. openSUSE for example uses Zypper instead.

Give the package manager some time to install and configure PHP and all of its prerequisites. Once it’s done, you can check that it’s fully installed using the following command:

php -v

This should print PHP’s version to the screen. As of this time of writing, Ubuntu installs PHP 7.4 by default. My output looks like this:

PHP 7.4.3 (cli) (built: May 26 2020 12:24:22) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

Installing Composer

All this talk of package managers, when yes, PHP has its own package manager! If you’re familiar with JavaScript and/or Node.js, you’ll have definitely encountered NPM or Yarn, which perform basically the same functionality.

PHP’s package manager is called Composer, and this can be easily installed at the command line in the same way you did PHP:

sudo apt install composer

Similarly, you can check the version and status of Composer using

composer -v

Your output will look similar to mine, along with a long list of commands and some ASCII art:

Composer 1.10.1 2020-03-13 20:34:27

Now Composer will be available at the command line for you to install global packages with, or to install local dependencies outlined in a project’s composer.json file.

Optional: Install the Laravel installer and add Composer binaries to PATH

First, install the Laravel installer using Composer:

composer global require laravel/installer

After this is installed, you should normally be able to type laravel to access its installer, but you should get an error. Why?

By default, any software you install globally via Composer won’t be instantly accessible from your command line, because Composer’s directories have not been added to your PATH. If you’re not sure what this is, don’t worry – in a nutshell, this is information that tells your terminal where it can find the programs that you type in (like apt or php) so they can be run. Windows has this too.

To fix this, you need to open your aliases file in a text editor. We’ll use Nano, but feel free to use something else:

nano ~/.bash_aliases

In this file, type:

export PATH=~/.config/composer/vendor/bin:$PATH

Press CTRL+O to save, and CTRL+X to close Nano. With this change in place, we need to reload Bash’s configuration. You only need to do this once.

source ~/.bashrc

Now, your Composer global applications will work. Type laravel to confirm this.

Conclusions

By following this guide, you should be able to install the WSL under Windows 10 with Ubuntu, and have a functioning installation of PHP and Composer ready to go. I hope that this helps.

Please leave your comments below and if you found this useful, please let me know!