Okay, i am having this issue and google is not helping and i think it is because my issue is too simple or something.
I'm trying to load whatever or connect with load->database(); but it aint working.
Here is my code with comments, it will explain everything.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class CI_Lang {
function __construct() {
# Show all files included
print_r(get_included_files()); // There is nothing like loader or load .php
# Check for loader file
if( ! class_exists('loader') || ! class_exists('load')) {
echo 'no load '; // is being shown,
}
# Check Database connection before
if (isset($this->db->conn_id) && is_resource($this->db->conn_id)) {
echo 'database is loaded and conected ';
} else {
echo 'no connection '; // is being shown,
}
# Get Database connection
$this->load->library('database');
/*
Script dies on line above with error:
Fatal error: Call to a member function library() on a non-object in
application/core/Lang.php on line 38
*/
$this->load->database();
# Check Database connection after
if (isset($this->db->conn_id) && is_resource($this->db->conn_id)) {
echo 'database is loaded and conected ';
} else {
echo 'no connection ';
}
# Register as loaded
log_message('debug', 'JK_Lang Class initialized');
}
}
This script is named Lang.php and it is in application/core folder. It is supposed to override the main Lang class and it is. The problem is, it seems to be all alone. How can i include other methods here?
I already have modified autoload.php and set there:
$autoload['libraries'] = array(
# We need Loader
'loader',
# Connect with DB
'database',
# Session Start
'session'
);
And now i have ran out of ideas, any help please ?
The problem was, i had no codeigniter instance declared. So there were 2 methods to fix it.
First, i could make a function like:
function __construct() {
$this->run();
}
function run() {
/* All my __construct code here */
}
Or as i actually fixed my problem, i made a library and to get all things needed to work with codeigniter i made an instance:
class L {
var $CI;
function __construct() {
$this->CI =& get_instance();
$this->CI->load->database();
$this->test();
}
function test() {
$query = $this->CI->db->query("SELECT * FROM `stackoverflow.com`");
}
}
Related
I'm trying to override show_404 method from CI_Exceptions, but MY_Exceptions is never loaded.
MY_Exceptions is located at application/core/
It's code
<?php
class MY_Exceptions extends CI_Exceptions {
function show_404(){
header("HTTP/1.1 404 Not Found");
$CI =& get_instance();
$CI->load->view('header.php');
$CI->load->view('err/custom404.php');
$CI->load->view('footer.php');
}
}
When I call show_404() I get two Fatal error
Fatal error: Class 'MY_Exceptions' not found in C:\workspace\ictp-tv-main\system\core\Common.php on line 196
Fatal error: Class 'MY_Exceptions' not found in C:\workspace\ictp-tv-main\system\core\Common.php on line 196
I have other extended classes that work well, with the same prefix MY_
EDIT
After #Tpojka suggestion my code is
class MY_Exceptions extends CI_Exceptions {
public function __construct()
{
parent::__construct();
$this->CI =& get_instance();
}
public function show_404($page = '', $log_error = TRUE){
header("HTTP/1.1 404 Not Found");
$this->CI->load->view('header.php');
$this->CI->load->view('err/custom404.php');
$this->CI->load->view('footer.php');
}
}
And now the 404 page is blank.
EDIT
SOLUTION
We need to ECHO the error, not simple load view.
public function show_404($page = '', $log_error = TRUE){
header("HTTP/1.1 404 Not Found");
echo $this->CI->load->view('header.php',array('PAGE_TITLE'=>'Page not found'),true);
echo $this->CI->load->view('err/custom404.php',null,true);
echo $this->CI->load->view('footer.php',null,true);
exit(4);
}
Thanks #Tpojka to open my mind.
Try this way
<?
class MY_Exceptions extends CI_Exceptions
{
public function __construct()
{
parent::__construct();
}
public function show_404($page = '', $log_error = TRUE)
{
if (is_cli())
{
$heading = 'Not Found';
$message = 'The controller/method pair you requested was not found.';
}
else
{
$heading = '404 Page Not Found This Time';
$message = 'The page you requested was not found.';
}
// By default we log this, but allow a dev to skip it
if ($log_error)
{
log_message('error', $heading.': '.$page);
}
echo $this->show_error($heading, $message, 'custom404', 404);//custom404 is in APPPATH.'views/errors/html/custom404.php'
exit(4); // EXIT_UNKNOWN_FILE
}
}
I edited code above. I took research and ended with this. I just copied show_404() method from CI_Exceptions class and saw what changes could be done. You can set your message, your header and call your template as well. You can play this way. Also I would suggest you to place $this->load->view('header') and $this->load->view('footer') in custom404.php file itself. In file custom404.php file just echo $heading and $message.
I created a custom Library in CodeIgniter, and positioned it in application/libraries/VarMatrixSpecanimal.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class VarMatrixSpecanimal {
protected $variabiliMatrix;
public function __construct() {
$variabiliMatrix['cat']['no']=1;
$variabiliMatrix['dog']['a']=2;
$variabiliMatrix['bird']['b']=3;
}
public function get_matrix() {
return $this->variabiliMatrix;
}
}
?>
Then in one controller (application/controllers/certificate.php) method I added these two lines of code:
public function save1()
{
//..... some more code before
$this->load->library('VarMatrixSpecanimal');
$numerical_values = $this->varmatrixspecanimal->get_matrix();
//..... some more code after
But when I call save1 method, I get this error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Certificate::$varmatrixspecanimal
Filename: controllers/certificate.php
Line Number: 139
I don't understand where I do wrong, please help me.
I checked also CodeIgniter help http://www.codeigniter.com/user_guide/general/creating_libraries.html but I was not able to get my error
Add get_instance() function in constructor in VarMatrixSpecanimal.php
public function __construct()
{
$this->variabiliMatrix =& get_instance();
}
After this load library on your controller
$this->load->library('varmatrixspecanimal');
I have experienced anything like this because I forgot this line of code:
parent::__construct
in my __construct or constructor.
Add include file with correct path at the top of program will solve this issue.
include "../../VarMatrixSpecanimal.php";
Try to change your constructor from this:
public function __construct() {
$variabiliMatrix['cat']['no']=1;
$variabiliMatrix['dog']['a']=2;
$variabiliMatrix['bird']['b']=3;
}
to this:
public function __construct() {
$this->variabiliMatrix['cat']['no']=1;
$this->variabiliMatrix['dog']['a']=2;
$this->variabiliMatrix['bird']['b']=3;
}
Also, when loading your library i think you don't need to uppercase any letters
I have got this controller:
class Start extends CI_Controller{
var $base;
var $css;
function Start()
{
parent::Controller(); //error here.
$this->base = $this->config->item('base_url'); //error here
$this->css = $this->config->item('css');
}
function hello($name)
{
$data['css'] = $this->css;
$data['base'] = $this->base;
$data['mytitle'] = 'Welcome to this site';
$data['mytext'] = "Hello, $name, now we're getting dynamic!";
$this->load->view('testView', $data);
}
}
it tells me in this line:
parent::Controller(); //error here.
Call to undefined method CI_Controller::Controller()
If I remove that line..I get an error for the next line that says..
Call to a member function item() on a non-object
How do I prevent such errors form happening?
If you're using CI 2.x then your class constructor should look like this:
public function __construct()
{
parent::__construct();
// Your own constructor code
}
read more in user guide
In CodeIgniter 2, the constructor is named __constructor and not the class name. So you need to call parent::__construct() instead of parent::Controller()
Here's an article that you can read that shows one major difference between CodeIgniter 1.x and CodeIgniter 2.x
http://ulyssesonline.com/2011/03/01/differences-between-codeigniter-1-7-2-and-2-0-0/
If you're running your Codeigniter project via Xampp or a similar server add the following code to the bottom of your config.php file in the following directory; ci_project/application/config/config.php
function my_load($class) {
if (strpos($class, 'CI_') !== 0) {
if (is_readable(APPPATH . 'core' . DIRECTORY_SEPARATOR . $class . '.php' )) {
require_once (APPPATH . 'core' . DIRECTORY_SEPARATOR . $class . '.php');
}
}
}
spl_autoload_register('my_load');
The above code would help to; load class in core folder.
I'm certain this works in the following setup; CI-3+, Xampp, Php5.6, and or 5.6+
Also, you can then decide to create and allow other classes to reference your own Controller (which extends the original CI_Controller) by creating a file named MY_Controller.php in the following directory: ci_project/application/core/ and adding the following code in it;
<?php
class MY_Controller extends CI_Controller {
}
?>
That way you can always just reference or extend the other Classes to your own Controller (MY_Controller) throughout the rest of the project e.g.
class Admin extends MY_Controller {
//your function here
}
I hope this helps.
For my current project i decided to create a library for some common functionalities.
Ex : Login_check,get_current_user etc.
With my little knowledge i created a simple one but unfortunately its not working.
Here my library :
FileName : Pro.php and located in application/libraries
class Pro{
public function __construct()
{
parent::_construct();
$CI =& get_instance();
$CI->load->helper('url');
$CI->load->library('session');
$CI->load->database();
}
function show_hello_world()
{
$text = "Hello World";
return $text;
}
}
?>
And i tried to load it on my controller :
<?php
class Admin extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->library(array('session'));
$this->load->library("Pro");
}
function index()
{
echo($this->Pro->show_hello_world());
}
}
?>
I cant see any erros there...but i am getting a blank page.
Whats wrong with me ??
Thank you .
Edit : I got this error :
Call to a member function show_hello_world() on a non-object in C:\wamp\www\Project\application\controllers\admin.php on line 13
One thing I notice: remove the parent::__construct() from your library constructor, because it's not extending anything so has no parent to call.
Also, enable error reporting by setting the environment to "development" in index.php, and you might also want to raise the logging threshold to 4 in config/config.php so you log errors.
Try this simple test-case:
file Pro.php in application/libraries:
class Pro {
function show_hello_world()
{
return 'Hello World';
}
}
Controller admin.php in application/controllers
class Admin extends CI_Controller
{
function index()
{
$this->load->library('pro');
echo $this->pro->show_hello_world();
}
}
while your class name is capitalized, all your references to the library when loading it and using it should be lower case. you also do not need the constructor, as the other commenter mentioned.
so instead of:
echo($this->Pro->show_hello_world());
you should have:
echo($this->pro->show_hello_world());
I prefer the standard php autoloader approach, with this you dont need to change your classes at all, you can use your standard classes without modifications
say for instance you class is class 'Custom_Example_Example2' and is stored in libraries
in sub folders you can add this autoloader in the master index.php
make sure it is added below the defined APPPATH constant
//autoload custom classes
function __autoload($className) {
if (strlen(strstr($className, 'Custom_')) > 0 ||
strlen(strstr($className, 'Other1_')) > 0 ||
strlen(strstr($className, 'Other2_')) > 0) {
$exp = explode('_', $className);
$file = APPPATH.'libraries';
if(!empty($exp)) {
foreach($exp as $segment) {
$file .= '/'.strtolower($segment);
}
}
$file .= '.php';
require_once $file;
//debug
//echo $file.'<br />';
}
}
This will look for class calls matching the 'Custom_' prefix
and reroute them to the relative location in this case
you only need to define the base prefix not the sub folders / classes
these will be auto detected by this code
APPPATH.'libraries/custom/example/example2.php'
You can call it in the controller the standard php way
$class = new Custom_Example_Example2;
or
$class = new custom_example_example2();
You can modify the script to your liking currently it expects all folders and filenames in the library to be lowercase but you can remove the strtolower() function to allow multiple casing.
you can change the require once to echo to test the output by uncommenting this line and refresh the page, make sure you have a class init / test in the controller or model to run the test
echo $file.'<br />';
Thanks
Daniel
In Pro.php
class Pro{
protected $CI;
public function __construct() {
$this->CI = & get_instance();
}
public function showHelloWorld(){
return "Hello World";
}
}
In your controller
class Staff extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->database();
$this->load->helper(array('url_helper', 'url'));
$this->load->library("pro");
}
public function index() {
echo $this->pro->showHelloWorld();die;
}
}
Just do these things you can access your custom library in codeignitor.
I have been trying to implement the solution referenced at:
Error when trying to load view in my_controller
Don't know why, but Aptana indicates a syntax error on my line:
parent::load->view('common/header_out');
...which is seconded by the interpreter when I execute: "unexpected T_OBJECT_OPERATOR"
EDIT: Previously I tried using:
$this->load->view('common/header_out');
...which generated the following errors:
Warning: include(application/errors/error_php.php) [function.include]: failed to open stream: No such file or >directory in /home/uom2/www/system/core/Exceptions.php on line 167
Warning: include() [function.include]: Failed opening 'application/errors/error_php.php' for inclusion >(include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear') in /home/uom2/www/system/core/Exceptions.php on >line 167
I am using CI 2.0.
Any help would be appreciated.
application/core/Uom_Controller.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Uom_Controller extends CI_Controller
{
public $data = array(); // Array to store data - passed to views.
protected $view_path = null; // Here to overide the view path if n
public function __construct()
{
parent::__construct();
}
protected function check_session()
{
//Here goes your function
}
protected function render() {
$data['page_title'] = 'Your title';
$data['page_title'] = 'Your title';
parent::load->view('common/header_out');
parent::load->->view('home/home', $data);
parent::load->->view('common/foot');
echo "view rendered: ".date('Y-m-d H:i:s');
}
}
controllers/home/home.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends Uom_Controller {
function __construct()
{
parent::__construct();
// Set the view path manually for this method.
$this->view_path = 'home/home.php';
}
function __destruct() {
$this->render();
}
public function index()
{
echo "home updated";
}
public function out()
{
echo "logout";
echo "<br>";
echo $this->view_path;
echo "<br>";
}
}
obviously the intention is similiar to the SO post at the top of this post: I want the $this->render(); to be called and render my page header, body, and footer.
(My next task is to add the authentication check to this MY_Controller pattern, so I am hoping that whaever solution works for calling views also works on models.
Thanks everyone and I really have spent hours trying to find a solution to this beforw once again turning to SO.
I think your problem is you are calling parent::load->view instead of $this->load->view
Ugh. here is the problem:
function __destruct() {
$this->render();
}
According to a remark by Phil Surgeon on here
__destruct does not work as expected due to the way everything is
inherited.
...and further down the page, a more detailed explanation that seems to make sense:
The problem is that the controller IS the CI superobject, once you
destroy that, you lose all access to CI. And you don’t have any
control over the destruct process, so you don’t know what is still
accessable, and what isn’t.
Using __destruct() for anything other than cleanup actions for
destruction of the current object is a very bad idea…
The short answer is that things work so long as you are not calling a method from the extended class from "_destruct".
Hopes this helps someone else.