我正在使用最新版本的codeigniter框架。我的代码出了问题,它给了我一个错误,比如:
类"App\Model\Users"未找到
控制器
文件名:Auth.php
<?php
namespace AppControllers;
use CodeIgniterRESTfulResourceController;
use AppModelUsers as CodeIgniterUsers;
class Auth extends ResourceController
{
public function login()
{
$model = new CodeIgniterUsers();
var_dump($model);
}
public function register()
{ }
}
型号
文件名:Users.php
<?php
namespace AppModel;
use CodeIgniterModel;
class Users extends Model
{
protected $db;
protected $table = 'user';
protected $returnType = 'array';
protected $allowedFields = ['name', 'email', 'password'];
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
}
如果您没有在应用程序中更改目录名,则需要将名称空间从app\Model\Users(末尾没有"s"(更改为app\Model\User。
命名空间应该遵循目录结构,除非您更改(或扩展(CI4的核心类,或者至少更改app/Config/Autoload.php
使用CI_model扩展您的模型,因为它将被识别。
class Auth_model extends CI_Model {
//you can always put function construct
public function __construct (){
parent::__construct ();
}
}
控制器内:
class User extends CI_Controller {
public function __construct () {
parent:: __construct();
//you can load here the model that you will just often so will load it everytime to use it in a function
$this->load->auth_model('model-name(exam- public function test_uer()');
}
}