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.

Why I continue to develop with PHP

I have no doubt that if you work in any realm of software engineering, or have at the very least spent enough time on Twitter, you’ll have encountered the seemingly endless cycles of arguments about what programming language, framework, or paradigm is best, and why everyone else is wrong.

PHP is no stranger to being a point of contention among different factions of web developers and is sometimes depicted as ancient, labouring technology. In reality, this is so far from the truth that it’s astonishing.

Why do people dislike it?

Some people can’t even give you an answer! It’s just fun to make fun of it. Though many do have genuine and understandable answers. A great deal of PHP code is written terribly, without conforming to the accepted standards that make working with the language, especially in corporate environments, significantly easier. Even common frameworks like CodeIgniter and the ubiquitous WordPress are guilty of this, hence the derision. Other codebases don’t make use of modern PHP functionality and suffer as a result.

A lot of other ridicule comes from lack of consistency in older function naming conventions and quirks of the language itself, but I really feel that this is grasping at straws at most, and such criticisms could be directed at other languages like JavaScript too, that aren’t flawless.

Node.js isn’t king, yet

Node.js, the JavaScript runtime powering many web applications is an insanely powerful tool with great frameworks to boot. It’s incredibly popular and is sometimes credited with being the future of all web development – JavaScript across the entire stack.

This, of course, doesn’t mean that PHP (and indeed other languages) are being forced out. Languages like PHP (and Python, Ruby, etc.) have diverse and mature ecosystems that power everything from simple blogs to large eCommerce monoliths. Let’s look at the statistics:

The PYPL index indicates that JavaScript sits at #3, and PHP shortly trails at #4, from this time of writing.

According to the TIOBE index, PHP continues to rise, sitting at around tenth place at the time of this writing.

Bare in mind, this includes usage of JavaScript across the entire stack, including front-end, where it remains an integral building block of the web.

The rise of Laravel… and WordPress

Backend Frameworks with the most stars on GitHub:

1. Laravel – 61.6K⭐️
2. Django – 52.1K⭐️
3. Flask – 52.1K⭐️
4. Spring Boot – 50.8K⭐️
5. Express – 50.2K⭐️
6. Ruby on Rails – 46.5K⭐️
7. Meteor – 42K⭐️
8. Nest – 30.5K⭐️
9. Koa – 30K⭐️

Which one is your favorite?— Marko ⚡ Denic (@denicmarko) September 26, 2020

The Laravel web framework, despite being relatively new, has skyrocketed its way to stardom on GitHub. This naturally isn’t representative of its usage, but its MVC, Rails-like architecture, command-line interface Artisan, database interactivity via object-relational mapping, templating engine, and testing suite for robust Test-Driven Development all out of the box, it’s a Swiss army knife that makes development not just easy, but immensely fast. I routinely make use of this framework, so I’m biased, but I highly recommend it.

For better, or for worse, WordPress continues to climb in usage. It’s easy to set up, its assortment of plugins and themes are unparalleled across all content management systems (CMS), and to its credit, has been working hard to rectify many of its historical security problems. The rise of JAMstack and static site generators have arguably made WordPress even more popular – by utilising it as an easy-to-use headless CMS that exposes its API to hydrate rapidly loading front-ends. Of course, if you’re looking to make money, then coding your own plugins and themes will remain profitable.

Okay I’m convinced! What now?

PHP is a fun and useful programming language to have in your arsenal, but don’t take my advice for it, go and explore the elephant ecosystem for yourself! I strongly recommend the following learning resources for getting on your way:

  • Codecademy is ideal for absolute beginners who want to learn language syntax without needing to install the language.
  • PHP The Right Way is something you should keep on hand at all stages of your career – it will remind you of the best ways to do things and common pitfalls to avoid. It will also guide you on how to install the language and package manager, Composer, on your operating system.
  • Laracasts is an incredible learning resource for both PHP and Laravel, with plenty of free learning paths for you to sample. I highly recommend a paid subscription if you’re serious about PHP. Great for Vue.js too.
  • Codecourse is another strong contender with plenty of free resources on PHP, Laravel, and other popular microframeworks.
  • PHP.net and of course, the language’s actual documentation.

Final thoughts

Ultimately, my tl;dr answer for why I develop using PHP in 2020 is because I like it. I grew up with it – and it remains and continues to mature as a respected and powerful tool on the Internet. This article’s purpose isn’t to discredit other languages, like JavaScript, but to reaffirm PHP’s place among them, and to serve as a reminder that it isn’t going anywhere anytime soon, regardless of how many paamayim nekudotayim jokes you make!