Welcome Guest, Not a member yet? Register   Sign In
A way to override the instantiation of the controller
#7

(This post was last modified: 03-08-2021, 04:35 PM by eavmarshall.)

PHP Code:
<?php

namespace App;

use 
CodeIgniter\CodeIgniter;
use 
Config\Services;
use 
Di\DIContainer;

class 
MyCodeIgniter extends CodeIgniter
{
    protected function createController()
    {
        $class DIContainer::getInstance()->getInstanceOf($this->controller);
        $class->initController($this->request$this->responseServices::logger());

        $this->benchmark->stop('controller_constructor');

        return $class;
    }


PHP Code:
<?php

namespace Config;

use 
App\MyCodeIgniter;
use 
CodeIgniter\CodeIgniter;
use 
CodeIgniter\Config\BaseService;

class 
Services extends BaseService
{
    public static function codeigniter(App $config nullbool $getShared true)
    {
        if ($getShared)
        {
            return static::getSharedInstance('codeigniter'$config);
        }

        $config $config ?? config('App');

        return new MyCodeIgniter($config);
    }


PHP Code:
<?php

namespace App\Libraries;

class 
HomeLibrary
{
    public function sayHello()
    {
        echo 'Hello world from ' __CLASS__;
    }


PHP Code:
<?php

namespace App\Controllers;

use 
App\Libraries\HomeLibrary;

class 
Home extends BaseController
{
    private $homeLibrary;

    public function __construct(HomeLibrary $homeLibrary)
    {
        $this->homeLibrary $homeLibrary;
    }

    public function index()
    {
        $this->homeLibrary->sayHello();

        return view('welcome_message');
    }


Worked perfectly, thanks

Attached Files Thumbnail(s)
   
Reply


Messages In This Thread
RE: A way to override the instantiation of the controller - by eavmarshall - 03-08-2021, 11:29 AM



Theme © iAndrew 2016 - Forum software by © MyBB