I need to access to my app services in a simple php script. I don't want to create a HTTP/Console kernel. If the following is the content test.php
file in the app root, what else should I call to make it working?
require_once __DIR__ . "/vendor/autoload.php"; //composer
$app = new Illuminate\Foundation\Application(realpath(__DIR__));
$app->boot(); // not sure about this
$app->make('db')->table('user')->get()->toArray();
Currently I get the following error:
PHP Fatal error: Uncaught ReflectionException: Class db does not exist in foo\vendor\laravel\framework\src\Illuminate\Container\Container.php:752
Fatal error: Uncaught ReflectionException: Class db does not exist in foo\vendor\laravel\framework\src\Illuminate\Container\Container.php:752
After checking the source code it's clear that the Application class does not load all classes defined in the config/app.php
file and either you have bootstrap it yourself of running bootstrap function of build-in kernel. So, If you create a php file with the following lines you can access all app dependencies without bothering creating artisan command to do your snippet codes directly in phpstorm.
require_once __DIR__ . "/vendor/autoload.php"; //composer
$app = new Illuminate\Foundation\Application(realpath(__DIR__));
// http Kernel yet have to be registered, perhaps a bad design issue.
$app->singleton(
Illuminate\Contracts\Http\Kernel::class,
App\Http\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
App\Console\Kernel::class
);
// optional
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();
// and playground is ready
$users= $app->make('db')->table('user')->get()->toArray();
or
require_once __DIR__ . "/vendor/autoload.php";
$app = include_once __DIR__.'/bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();
dd(app('db')->table('user')->count());