Search code examples
phplaravel-5inversion-of-control

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

  • 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");