Я новичок в PHP OOP и CodeIgniter, и мне нужна помощь. Я использую CodeIgniter 4 и пытаюсь предварительно загрузить модель logas_model в BaseController. php:
<?php
namespace App\Controllers;
/**
* Class BaseController
*
* BaseController provides a convenient place for loading components
* and performing functions that are needed by all your controllers.
* Extend this class in any new controllers:
* class Home extends BaseController
*
* For security be sure to declare any new methods as protected or private.
*
* @package CodeIgniter
*/
use App\Models\Admin\logas_model;
use CodeIgniter\Controller;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
class BaseController extends Controller
{
/**
* An array of helpers to be loaded automatically upon
* class instantiation. These helpers will be available
* to all other controllers that extend BaseController.
*
* @var array
*/
protected $helpers = ['form', 'session', 'html', 'number'];
protected $logas;
/**
* Constructor.
*/
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
// Do Not Edit This Line
parent::initController($request, $response, $logger);
//--------------------------------------------------------------------
// Preload any models, libraries, etc, here.
//--------------------------------------------------------------------
///$this->session = \Config\Services::session();
$this->logas = new logas_model();
}
}
Код модели logas_model:
<?php namespace App\Models\Admin;
use CodeIgniter\Model;
use Config\Services;
class logas_model extends Model
{
protected $table = 'veiksmai_logs';
protected $primaryKey = 'id';
protected $returnType = 'array';
protected $useSoftDeletes = true;
protected $useTimestamps = TRUE;
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
protected $allowedFields = ['uid', 'veiksmas'];
public function get_all_veiksmai()
{
return $this->select("CONCAT(`user`.`vardas`, ' ', `user`.`pavarde`) AS `user_vardas_pavarde`, `veiksmai_logs`.`veiksmas`, `veiksmai_logs`.`created_at`, `veiksmai_logs`.`id`")
->join('user', 'user.id = veiksmai_logs.uid')
->orderBy("`veiksmai_logs`.`created_at`", 'DESC')
->findAll();
}
public function add_veiksmas($veiksmas)
{
$session = Services::session()->get();
$data = [
'uid' => $session['uid'],
'veiksmas' => $veiksmas
];
return $this->insert($data);
}
}
И затем в другом контроллере, который расширяет контроллер Я пытаюсь использовать эту предварительно загруженную модель, используя ее функцию, такую как:
$this->logas->add_veiksmas('Created User.');
Полный код контроллера:
<?php namespace App\Controllers\Admin;
use App\Models\Admin\vartotojai_model;
use CodeIgniter\Controller;
class Vartotojai extends Controller
{
private $vartotojai;
public function __construct()
{
$this->vartotojai = new vartotojai_model();
}
public function index()
{
$this->logas->add_veiksmas('Created User.');
$this->data['vartotojai'] = $this->vartotojai->get_all_vartotojai();
echo view('templates/Admin/Header', ['title' => 'Vartotojai']);
echo view('Admin/Vartotojai/Index', $this->data);
echo view('templates/Admin/Footer');
}
}
Но она не работает.
Что я делаю не так?