Todd Austin

Personal Blog

Laravel Configuration Script Template

1 year ago · 2 MIN READ
#Laravel 5  #Console Commands 

Many developers find it kinda confusing at first on how to go about creating install scripts for clients or users of open source projects. This is a simple template/step-by-step process to help you accomplish that. You can fork, contribute to it or just copy the template/process into your app and improve on it based on your specific need.

Let's get started creating your first Artisan command. Remember, if you ever get stuck in a certain area, always go back to the Official Documentation.

Step 1: Create the Command

Using the Terminal or an equivalent shell, run this command inside an installation of Laravel:

php artisan make:console Config

This creates a file called Config.php in the app/Console/Commands directory.

Step 2: Define the Command

Open the file app/Console/Commands/Config.php and drop the following code:

<?php

namespace App\Console\Commands;

use DB;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;

class Config extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'laravel:config';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Configure your Laravel application';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        try {
            DB::connection();
        } catch (Exception $e) {
            $this->error('Unable to connect to database.');
            $this->error('Please fill valid database credentials into .env and rerun this command.');
            return;
        }

        $this->comment('Attempting to configure the application...');

        if (! env('APP_KEY')) {
            $this->info('Generating app key');
            Artisan::call('key:generate');
        } else {
            $this->comment('App key exists -- skipping...');
        }

        $this->info('Migrating database...');

        Artisan::call('migrate', ['--force' => true]);

        $this->comment('Database Migrated Successfully...');

        $this->info('Seeding DB data...');

        Artisan::call('db:seed', ['--force' => true]);

        $this->comment('Database Seeded Successfully...');
        $this->comment('Successfully Configured! You can now run the application.');
    }
}

By adding the command, a user can now run php artisan laravel:config and automate the initial configuration process.

Conclusion

The custom command runs through the following steps:

  • Ensuring the user has a database created with valid database credentials loaded in the .env file
  • Generating an application key
  • Migrating the database scripts
  • Seeding the database with data (Ensure you have the seeder classes set up)

This process captures the basic install process every user usually goes through in just one script, you can definitely add or improve on it.

···

Todd Austin

Web artisan with a passion for open source technologies.
comments powered by Disqus


Proudly powered by Canvas · Sign In