Patrick LaRocheEngineering @ Manifold

Announcing Manifold’s Laravel Integration

Manifold Laravel Demo App

This demo app will show you how to automatically inject your Manifold resource credentials into your Laravel app using the manifold-laravel php package.

Most of this demo application is boilerplate and comes from this great intro laracast on Laravel auth and config set up, except that it pulls its configuration variables directly from your Manifold project (vs editing your .env file and keeping it up to date).

I’ll take you through the main three steps to recreate the code in this repo:

  1. Setting a demo Laravel application

  2. Setting up some resources to use inside manifold.co

  3. Plugging in these resources to your application

At the end, you’ll only have to worry about one API key and a project name. That’s all you have to keep secret, the rest gets pulled into your app via our package.

You can find the repository for this demo here.

Steps to make a simple Laravel application

Install Composer

Follow the instructions here to install Composer.

Set up basic demo app

Run:

mkdir demo-app && composer create-project laravel/laravel demo-app && cd demo-app

Verify its running by using the command:

php artisan serve

You should see the boilerplate Laravel 5 home page, feel free to shut the serve down CTL+c

Add the manifold-laravel package:

Install and publish the configuration

composer require manifoldco/manifold-laravel php artisan vendor:publish

Select manifoldco\manifold-laravel from the vendor list.

This will generate config/manifold.php and add two lines to your .env file:

MANIFOLD_API_TOKEN=
MANIFOLD_PROJECT=

You can leave them blank for now, we will get to modifying that a bit later on.

Setup authentication

Our demo is going to showcase a simple user registration system and authentication in an application.

Thankfully Laravel makes this really easy to do with: php artisan make:auth

This adds a bunch of views and routes for account creation, password resets, etc.

Prior to running the migrations, we’ll need to add a database. Let’s add JawsDB from the Manifold marketplace so we don’t have to run the service ourselves.

Using the Manifold Marketplace

Create a Manifold account and provision resources

  1. Create an account (free!) here.

  2. Once your account is verified, follow the flow to make your first project here.

  3. Provision a LogDNA resource and a JawsDB MySQL resource in your newly created project. You can follow instructions here.

Set up your API token

You will need an API Token so your Laravel application can access your Manifold account.

  1. Download the Manifold CLI

  2. Login using manifold login

  3. Create an API token, giving it read credentials:

    $ manifold tokens create ✔ Token Description: test Use the arrow keys to navigate: ↓ ↑ → ← ? Select Role: read ▸ read-credentials write admin

  4. Edit your .env file (never to be committed to git) with:

    MANIFOLD_API_TOKEN= MANIFOLD_PROJECT=project-name

Logging with LogDNA

Like all good apps, I want my logs pushed to a cloud logging provider. For this demo I’ve chosen LogDNA. You will need the name of your LogDNA resource that was provisioned in the previous steps. I named mine logdna.

In your code:

  1. Add the LogDNA monolog package:

composer require nvanheuverzwijn/monolog-logdna

  1. Modify bootstrap/app.php to extend the default logger:

    $app->configureMonologUsing(function($monolog) { $handler = new \Zwijn\Monolog\Handler\LogdnaHandler(config(‘logdna.KEY’),config(‘manifold.project’),\Monolog\Logger::DEBUG); $monolog->pushHandler($handler); });

Explanation: We are pulling in the config (KEY) for logdna via the manifold package config(‘logdna.KEY’) . If you had called your LogDNA resource logger, or anything else, swap logdna for the name you gave it. Also, I’m passing the name of the project up to LogDNA for fun, I could have used “my app name” or anything else I chose there.

Thats it, logging hooked up!

Of course, your app will want to have informative and useful logging, to see more details on Laravel logging, I suggest checking out their docs here. With the above done all your logs will pump to your logdna dashboard, which you can SSO into via the manifold dashboard.

Database with JawsDB

JawsDB is a great example of how Manifold supports aliasing. JawsDB returns its entire username, password, hostname, database and port in one long URL. This is handy, but not so much for Laravel as we need those broken out into different parts. So unlike Logdna where we could simply reference the secrets by resource-name.key, we need to do some fun aliasing in the config/manifold.php file.

In your code:

Modify config/manifold.php and then alias in the mysql config needed.

Embedded content: https://gist.github.com/PLaRoche/44fa39987d54f5ac91dae5ca2ac8d752#file-manifold-php

I’m setting up a series of alias’ to set the database related configs. And that’s it! No need to mod the config/database.php file any more than simply telling it to use mysql. The configs will now be used where needed and Laravel will use our JawsDB instance!

One last step to get it all working:

Mod AppServiceProvider to work with JawsDB

In the AppServiceProvider.php, you include this code top of the file. use Illuminate\Support\Facades\Schema;

And you add this code in boot method. Schema::defaultStringLength(191);

Run the migrations

php artisan migrate

Turn it all on and play

php artisan serve

You should now see a fancy default Laravel app, with login and register functionality all in place. Under the hood it is using your JawsDB MySQL instance, and LogDNA to serve up its internal logs.

From here, we could plug in MemCache for caching (or Redis ) or even switch over to a different JawsDB offering (Postgres). Heck, want to plug elastic in, or email, the list goes on and on :-)

Let me know your thoughts? Have a service, even one not in Manifold you want to plug into your Laravel app? Ping me and maybe we can help (we support custom resources).

StratusUpdate

Sign up for the Stratus Update newsletter

With our monthly newsletter, we’ll keep you up to date with a curated selection of the latest cloud services, projects and best practices.
Click here to read the latest issue.