Codeigniter: Extending CI_Exceptions not working in 2.2.1 - codeigniter

I have an issue with my custom 404 page in codeigniter. The below code works fine in 2.2.0 but having upgraded to 2.2.1 i'm getting the following error:
Fatal error: Call to a member function helper() on a non-object in /application/core/MY_Exceptions.php on line 14
MY_Exceptions.php (/library/core/MY_Exceptions.php)
class MY_Exceptions extends CI_Exceptions {
public function __construct() {
parent::__construct();
}
public function show_404() {
$CI =& get_instance();
$CI->load->helper('form');
$CI->load->helper('text');
$CI->load->model('Pages_model');
$data['nav'] = $CI->Pages_model->getPages();
$CI->output->set_status_header('404');
$CI->load->view('header', $data);
$CI->load->view('error404', $data);
$CI->load->view('footer', $data);
echo $CI->output->get_output();
exit;
}
}
I thought maybe it should now be located in /application/libraries but when placed in here I just get the standard codeigintor 404 error page?
What am I missing?

Related

Autoloading form_validation library

I am having problem autoloading the form_validation library in Codeigniter. I have a controller Posts with a create function which works great.
public function create(){
$data['title']='New Post';
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
}
Now I want to do form validation . Something like this :
public function create(){
$data['title']='New Post';
$this->form_validation->set_rules('title', 'Title','trim|required|min_length[5]|max_length[128]');
$this->form_validation->set_rules('body', 'Blog','trim|required|min_length[5]');
if($this->form_validation->run==FALSE){
$data['errors']=validation_errors();
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
}else {
$this->load->view('templates/header');
$this->load->view('posts/success', $data);
$this->load->view('templates/footer');
}
}
For now I am not calling any model to store the data, just showing the success message by loading posts/success view . However , even before I code the create function with the validation(that is the above code) , the moment I add form_validation in the autoload.php (even with the first code) like so :
$autoload['libraries'] = array('database','form_validation');
I am getting the below error :
Message: Undefined property: Post_model::$load
Filename: libraries/Form_validation.php
Line Number: 147
Backtrace:
File: C:\xampp\htdocs\ciblog\index.php Line: 292 Function:
require_once
I do not understand the error as I am not even using the Post_model in the method .
My Post_model.php is :
class Post_model extends CI_Controller {
public function get_post($slug=NULL){
if(!$slug) {
$query = $this->db->get('posts');
return $query->result();
} else {
$this->db->where('slug',$slug);
$query = $this->db->get("posts");
return $query->result();
}
}
}
My complete post controller is this :
<?php
class Posts extends CI_Controller {
public function index(){
$data['title']='Latest Posts';
$posts=$this->post_model->get_post();
$data['posts']=$posts;
$this->load->view('templates/header');
$this->load->view('posts/index',$data);
$this->load->view('templates/footer');
}
public function view($slug){
$posts=$this->post_model->get_post($slug);
if(empty($posts)){
show_404();
} else {
$data['posts']=$posts;
$this->load->view('templates/header');
$this->load->view('posts/view',$data);
$this->load->view('/templates/footer');
}
}
public function create(){
$data['title']='New Post';
$this->form_validation->set_rules('title', 'Title','trim|required|min_length[5]|max_length[128]');
$this->form_validation->set_rules('body', 'Blog','trim|required|min_length[5]');
if($this->form_validation->run()==FALSE){
$data['errors']=validation_errors();
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
}else {
$this->load->view('templates/header');
$this->load->view('posts/success', $data);
$this->load->view('templates/footer');
}
}
}
I have autoladed the model which is working .I searched other posts , most were to do with the first letter of the model name being not in caps which is not my case . Could somebody please make me understand whats going wrong ?
You used your model in your controller without loading it, you have to load it first:
$this->load->model('post_model');
Another error here is that model extends CI_Model:
class Post_model extends CI_Model
As the documentation says ref you have to validate like this :
I have seen mistake behind the run() method
Please fix it with :
if ($this->form_validation->run() == FALSE)
Also you can check by loading form_validation library directly by updating your constructor
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form');
}

CodeIgniter - Call to a member function ... on a non-object from Model [duplicate]

