Codeigniter4 如何从派生的 class 构造函数调用父控制器的构造函数

Codeigniter4 how to call parent controller's constructor from derived class constructor

在 Codeignter 4 中,我们不能在 BaseController 中使用构造函数。但是方法 initController() 会做到这一点。但是如何从派生控制器的构造函数中调用这个方法呢?

我的问题是 BaseController::is_allowed() 方法将执行对所有派生控制器 classes 通常有用的所有基本功能。但是要工作 BaseController::is_allowed()BaseController::__construct() 应该在此之前执行。但是在 CI-4 中,构造函数在 BaseController 中是不允许的。它可以有 BaseController::initController()。但是问题是这个方法只有在DerivedClass::__construct().

之后才会执行

我需要在执行每个派生的 class 方法之前执行 BaseController::is_allowed()。所以我在派生控制器的构造函数中调用 BaseController::is_allowed() 方法。但是派生的 class 构造函数在 BaseController::initController() 执行之前执行。所以 BaseController::is_allowed() 不起作用。

BaseController.php

<?php

namespace App\Controllers;

use CodeIgniter\Controller;
use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;


class BaseController extends Controller
{
    public $request;

    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        
        // Doing all basic setups here that are needed to all other methods in this class.

        // This method will be called only after derivedClass::__construct().
        // But CI-4 not allows to use __construct() method in BaseController class.
        // This causes my problem.
    }

    function is_allowed()
    {
        // Provides all basic features for all derived controller classes.
        // But to work code in this method, initController() method should execute first.
    }
}

而导出的class为

Users.php

<?php

namespace App\Controllers;

class Users extends BaseController
{
    public function __construct()
    {
        // BaseController::is_allowed() will provide all basic features for this controller.
        // To work this method, BaseController::initController() should execute.
        // But this will execute only after this ( __construct()) constuctor.
        // In Codeignier-3, BaseController::__construct() was possible.
        // It will execute before derived class constructor.
        $this->is_allowed();
    }
}

In Codeignter 4 we can't use constructor in controllers.

您没有在 BaseController class 中使用典型的构造函数,您仍然可以在 App/Controller classes 中使用构造函数.

您不需要手动调用 initController,这是在 bootstrap 过程中为您完成的。从 App\Controllers\Users 构造函数中删除该行。

基本上您的用户控制器应该使用 iniController 而不是构造,如下所示:

<?php

namespace App\Controllers;
use CodeIgniter\HTTP\RequestInterface; 
use CodeIgniter\HTTP\ResponseInterface; 
use Psr\Log\LoggerInterface;

class Users extends BaseController
{
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        $this->is_allowed();
    }
}

然而,在您的 BaseController 中创建您的 is_allowed 函数作为受保护函数是一种很好的做法,否则人们可能能够通过任何 url 之类的站点访问它。com/users/is_allowed

我什至可以补充说,如果 is_allowed 函数的目的是检查用户是否有权执行某项操作,甚至是在该控制器中,您应该查看 Filters 而不是这个