Welcome Guest, Not a member yet? Register   Sign In
Undefined property: App\Controllers\Security_Controller::$Users_model
#1

hii, i have an error when load model in initController and extend baseController in other controller i cant access property in baseController the error say [size=1]Undefined property: App\Controllers\Security_Controller::$Users_model[/size]
in my baseController file :

PHP Code:
<?php

class BaseController extends Controller
{
    protected $helpers = ['general''app_files_helper''url''form'];

    /**
    * Constructor.
    */
    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();

        //load models
        $models_array $this->get_models_array();
        foreach ($models_array as $model) {
            $this->$model model("App\Models\\" $model);
        }
      
        
//get the current user id
        $login_user_id $this->Users_model->login_user_id();

    }

    private function get_models_array()
    {
        return array(
            'Users_model',
            'Settings_model'
        );
    }




PHP Code:
<?php

namespace App\Controllers;

class 
Security_Controller extends BaseController 
{
    public $login_user;

    public function __construct($redirect true
    {
        //check user's login status, if not logged in redirect to signin page
        $login_user_id $this->Users_model->login_user_id();

    }

Reply
#2

See https://codeigniter4.github.io/userguide...components
Reply
#3

PHP Code:
$this->$model model("App\Models\\" $model);

// should this be?
$this->$model model("App\Models\" . $model); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(This post was last modified: 02-28-2022, 05:00 AM by devo.)

(02-28-2022, 01:29 AM)InsiteFX Wrote:
PHP Code:
$this->$model model("App\Models\\" $model);

// should this be?
$this->$model model("App\Models\" . $model); 

can't must use \\
[img]https://imgur.com/F3no4hd[/img]
Reply
#5

the solution is i make __construct in baseController
Reply
#6

(02-27-2022, 04:58 PM)kenjis Wrote: See https://codeigniter4.github.io/userguide...components

i don't know this is was the correct solution 

but i make __construct in baseController and extend baseController to other controller and in extended controller i make __construct and call parent::__construct();
Reply




Theme © iAndrew 2016 - Forum software by © MyBB