0

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
3
  • Are you sure that this is the right thing for your? Did you tried Silex? Can you explain what you want to do? Commented Oct 14, 2017 at 21:35
  • @SebTM oh! I asked one question and you asked three questions! So funny! Commented Oct 14, 2017 at 21:37
  • Just create an artisan command, that's what it is made for. Commented Oct 14, 2017 at 22:10

1 Answer 1

1

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());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.