Model Fails To Load - codeigniter

I am having issues with my Code Ignitor, in that I load my model the code fails, without leaving any trace.
I am attempting to load my model like so
<!--CONTROLLER-->
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Concierge extends Public_Controller {
public function submit()
{
$this->load->model('Concierge_model');
}
My model is set up as below:
<!--MODEL-->
<?php
class Concierge_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
function saveRequest($action, $owner)
{
$query = $this->db->query('select * from table');
return $query;
}
Does anyone see what might be the issue that is causing my problem? I am using CodeIgnitor 2.1.2.

Assuming Public_Controller is something custom, make sure that class extends CI_Controller and also make sure the parent's contruct function is called there.
class Public_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
}

I don't know what the problem was, I deleted the model, view, and controller and recreated them. This fixed the issue, but sadly didn't shed any light onto what caused it.

Related

How to redirect old php routes in codeigniter

I have old urls like http://example.com/products.php?s_productid=231 and now in my proyect whit url would be http://example.com/products/ver/231
How can I make a rule in routes.php to redirect?
Thanks
you don't need to do any change in routes.php, all you need is to create a Controller "Product", with one Method "ver", which receives 231 (it's id I guess) as a parameter, like this:
defined('BASEPATH') OR exit('No direct script access allowed');
class Product extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function ver($id) {
// do your things
}
I hope it helps.
You may need to set the routes in config/routes.php
$route['product/ver/(:num)'] = 'product/ver/$1';
http://example.com/index.php/products/ver/231
Then the controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Product extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function ver($id) {
/*
echo $id;
*/
}

Redirect loop while extend controller Codeigniter

I have a problem in login application in codeigniter , the problem is like this:
I create MY_Controller, like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public function __construct(){
parent::__construct();
}
}
Then I extended it on my library that i created, this is my "Petugas_Controller" library:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Petugas_Controller extends MY_Controller {
public function __construct(){
parent::__construct();
$this->load->model('petugas_m');
} // end contructor.
public function check(){
if ($this->uri->uri_string() !== 'index' && ! $this->petugas_m->is_logged_in() == TRUE){
redirect('admin/petugas/index');
}
}
}
I'm using that library to be used in my "Petugas" controller, like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Petugas extends MY_Controller {
public function __construct(){
parent::__construct();
$this->load->library('petugas_controller');
$CI = &get_instance();
$CI->check();
}
public function index(){
echo "You have to login";
}
public function dashboard(){
echo "Welcome to dashboard";
}
}
In "Petugas_Controller" library, I loaded model that is called "Petugas_m", here is the model :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Petugas_m extends CI_Model {
public function __construct(){
parent::__construct();
}
public function is_logged_in(){
return FALSE;
}
}
That's all what i was trying, and my problem is I want to check if username is logged or not, by doing $this->check() in "Petugas_library" that loads "Petugas_m" model, i could see an dashboard echo if the "check" method in "Petugas_m" is set to TRUE but it's always redirect me a loop if it FALSE.
I was searching for tutorials and still have this crazy problem :( ,
Thank in advance, and sorry for my bad english.
As long as your petugas_controller library is located within the libraries folder, you can access the check method via
$this->petugas_controller->check();
Also try changing the if statement in your check to something more like this
if ($this->uri->uri_string() !== 'index' && $this->petugas_m->is_logged_in() !== TRUE){
lol, I've just realized my mistake here, I've just tried to make another controller, it's called login, then in my "petugas_controller" library I redirect the user if not login to this "login" controller, it's totally worked ...
Youy guys are inspiring me so much,Thanks :)

codeigniter : load second controller after execution of first controller

we have two controllers one is members.php which is registering the user another one is reserv.php for reserving a ticket for busses.
I want to load second controller after completion of first controller class.
members.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Members1 extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('member_model1');
}
public function index() {
$table = $this->member_model1->insert_members();
$data['members'] = $table;
//$this->load->view('header1', $data);
$this->load->view('reservation_view',$data);
//$this->load->view('members');
//$this->load->view('footer');
}
}
reserv.php
<?php
class reserve extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('member_model1');
}
function index() {
$table = $this->member_model1->get_members();
$data['members'] = $table;
//$this->load->view('header1', $data);
$this->load->view('reservation_view',$data);
//$this->load->view('reservation_view');
// $this->load->helper(array('form'));
//$this->load->view('reservation_view');
}
}
?>
To call a controller from another controller, you can use the function redirect() :
// In Members1 controller
redirect('/reserve/index');
Note that you won't be able to send data along with the redirect, such as when you load a view. To send data, you must use $this->session->set_flashdata(); because the redirects seem to use header() (I advise you to have a loot at this post).
// In Members1 controller
$this->session->set_flashdata('key', 'value');
redirect('/reserve/index');
By the way, when I look at your two classes, I wonder why you don't simply create a unique Ticket class with two methods like registerUser() and bookTicket() (these names are pure fancy, be free to use yours).

