Add another controller in codeigniter - php

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

Related

Why I am getting forbidden error during model call in controller

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');

How to load view file in core functions in codeigniter

Am trying to load view file in application/core/MY_Controller.php but its giving as follows.
Message: Undefined property: Edustaticlanding::$load
Filename: core/MY_Controller.php
Function is as follows in MY_Controller.php
function menu(){
$arrr = array();
return $arrdata['menu'] = $this->load->view('menu',$arrr,true);
}
And am calling this function in controller(Edustaticlanding.php) as follows.
function __construct(){
$this->menucontent = $this->menu();
print_r($this->menucontent); die;
}
Pls correct me.. where its going wrong.. thanks
On application/core/MY_Controller.php
Add public $data = array() like below
<?php
class MY_Controller extends CI_Controller
{
public $data = array();
public function __construct()
{
parent::__construct();
$this->data['menu'] = $this->menu();
}
public function menu(){
return $this->load->view('menu', NULL, TRUE);
}
}
On the controller then once add public $data = array(); you can access the menu on view
You have to use $this->data now on controller
<?php
class Example extends MY_Controller {
public $data = array();
public function index() {
$this->data['title'] = 'Welcome to Codeigniter';
$this->load->view('example', $this->data);
}
}
On the example view now you can echo
<?php echo $menu;?>
Add extends CI_Controller to your core controller like following codes:
class MY_Controller extends CI_Controller {
Please check below mentioned solution. You need to call parent constructor first. So it will load all basic configurations.
First extent CI_Controller class and call parent constructor like describe below
class MY_Controller extends CI_Controller {
public function __construct{
parent::__construct();
}
}
Please let me if it not works.

How to load a model in a master controller on codeigniter framework?

I am creating a master controller so that every other controllers on my app extend the master (MY_Controller).
My problem is how to make loading a model more abstract, let me show you the code to better explain.
class MY_Controller extends CI_Controller
{
protected $model;
function __construct()
{
parent::__construct();
}
function get($order_by)
{
$this->load->model($this->model);
$query = $this->$this->model->get($order_by);
return $query;
}
}
I declare a protected variable $model in the master controller so that on the extending controller i can asign it a value:
class Home extends MY_Controller {
public function __construct() {
parent::__construct();
$this->model = "home_model";
}
public function index()
{
$test = $this->get('id');
}
}
The problem is that on MY_Controller i cant load the model
$this->$this->model->get();
i get the following error: Message: Object of class Home could not be converted to string
any help will be appreciated , thank you !
Why do not save the value you assign to protected global $model variable in a variable inside the function as such:
class MY_Controller extends CI_Controller{
protected $model;
function __construct(){
parent::__construct();
}
function get($order_by){
$_model = $this->model // I added this line.
$this->load->model($_model); // I modified this line.
$query = $this->$_model->get($order_by); // I modified this line.
return $query;
}
}
The $this->$this->model->get() is your problem. If you want to use an object property ($this->model) inside a chain like this, you need to wrap it in braces: $this->{$this->model}->get().

Codeigniter Error Unable to locate the specified class: Loader.php

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

Access an array in all views in codeigniter

I made an array in MY_Controller class placed on core folder. In its constructor i fetched records from db so as to make navigation menu in my views. Since i have different page layouts so i cannot call the same header view every where. for this reason i made a core class as per my understanding which i am not sure is right or not. below is the code for my controller
class MY_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('Category_model');
$data['parent'] = $this->Category_model->getParentCategories();
$data['child'] = $this->Category_model->getChildCategories();
}
}
my default controller is main
class Main extends MY_controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('home/header',$data);
$this->load->view('home/footer');
}
Now in my header view i am receiving undefined variable parent and child error. I want this two variables available in all the views so that i do not have to define those two variables in every controller.
Thanks
You may try something like this:
class MY_controller extends CI_Controller
{
$commonData = array();
function __construct()
{
parent::__construct();
$this->load->model('Category_model');
$this->commonData['parent'] = $this->Category_model->getParentCategories();
$this->commonData['child'] = $this->Category_model->getChildCategories();
}
}
Then use $this->comonData in your index method instead of $data to pass to the view:
public function index()
{
$this->load->view('home/header', $this->comonData);
$this->load->view('home/footer');
}
Now it'll be available in the header view and since it's at the top of other views then you may use it further, unless you override it with other value in any class.

Categories