Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter 4 Models preloading
#2

(This post was last modified: 02-28-2020, 02:47 PM by dave friend.)

You should preload components (like models, etc.) by using initController() as demonstrated here.

There is a controller named BaseController distributed with the framework that is included exactly for the purpose of doing what you want. Look closely at how initController() is defined in app\Controllers\BaseController.

PHP Code:
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
{
 
// Do Not Edit This Line
 
parent::initController($request$response$logger);

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


Put your model loading in the commented section near the bottom. (Oh, and remove the comments.) It might look like this

PHP Code:
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
{
 
// Do Not Edit This Line
 
parent::initController($request$response$logger);

 
//--------------------------------------------------------------------
 // Preload any models, libraries, etc, here.
 //--------------------------------------------------------------------

        $this->vartotojai =  new vartotojai_model();


Then extend any controllers that need this model from BaseController

PHP Code:
namespace App\Controllers;

use 
CodeIgniter\Controller;

class 
NewController extends BaseController
{
        //use $this->vartotojai as needed


On a side-note: Please don't use Lightshot or the like to show code. Instead, copy & paste and surround the lines of code with [php] ...lines of code... [\php] tags. Thanks.
Reply


Messages In This Thread
CodeIgniter 4 Models preloading - by Aliton - 02-28-2020, 05:42 AM
RE: CodeIgniter 4 Models preloading - by dave friend - 02-28-2020, 02:44 PM



Theme © iAndrew 2016 - Forum software by © MyBB