This question already has answers here:
Call to a member function on a non-object [duplicate]
(8 answers)
Closed 9 years ago.
I'm using CI 2.1.3 and I'm having issues with loading models and referring to the CodeIgniter "super object".
For example:
I'm trying to log in using my Login controller:
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Users_model');
}
public function validate_credentials() {
$this->load->library('form_validation');
// Some form validation here...
$query = $this->Users_model->validate();
if($query) { // if the user's credentials validated...
// something
} else {
// something
}
}
And when it gets to my Users_model:
class Users_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function validate()
{
$this->db->where('active', 1);
$this->db->where('email', $this->input->post('email'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('user');
if($query->num_rows == 1)
{
return $query;
}
}
}
I get an error "Fatal error: Call to a member function where() on a non-object in users_model.php on line XX" referring to the first line of validate() function.
I can get it working by using double colon (::) operator in Login controller like Users_model::validate() but I think I shouldn't need that.
I can also get it working by writing something like:
$ci = get_instance();
$ci->db->where...
at the start of the Users_model->validate() function, but I think I shouldn't need to do that either.
The database class is being loaded in autoload.php.
So the problem is that $this in my models refers to the model class itself rather than the "super object" it's supposed to. I have no moves left and I'm guessing it's about something very simple but I just can't see it. Please help me.
Please try by loading CI library like this
$this->load->library('database');
in __construct() function before loading model:
or load this library from autoload file like:
$autoload['libraries'] = array('database','form_validation', 'session');

Codeigniter HMVC Modular Error

I'm new to CI + MX and I tried the Modules::run(); style but I can't seem to let it work.
Here's my directory structure:
/application
-/modules
--/main
---/controllers
----main.php
---/models
---/views
--/connections
---/controllers
----connections.php
---/models
----/group_model.php
---/views
----connection_view.php
main.php controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends MX_Controller {
function __construct(){
parent::__construct();
$this->load->helper('url');
}
function index(){
echo modules::run('connections/load_connections');
}
}
?>
connections.php controller:
<?php
class Connections extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->model('connections/group_model');
}
function load_connections(){
$user_id = 2;
$data['tabs'] = $this->group_model->get_groups($user_id);
$this->load->view('connection_view', $data);
}
}
?>
group_model.php model:
class Group_model extends CI_Model{
function __construct(){
parent::__construct();
}
/**
* Get all groups in db
**/
function get_groups($user_id){
$this->db->select('g.group_name');
$this->db->from('groups AS g');
$this->db->join('members AS m', 'g.group_id = m.group_id');
$this->db->where('m.user_id', $user_id);
return $this->db->get()->result_array();
}
}
My connection_view.php view contains a div and some php codes to display the $data['tabs'] passed as array in load_connections function.
The problem is, I'm getting an error that says:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Connections::$group_model
Filename: controllers/connections.php
Line Number: 14
and
Fatal error: Call to a member function get_groups() on a non-object in C:\xampp\htdocs\hmvc\application\modules\connections\controllers\connections.php on line 14
I have clearly followed all the instructions provided on MX wiki at https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home
and set up everything as needed.
My database.php under /application/config is already configured.
routes.php is also configured pointing the default controller to main.php
I wonder what I have missed or have done wrong that I can't get it to work.
Codeigniter version: 2.1.3
MX version: 5.4
almost missed it, extend connections with MX_Controller since you are calling modules::run(). Whenever you want a controller to be called with modules::run(), you extend it with MX_Controller instead of CI_Controller
Your second error is caused because of your first error.
Your first error is most likely caused because it cant open the group_model.
try this
$this->load->model('group_model');

Custom 404 not work when try to call not function in controller

i try to use custom 404 page from this tutorial http://maestric.com/doc/php/codeigniter_404
my controller error.php :
class Error extends Controller{
function error_404()
{
$CI =& get_instance();
$CI->output->set_status_header('404');
echo "error bro";
}
}
when i open link localhost/mading/admin/blablabla , where there is not function blablabla() in controoler admin. the output : “error bro”
i try change line in method error_404() become the code below,
class Error extends Controller{
function error_404()
{
$CI =& get_instance();
$CI->output->set_status_header('404');
$data['title'] = "404 Page Not Found";
$data['body'] = $CI->load->view('web/404','',true);
$CI->load->view('web/web_page',$data);
}
}
but, when i open link localhost/mading/admin/blablabla , where there is not function blablabla in controller admin. the output : blank page.
the function error_404 not load the view . why the 404 page not load when i open controller admin/blablabla ??
thanks
Reading the comments of that tutorial, many people are having the same problem with that code. Did you change parent::CI_Router() on line 10 to parent::__construct()?
But why not just set the $route['404_override'] variable?
Just set a proper route in your config/routes.php:
$route['404_override'] = 'your_controller/show_404';
And the corresponding controller would look like that:
class Your_controller extends CI_Controller {
function __construct() {
parent::__construct();
}
function show_404() {
$this->output->set_header("HTTP/1.1 404 Not Found");
$this->load->view ('common/errors/404'); //wherever your view is
}
}
That should do the trick for you.

Pass data from library to controller and then to view in CodeIgniter 2

I'm trying to pass some data from my custom library to the controller and make it available in my view. For the life of me, I can't see where I'm going wrong.
I was going to get database results; for testing I've resorted to a simple array but it's still not working.
The library:
class Test{
public function __construct(){
$this->CI =& get_instance();
}
public function getStuff(){
$test = array('one','two','three');
return $test;
}
}
The controller:
function __construct(){
parent::__construct();
$this->load->library(array('form_validation', 'test'));
}
public function index(){
$data['q'] = $this->test->getStuff();
$this->load->view('index', $data);
}
The view:
var_dump($q); // returns Message: Undefined variable: q
It's late, so this could be a stupid error/typo. This is using CodeIgniter 2.1.0. I've Googled this - none of the solutions seem to work.
Thanks for taking a look!

Resources