kenjis / codeigniter-ss-twig

A Simple and Secure Twig integration for CodeIgniter 3.x and 4.x
MIT License
168 stars 46 forks source link

The invocation of `Kenjis\CI4Twig\Twig` results in the error message: "Call to a member function render() on null." #72

Closed SilkeKohl closed 5 months ago

SilkeKohl commented 5 months ago

I am very much asking for advice on how to resolve the following error: "Call to a member function render() on null."

This issue arises when calling it in the following manner:

<?php
namespace App\Controllers;

use App\Libraries\MyEasyTaskScheduler;
use App\Controllers\GetSystem;

class TaskScheduler extends BaseController
{
    private $taskScheduler;

    public function __construct()
    {
        $this->taskScheduler = new MyEasyTaskScheduler();
    }

    public function runTask()
    {
        $this->taskScheduler->named('genStaticPage')
        ->callTask(function(){
            (new GetSystem)->genStaticPage();
        })
        ->nextRun('+1 hour');
    }
}

Here's some additional information:

  1. Controller and Method Behavior:

    • The GetSystem controller and its genStaticPage method work correctly when used in the standard way.
    • For instance, if you add the following route to test/app/Config/Routes.php:
      $routes->get('gen_static\.html', 'GetSystem::genStaticPage');

      and then access http://my-test.dev/gen_static.html, everything functions as expected.

  2. Class Initialization and Variable Assignment:

    • Your class is initialized in the BaseController within the initController function and assigned to the variable: protected $twig;.
    • Out of curiosity, I added the following code snippet to the genStaticPage function in the GetSystem class:
      var_dump($this->twig);

      When called in the manner described in Info 1, it returns the complete data for the Twig class. However, when called in the way that triggers the error, it only returns NULL NULL NULL NULL NULL NULL NULL.

  3. MyEasyTaskScheduler Code Fragment: Below is a snippet from the MyEasyTaskScheduler library responsible for executing code:

    <?php
    
    class MyEasyTaskScheduler {
       // Other code
       public function callTask($callback)
       {
           $this->callback = $callback;
           return $this;
       }
       // More code
       private function executeCallback()
       {
           if ($this->callback instanceof Closure)
           {
               // Invoke the function passed as a closure
               return $this->callback->__invoke();
           }
    
           // Return null
           return null;
       }
       // Additional code
    }

Please help me with this problem.

kenjis commented 5 months ago

You should not use Controllers like that. It is a misuse. Controllers should be executed by the framework.

The reason why $this->twig is null is because you did not call initController(). See the framework code to create a controller: https://github.com/codeigniter4/CodeIgniter4/blob/813058769ac521625ec2cfd0212dc3f04d4e296d/system/CodeIgniter.php#L913-L918