'How to bootstrap a Laravel command line script?

I want try some code in cmd. I need to access to the service container of a full MVC laravel app.

I have create a PHP file in the root directory of application with the following contents:

<?php
       require_once __DIR__ . '/bootstrap/autoload.php';
$app = require_once __DIR__ . '/bootstrap/app.php';

dd(config('app.locale')); // does not works!

However the no service is available. What other should I add to the file to make it working?



Solution 1:[1]

You need to bootstrap the app. The simplest way to do this is through the console kernel. This will register providers, facades, etc.

require_once("vendor/autoload.php");
$app = require_once("bootstrap/app.php");

$kernel = $app->make(\Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();

echo config("app.name");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Handsome Nerd