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

As for the BaseModel.php. Find it out later.

The BaseModel should look like this:

PHP Code:
namespace App\Models;
use 
CodeIgniter\Model;
class 
BaseModel extends Model {

    function 
__construct(){
        
$this->session = \Config\Services::session();
        
        
$this->uri = new \CodeIgniter\HTTP\URI(current_url(true));
    }


And it should be looking like this in your model:
PHP Code:
class User_accounts_model extends \App\Models\BaseModel {

    function 
__construct(){
        
parent::__construct();
    }


At least, this seems to work.

(06-22-2021, 05:22 AM)MGatner Wrote: Unlike CI3, version 4 has convenient ways for accessing components on-the-fly. There is nothing wrong with preloading instances into your BaseController but a) you run the risk offloading things you never use for a performance hit, and b) these instances would need to be passed from your Controller if you want them accessible anywhere else.

For starters I recommend reading up on Services. This is CI4’s version of a dependency container - a way of making “shared instances” of various components available to other components. So I’m your example, instead of preloading a URI instance you can just grab it when you need it:

service('uri')->setPath('foo/bar');


Because Services is managing your instances, this will be the same object anywhere you call it. So if the above code was in a Filter then you can reuse the same URI in your view:

Current path is <?= service('uri')->getPath() ?>
Thanks for your response and advice. That seems to be what I'm looking for. I will read into that.
Reply


Messages In This Thread
RE: Autoload for models like BaseController - by LuxesR - 06-22-2021, 05:55 AM



Theme © iAndrew 2016 - Forum software by © MyBB