Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

antonsash's avatar

How to bootstap Laravel 5.0 application from console php file?

Hello! I have a Laravel 5.0 application, and i want to integrate it with my other projects. For this, i want to use socket apis.

For example, i make socket.php in app folder.

<?php namespace App\Socket;

set_include_path(realpath(__DIR__.'/../'));
require_once 'bootstrap/autoload.php';
$app = require_once 'bootstrap/app.php';

if (!empty($_POST['m'])) {
    $method = $_POST['m'];
    $args = !empty($_POST['a']) ? $_POST['a'] : array();
    echo serialize(call_user_func_array(array(new Methods, $method), $args));
}
<?php namespace App\Socket;
use App\Company;
class Methods
{
    public function hello()
    {
        return Company::all()->take(5)->toArray();
    }
}

in this class i use model Company, but receive a fatal error: PHP Fatal error: Call to a member function connection() on a non-object

I think i need to bootstrap whole application with models, but don't know how...

0 likes
1 reply
jimmck's avatar

First off. You are using POST Globals, how are they being populated?

Second, why are you putting things into App? If you are going that far, then just run it like normal.

When to test or get access to parts of Laravel Infrastructure and my own parts, I create a BIN directory under the main laravel install dir, not App.

At the top of my PHP file I do the following:

<?php

use App\lib\myutils\apputils\mongo\MongoConnection;
use App\lib\myutils\Person;
use App\lib\myutils\Junk;
use App\lib\myutils\Dummy;
use App\lib\myutils\Everything;
use App\lib\myutils\Address;
use App\lib\myutils\Contact;
use App\lib\myutils\Email;
use App\lib\myutils\ContactNote;
use App\lib\myutils\Note;
use App\lib\myutils\apputils\mongo\MongoArray;
use App\lib\myutils\apputils\mongo\MongoAssocArray;
use App\lib\myutils\MyModel;
use App\lib\myutils\utils\Utils;

require dirname(__DIR__) . '/vendor/autoload.php';
require dirname(__DIR__) . '/bootstrap/app.php';

As you can see, I reference my custom libs via the bootstrapping Laravel does at startup. I can even access the Kernerl and get the path to App directory. Then I can dynamically load classes as if I am running inside a controller.

So I can pull in class info from a data source like Mongo and instantiate a class.

                    $docType = $source[$key]['_docType'];
                    $className = $this->getNamespace() . $docType;
                    $class = new $className;
                    $class->loadFromSource($source, $key);
                    $field->setValue($class);

getNamespace() makes the Kernel call to get the Laravel application root.

But, if I try to call some code in a controller directly, it fails when it looks up the Facade, either the Filesystem.php file has not been run or the Facade has not been initialized completely.

    $s3 = Aws\Laravel\AwsFacade::get('s3');
    $result = $s3->putObject(array ('Bucket' => 'jimmck-holmdel', 'Key' => 'melissa.txt', 'Body' => 'Hi!!!!!',));
    print  $result['Expiration'] . "\n";
    print  $result['ServerSideEncryption'] . "\n";
    print  $result['ETag'] . "\n";
    print  $result['VersionId'] . "\n";
    print  $result['RequestId'] . "\n";
    // Get the URL the object can be downloaded from
    print  $result['ObjectURL'] . "\n";

Fails in Facade:

    public static function __callStatic($method, $args)
    {
        $instance = static::getFacadeRoot();

        switch (count($args)) {
            case 0:
                return $instance->$method();

            case 1:
                return $instance->$method($args[0]);

            case 2:
                return $instance->$method($args[0], $args[1]);
...

Thanks to PHPStorm to let me debug into the process.

So you are going to need to dig deeper to see what else needs to get started inm order to complete the call.

/Applications/XAMPP/bin/php -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9000 -dxdebug.remote_host=127.0.0.1 /Users/jimm/GitHub/laravel5/bin/mongo-test.php

PHP Fatal error:  Call to a member function get() on null in /Users/jimm/GitHub/laravel5/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 210
Fatal error: Call to a member function get() on null in /Users/jimm/GitHub/laravel5/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 210
PHP Stack trace:

PHP   1. {main}() /Users/jimm/GitHub/laravel5/bin/mongo-test.php:0
Call Stack:
PHP   2. Aws\Laravel\AwsFacade::get(*uninitialized*) /Users/jimm/GitHub/laravel5/bin/mongo-test.php:38
    0.2874     305960   1. {main}() /Users/jimm/GitHub/laravel5/bin/mongo-test.php:0
PHP   3. Illuminate\Support\Facades\Facade::__callStatic($method = *uninitialized*, $args = *uninitialized*) /Users/jimm/GitHub/laravel5/bin/mongo-test.php:38
   13.0922    3202744   2. Aws\Laravel\AwsFacade::get(???) /Users/jimm/GitHub/laravel5/bin/mongo-test.php:38
   13.0922    3203144   3. Illuminate\Support\Facades\Facade::__callStatic(???, ???) /Users/jimm/GitHub/laravel5/bin/mongo-test.php:38


Process finished with exit code 255

Please or to participate in this conversation.