CodeIgniter Forums
Plain Controller, without extending BaseController ?? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: Plain Controller, without extending BaseController ?? (/showthread.php?tid=90230)



Plain Controller, without extending BaseController ?? - abdmaster - 02-24-2024

Just want to know if it is possible to have a plain controller class without extending the base controller ?? or without initController() ?? with my own dependency injection in __constuct() ??

I'm doing a test to see if I my plain controller class can be used by other frameworks. It looks something like this:

Code:
<?php

namespace MyPackage\Controller;

use MyPackage\RequestInterface as Request;
use MyPackage\ResponseInterface as Response;

class HealthController
{
    public function __construct(
        public Request $request,
        public Response $response,
    ) {
    }

    public function index()
    {
        $data = [
            'status' => true,
            'queries' => $this->request->allQueries(),
        ];

        return $this->response->setJson($data)
            ->setStatusCode(200)
            ->respond();
    }
}



RE: Plain Controller, without extending BaseController ?? - kenjis - 02-24-2024

Probably possible if you extend the CodeIgniter::createController() method.

See
- https://github.com/kenjis/ci4-tettei-apps/blob/develop/app/MyCodeIgniter.php
- https://github.com/kenjis/ci4-tettei-apps/blob/31786c7c3001b010791aec45c5add6be8bbfc8f6/app/Config/Services.php#L34-L43