i am trying to make a core class in my codeigniter but its giving error that Unable to locate the specified class: Loader.php.
My Core class is MY_base.php and code is.
class MY_base extends CI_Controller{
public function load_header(){
$this->load->model('mod_practice');
$headData=$this->model->get_header();
$this->load->view('header',$headData);
}
}
My model Mod_practice.php code is
class Mod_practice extends CI_Model{
public function get_header(){
$query = $this->db->get('header');
$result = $query->result_array();
return $result;
}
}
My home.php ( main controller) code is
class Home extends MY_loader{
function index(){
parent::MY_base();
}
}
but when i try to run Home controller its giving me the following error
Unable to locate the specified class: Loader.php.
Where can be the error ? Thanks in advance.
You are doing something wrong. You need to construct CI_controller first.
class MY_base extends CI_Controller{
public function __construct()
{
parent::__construct();
// Your own constructor code
}
public function load_header(){
$this->load->model('mod_practice');
$headData=$this->model->get_header();
$this->load->view('header',$headData);
}
}
And now you can do this:
class Home extends MY_base{
public function __construct()
{
parent::__construct();
// Here you have access to load_header() function
}
}
Ou - and also your create MY_base and then refer to MY_loader.
may bee you can use HMVC Codeigniter https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc
Related
My controller, if I run this, it works properly
class Front_controller extends CI_Controller{
public function index(){
$this->load->view('front/index');
}
}
if I call model
class Front_controller extends CI_Controller{
public function index(){
$data['list'] = $this->Mymodel->getdata();
$this->load->view('front/index',$data);
}
}
it gives :
403 forbidden error and 500 internal server error
You need to load the model before using it, like this:
$this->load->model('Mymodel');
$this->Mymodel->mymodelmethod();
More info here: https://codeigniter.com/user_guide/general/models.html#loading-a-model
Update your code:
class Front_controller extends CI_Controller{
public function index(){
$this->load->model('Mymodel');
$data['list'] = $this->Mymodel->getdata();
$this->load->view('front/index',$data);
}
}
Before use of model in CI, you need to load that model by using the method $this->load->model('modelName');
Hello I'm currently facing a problem of adding another controller and the problem is that
I have 2 controllers
class 1st_Controller extends CI_Controller {
}
and
class 2nd_Controller extends CI_Controller{
my 2 models are working perfectly fine and the only problem is that I need to call 1 model per each controller
for example
1st controller is for 1st model and 2nd controller is for 2nd model.
Now what I've tried so far is
class 2nd_Controller extends 1st_Controller {
public function __construct()
{
header("Access-Control-Allow-Origin: *");
parent::__construct();
$this->load->model('2nd_model','2ndmodel');
$this->load->helper('url');
$this->load->library("pagination");
$this->load->library("session");
}
public function index()
{
$data['title'] = 'System Login';
$get_all_inv = $this->2ndmodel->get_all();
$data["tryvariable"] = $get_all_inv;
$this->template->load('default_layout','contents','myview2nd', $data);
}
}
I tried echo in my view like this
<?php echo $tryvariable; ?>
but no luck because the error says that it is an undefined variable .
Your second controller can't be define cause it's not define as subclass_prefix in your codeigniter application.
class 2nd_Controller extends 1st_Controller { //codeigniter don't recognize this.
}
The simpliest way to solve your problem is.. the fact that you can call multiple models in 1 Controller.
so you can have:
class 1st_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('1st_model','1stmodel');
$this->load->model('2nd_model','2ndmodel');
}
}
or call just once.
class 2nd_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('2nd_model','2ndmodel');
}
}
Hope that helps.
So what I did was write like this on my route to have my 2nd_Controller working
$route['default_controller'] = '1st_Controller';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['2nd_Controller'] = '2nd_Controller';
Now everything is working perfectly. Thanks for all the help
I am trying to add phpstorm autocomplete for codeigniter as seen in different quesiton.
The link provided: https://github.com/topdown/phpStorm-CC-Helpers
I commented out the specified files and added
* #property Test_model $test_model
to my_models.php
When I try to load Test_model.php from my controller using:
$this->load->model('Test_model');
I get:
'Unable to locate the specified class: Model.php'
If I remove the load row and try:
$this->Test_model->insert_entry()
I get:
Message: Undefined property: Test::$Test_model
Test.php:
class Test extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function modelTutorial(){
$this->Test_model->insert_entry();
}
Test_model.php
class Test_model extends CI_Model{
public function __construct(){
parent::__construct();
}
public function insert_entry(){
die('asdasd');
}
}
When creating models, you need to place the file in application/models/ and name the file in all lowercase - like yourfile_model.php
Change your file name from Test_model to test_model.php and call it on controller constructor
public function __construct(){
parent::__construct();
$this->load->model('test_model');
}
Read manual Models
My CI version is CI2.3. I'm running this php code in my local host. I followed all the steps given there but I'm getting this error don't know why? and I changed Controller to CI_Controller. Hello world Program worked finely. This link code is not working. please need help!
you should extend model like this in codeIgniter
class Modelname extends CI_Model
{
function __construct()
{
parent::__construct();
}
}
Well actually the study guide is of old version of CI, where you used to extend your models from Model class as show in the guide. But now it has been changed. Now you have to extend it from CI_Model, same goes for Controller.
For controllers
class Employee_controller extends CI_Controller
{
//load the constructor
function __construct()
{
//inherit the parent constructor
parent::__construct();
}
}
and for models:
class Employee_model extends CI_Model
{
//load the constructor
function __construct()
{
//inherit the parent constructor
parent::__construct();
}
}
Use like this
<?php
class Employee_model extends CI_Model
{
//load the constructor
function __construct()
{
//inherit the parent constructor
parent::__construct();
}
}
You must create a model in the model folder like my_model.php
And create the class like
class My_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
}
Remember the class and the file should be same.
Docs http://ellislab.com/codeigniter/user-guide/general/models.html
I'm trying out codeigniter for the first time on a MAMP deployment however I'm unable to load a model into a controller. The end result is always...
Unable to locate the model you have specified: second_model
Controller - welcome.php
class Welcome extends CI_Controller {
public function index()
{
$this->load->model('second_model');
$this->load->view('welcome_message');
}
}
Model - second_model.php
class Second_model extends Model {
public function __construct()
{
// model constructor
parent::__construct();
}
}
You should be extending CI_Model rather than Model
So:
class Second_model extends CI_Model {
public function __construct()
{
// model constructor
parent::__construct();
}
}