1 month ago

Upkeep Tasks

One of the issues we've ran into over at efelle in the development of FusionCMS is having to run maintenance or "upkeep" tasks. We've been utilizing migrations for these items - but doing so feels like a misuse of what migrations are originally intended for.

While migrations are used to scaffold our database layer, our upkeep tasks simply need to perform a single task. These tasks should also be capable of being ran at anytime during the application's lifespan.

With this in mind, we've come up with what we think is a solid and sound approach to performing upkeep tasks for our applications.

  • Upkeep tasks are not tracked when ran like migrations are. There will be no "upkeep" table for example.
  • Unlike migrations, there will only be a single handle method. Upkeep tasks are not meant to be reversed in any manner.
  • Upkeep tasks should be completely independent of one-another. Once registered, it shouldn't matter which order they are ran in.
  • By convention it makes sense to store upkeep tasks within a Support/Upkeep directory - but in reality these tasks can be stored and maintained anywhere in your application.
  • Upkeep tasks will be registered to a global container during runtime. This will simply store the reference to our upkeep tasks in the event that upkeep should be performed.

Theoretically, all of this is possible and can be simply handled through Laravel's Job class interface, but personally, I'd like to streamline the process a bit more - so keep an eye out for a new package dropping under the Caffeinated namespace!

1 month ago

Use Laravel Mix in Stand-Alone Projects

Laravel Mix has brought the world of webpack to many developers in the form of a tightly coupled and easy to use API. Let's see how we can get this up and running for our stand-alone projects that don't utilize Laravel.

Install Laravel Mix and Webpack

Using yarn or npm, install both Laravel Mix and Webpack to your project. I will be utilizing yarn in the examples below.

yarn add webpack laravel-mix

Copy the Laravel Mix Configuration

cp -r node_modules/laravel-mix/setup/webpack.mix.js ./

Add Compilation Scripts to Your package.json

"scripts": {
    "dev": "NODE_ENV=development webpack --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "watch": "NODE_ENV=development webpack --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "hot": "NODE_ENV=development webpack-dev-server --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
    "production": "NODE_ENV=production webpack --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
  }

You're now all set! Simply hop in your webpack.mix.js file to configure the files you wish to compile.

4 months ago

Form Requests and Form Objects

In most cases, performing the persistence logic within the Controller is simple. In the cases where I need to perform additional tasks, muddying up the Controller with this logic can get a little messy. I want to clean this up, but how?

Form Objects.

Laravel already provides an easy to use interface for handling form requests in terms of validating the request data. I want to take this one step further, and turn my Form Requests into Form Objects. Form Objects will not only handle validating our request, but also be in charge of persisting the request to storage (e.g. the database).

To do this, I simply need to add a persist method:

<?php

namespace Modules\Test\Http\Forms;

class CreateTestForm extends FormRequest
{
    ...

    public function persist()
    {
        dump('perform necessary tasks and persist the data.');
    }

    ...
}

And within the controller, simply call it:

public function store(CreateTestForm $form)
{
    $form->persist();

    ...
}

Notes

One important thing to keep in mind with this approach, is to create a form object for each individual "form". This is important, because:

  1. Each form will have different validation rules.
  2. Each form will have different authorization rules.
  3. Each form will have different persistence logic - am I creating a new entry or updating an existing one?
  4. It keeps the controller and model clean!