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.
Related
I have a forum I've put in the public directory and I want to make a SSO with the site and the forum. The problem is, when I try to get a user session using Zend's AuthenticationService class, it gives the following error :
Class 'Zend\Authentication\AuthenticationService' not found in C:\Users\Milen\Documents\site\public\forum\admin\sources\classes\session\ssoPublicSessions.php
What am I doing wrong ?
The code:
<?php
use Zend\Authentication\AuthenticationService;
class ssoPublicSessions extends publicSessions {
public function __construct() {
parent::__construct();
if (self::$data_store['member_id'] OR $this->_member->is_not_human) {
return;
}
$auth = new AuthenticationService();
$member = $auth->getIdentity();
if (!$member) {
return;
}
self::setMember($member['member_id']);
$this->_updateMemberSession();
return;
}
}
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`");
}
}
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 am try to use helperClass in Zend_Layout options.
I create a class:
class Helper_Testi extends Zend_Controller_Action_Helper_Abstract{
public function init(){
echo "111111111";
$this->fff = 'hello from Helper';
}
public function getMessage(){
echo "==============";
return "message";
}
}
and in Bootstrap.php try add it to Zend_Layout:
$options = array('layout' => 'layout','helperClass'=>'../application/controllers/helper/Testi');
$layout = new Zend_Layout();
$layout->startMvc($options);
But when I reload browser I see Exception:
Fatal error: Uncaught exception 'Zend_Exception' with message 'File "../application/controllers/helper/Testi.php" does not exist or class "../application/controllers/helper/Testi" was not found in the file'
What I do wrong? Help me please.
Try to use APPLICATION_PATH constant in your path
You need place your helper class in correct place.
E.g. in my bootsrap there are such lines:
protected function _initViewHelpers() {
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$view->addHelperPath('Icc/View/Helper' , 'Icc_View_Helper');
}
And helper files will be located in this folder: Icc\View\Helper\
Edit:
E.g. I have file FormDropdown.php
with such content:
class Icc_View_Helper_FormDropdown extends Zend_View_Helper_Abstract {
function formDropdown($name = '')
{
return "<select name='$name' id='$name'></select>";
}
}
In view I can use this helper in this way:
<?=$this->formDropdown('icc_info_salutation')?>