Connect with us

How to Automate App Setup with Laravel Initializer

kizinho

Published

on

Follow
NEWS: How to Automate App Setup with Laravel Initializer [New  Developer] » Naijacrawl
Photo: Naijacrawl
site

Have you ever found yourself writing multiple manual steps to set up a Laravel application in a new environment? Laravel Initializer is a convenient way to automate installing and updating a Laravel application:

Laravel Initializer gives you the ability to declare multiple processes and run them with app:install and app:update artisan commands, which run predefined actions chain depending on the current environment.

The app:install and app:update commands use two distinct classes that run commands based on a given environment. First, the install command uses the App\Install class:

namespace App;

use MadWeb\Initializer\Contracts\Runner;

class Install
{
    public function production(Runner $run)
    {
        return $run
            ->external('composer', 'install', '--no-dev', '--prefer-dist', '--optimize-autoloader')
            ->artisan('key:generate')
            ->artisan('migrate', ['--force' => true])
            ->artisan('storage:link')
            ->external('npm', 'install', '--production')
            ->external('npm', 'run', 'production')
            ->artisan('route:cache')
            ->artisan('config:cache')
            ->artisan('event:cache');
    }

    public function local(Runner $run)
    {
        return $run
            ->external('composer', 'install')
            ->artisan('key:generate')
            ->artisan('migrate')
            ->artisan('storage:link')
            ->external('npm', 'install')
            ->external('npm', 'run', 'development');
    }
}

The app:update command looks similar, using an App\Update class:

namespace App;

use MadWeb\Initializer\Contracts\Runner;

class Update
{
    public function production(Runner $run)
    {
        return $run
            ->external('composer', 'install', '--no-dev', '--prefer-dist', '--optimize-autoloader')
            ->external('npm', 'install', '--production')
            ->external('npm', 'run', 'production')
            ->artisan('route:cache')
            ->artisan('config:cache')
            ->artisan('event:cache')
            ->artisan('migrate', ['--force' => true])
            ->artisan('cache:clear')
            ->artisan('queue:restart'); ->artisan('horizon:terminate');
    }

    public function local(Runner $run)
    {
        return $run
            ->external('composer', 'install')
            ->external('npm', 'install')
            ->external('npm', 'run', 'development')
            ->artisan('migrate')
            ->artisan('cache:clear');
    }
}

You can also inject dependencies from the service container if you need to access services while running commands.

This package contains a variety of runner actions you should check out in the readme. I found the MakeCronTask dispatch interesting:

$run->dispatch(new \MadWeb\Initializer\Jobs\MakeCronTask)

MakeCronTask adds the following to the server’s crontab list:

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

You can do other things like creating a supervisord config for a typical queue worker or horizon.

You can learn more about this package, get full installation instructions, and view the source code on GitHub at mad-web/laravel-initializer.

site


kizinho

I am a software developer, like meeting people and love blogging, that's why I developed naijacrawl because that's what I love doing.

Continue Reading
Click to comment

Be first to comment


    Leave a Reply

    Your email address will not be published. Required fields are marked *

    How to show a nice message to the user when an error occurs in using Guzzle with Laravel

    kizinho

    Published

    on

    DEVELOPER PROGRAMMING: How to show a nice message to the user when an error occurs in using Guzzle with Laravel [New  Developer Programming] » Naijacrawl
    Photo: Naijacrawl

    Hi Folks, have you encountered an ugly Exception when making a post, get, put, delete to external API when an error like 500 or 404 occurs. Today you are at the right place, after reading this you will be able to show a nice message to your users in case of any of those errors occurs

    When making a request to external API using guzzle to solve this problem you will need to catch the Exception by following the steps below.

    1. Prepare your request using Guzzle, I believe you know or have set it up already
    2. Make a post or get request to your external API
        public function create(Request $request) {
        $input = $request->all();
        try {
            $client = new Client();
            $headers = ['API-Key' => env('API_KEY')];
            $url = 'api/post/create';
            $response = $client->request('POST', $url, ['headers' => $headers, 'query' => $input]);
            $res = json_decode($response->getBody());
    //here will catch error to aviod ugly message to users  
        } catch (\GuzzleHttp\Exception\RequestException $res) {
            if ($res->hasResponse()) {
                $response = $res->getResponse();
                if ($response->getStatusCode() == 500) {
                    abort(500);
                } if ($response->getStatusCode() == 404) {
                    abort(404);
                }
            }
        }
    }
    


    Congrats , you have saved your users from seeing that ugly message . Share and comment

    Continue Reading

    Laravel 6 and 7 Package for Paystack

    kizinho

    Published

    on

    NEWS: Laravel  6 and  7 Package for Paystack [New  Developer] » Naijacrawl
    Photo: Naijacrawl

    Hi, Folks since the release of laravel version 6 and 7, it is very difficult to easily use Paystack on it due to lack of maintenance but today unicodeveloper of the package have taken his time to release a new update on the package.

    To use it, you just need to follow the same pattern we normally do and get things working back again.

    Visit the package URL here


    Continue Reading

    How to solve This page isn’t working HTTP ERROR 500 in Laravel

    kizinho

    Published

    on

    DEVELOPER PROGRAMMING: How to solve This page isn’t working HTTP ERROR 500 in Laravel [New  Developer Programming] » Naijacrawl
    Photo: Naijacrawl

    In case you ran into this problem This page isn’t working www.example.com is currently unable to handle this request HTTP ERROR here is the cause and how to fix it. First, it must not be an error from your code, it is an error from querying large data from your database. So request ran out of memory, to fix it follow this step.

    In your Laravel root project folder locate this file

    vendor/laravel/framework/src/Illuminate/
    Database/Connection.php.
    

    Insert this line of code after line 324 

    ini_set('memory_limit', '-1'); 
    

    or follow the code snippet or image below:


     public function select($query, $bindings = [], $useReadPdo = true)    {        return $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo) {            if ($this->pretending()) {                return [];            }            // For select statements, we'll simply execute the query and return an array            // of the database result set. Each element in the array will be a single            // row from the database table, and will either be an array or objects.            ini_set('memory_limit', '-1');         
    
       $statement = $this->prepared($this->getPdoForSelect($useReadPdo)                              ->prepare($query));            $this->bindValues($statement, $this->prepareBindings($bindings));            $statement->execute();            return $statement->fetchAll();        });    }
    


    Continue Reading

    Latest