Welcome Guest, Not a member yet? Register   Sign In
Autoload for models like BaseController
#1

So I'm working on CodeIgniter 4 lately and I'm trying to put my code from my CodeIgniter 3 project to CodeIgniter 4. Which isn't very easy. I want to use CodeIgniter 4 the best way possible, which is why I'm wondering, what is the best way to include certain functions for all of my models and views.

Autoload for models is gone, I'm aware of that. Instead there is the BaseController. My initController in the BaseController looks like this:

PHP Code:
public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
    {
        
// Do Not Edit This Line
        
parent::initController($request$response$logger);

        
//--------------------------------------------------------------------
        // Preload any models, libraries, etc, here.
        //--------------------------------------------------------------------
        // E.g.: $this->session = \Config\Services::session();
        
$this->session = \Config\Services::session();
        
$this->db = \Config\Database::connect();

        
$this->uri = new \CodeIgniter\HTTP\URI(current_url(true));
        
        
$this->settings = new settings_model();
        
$this->search = new search_model();
        
$this->pages = new pages_model();
    } 

I can use everything in the BaseControntroller inside my other controllers, which is great. However, is there a way to do the same for the models and for the views or do I have to include all the function inside each model and view?

I can put some functions inside the '__construct' of my model, but it's still not very convenient. Things like a database connection and uri use are so commonly used that it feels like a hassle to include them every time. It feels like a step back then a step forward.
Reply


Messages In This Thread
Autoload for models like BaseController - by LuxesR - 06-19-2021, 08:35 AM



Theme © iAndrew 2016 - Forum software by © MyBB