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

DOD's avatar
Level 2

Using Illuminate\Container as a Stantalone

Hi guys,

I am currently trying to create an app that utilizes Laravel's Illuminate\Container class and have 2 questions regarding my implementation.

Please see the following code pieces:

Index.php

<?php
namespace App;

require 'vendor/autoload.php';

use App\Core\Container as Container;
use App\Core\AppInit as AppInit;

$cont = new Container();

// instantiate AppInit
$app = $cont->make('AppInit');

// call method output, pass in object name
$app->output('OutputEvents');

Container.php

<?php

namespace App\Core;

use Illuminate\Container\Container as App;

class Container extends App {

    function __construct() {
        $this->register_container();
    }

    private function register_container() {

        // bind the container
        $this->bindShared('Container', function(){
            return new Container();
        });

        // bind the AppInit class
        $this->bindShared('AppInit', function(){
            return new AppInit($this);
        });

    }

}

AppInit.php

<?php

namespace App\Core;

class AppInit {

    function __construct(Container $container) {

        setlocale(LC_TIME, CONFIG::app_language);

        // register container
        $this->container = $container;

        // instantiate blade
        $this->blade = $this->container->make('App\Core\Blade');

    }

    public function output($class_title) {

        // all classes will exist within App\Modules namespace
        $full_class_name = '\App\Modules\\' . $class_title;
        
        if(class_exists($full_class_name)) {
            $this->{$class_title} = $this->container->make($full_class_name);

        } else {
            echo "Module not found";
            return false;
        }

        try {
            echo $this->blade->view()->make($class_title)->withData($this->{$class_title}->data);
        } catch(Exception $e) {
            echo "Blade view does not exit";
            return false;
        }

    }
    
} 

Am I wrong in passing my container instance to 'AppInit'?

To explain what is going on with the AppInit->Output method, a class name is passed as a parameter and then the object is instantiated via the container. The 'data' variable is then passed to blade for output.

Can anyone see something wrong with this implementation?

0 likes
0 replies

Please or to participate in this conversation.