Accessing Variables from MY_Controller CodeIgniter

As I have read through articles, the best possible way to validate a user in all controllers of my application is to extend my own Controller that extends CI_Controller
But I can't make it work.
What I have is a members_controller that extends my MY_Controller.
in MY_Controller i have this piece of code, just to test if how am I going to utilize this.
<?php
class MY_Controller extends CI_Controller {
private $foo;
function __construct() {
parent::__construct();
$this->foo = 'hello world again';
}
}
in my members controller
<?php
class Members extends MY_Controller {
function index() {
$this->load->view('members');
}
}
in my members view
i want to access this variable i just set up, but i don't know how.
i tried echoing it out like this echo $foo, like this $this->foo
but neither work. thanks much!!
You need to pass the variable to your view file.
<?php
class Members extends MY_Controller {
function index() {
$data = array('foo' => $this->foo)
$this->load->view('members', $data);
}
}
Now you can refer to $foo inside your view. You may need to declare $this->foo as protected.
There's a video tutorial on MY_Controller that you may find useful at http://codeigniter.tv/a-10/Extending-the-core-MY_Controller-and-beyond
Have you tried passing the variable to the 'members view'.
$this->load->view('members',$foo);
You need to make $foo as protected and not private.
Then you will be able to access it in your controller that extends MY_Controller.php like this:
$this->foo
Today for the first time I used codeigniter 3. In the past I used version 2.x a lot of times. Now I can't extend CI_Controller:
application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
private $data = array();
function __construct() {
parent::__construct();
date_default_timezone_set("Europe/Zagreb");
$this->data['foo'] = "bar";
}
application/controllers/Welcome.php
class Welcome extends MY_Controller {
public function index() {
$this->data['main_content'] = 'home';
$this->load->view('template/template', $this->data);
}
application/view/home.php
(this is view loaded by $this->data['main_content'])
If I try to echo any data declared in MY_Controller, I get an error.
<?php echo $foo; ?>
A PHP ERROR WAS ENCOUNTERED
Severity: Notice
Message: Undefined variable: foo
Filename: template/header.php

How to Inherit A Model from Another Model in CodeIgniter

i'm using codeigniter for my project and i have this class model which i call Genesis which looks like this:
class Genesis_model extends CI_Model {
function __construct() {
parent::__construct();
}
function get() {
return 'human soul';
}
}
and i have another model, stored in the same directory, which extends Genesis_model
class Human_model extends Genesis_model {
function __construct() {
parent::__construct();
}
function get_human() {
return $this->get();
}
}
Human_model is used by Human controller
class Human extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('human_model');
}
function get_human() {
$data['human'] = $this->human_model->get_human();
$this->load->view('human/human_interface', $data);
}
}
if i execute the code, it will produce an error which point to return $this->get(). it reads "Fatal error: Class 'Genesis_model' not found in ...\application\models\human_model.php on line 2".
i use this method because nearly all my models shared almost identical structure. I gather the similar functionality in Genesis while the other models serve only as data suppliers unique to the tables they represent. it works well in my asp.net (vb.net) but i don't how to do it in codeigniter.
is there a way for Human_model to inherit Genesis_model. i don't think i'm allowed to use include('genesis_model.php'). i don't know if it works either.
thanks in advance.
core/MY_Model is good if there's only 1 important superclass for your models.
If you want to inherit from more than model superclass, a better option is to change your autoload configuration.
In application/config/autoload.php, add this line:
$autoload['model'] = array('genesis_model');
Put the file genesis_model.php in the core directory
Change your Human_model to this:
include('genesis_model.php');
class Human_model extends Genesis_model {
function __construct() {
parent::__construct();
}
function get_human() {
return parent::get();
}
}
notice the get_human function and the include.
You have to include the Genesis_model on your Human_model.php like this:
include_once( APPPATH . 'folder/file' . EXT );
Or you can autoload it on your config/autoload.php file, what I think is stupid =)
other solution
<?php
$obj = &get_instance();
$obj->load->model('parentModel');
class childModel extends parentModel{
public function __construct(){
parent::__construct();
}
public function get(){
return 'child';
}
}
?>

Resources