I'm aware of the ways to set up custom error pages for expected errors. However, what I'd like to do is a set up a pretty, catch-all page that viewers will see on any error. So for example instead of "Unable to connect..." or whatnot, they get "We're sorry, an error occurred...".
Is there a view that can be modified or overridden to provide this functionality?
The error views are defined in /application/views/errors/html. There is one for php errors and one for exceptions. If you don't want to edit those you can extend the CI_Exceptions class and override show_php_error or what ever else you would like. If you wanted to try that you should create a file called "MY_Exceptions.php" in the /application/core/ folder and in it put....
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Exceptions extends CI_Exceptions {
public function show_php_error($severity, $message, $filepath, $line)
{
echo "Caught random php Error...";
}
}
Related
I am trying to implement tcpdf library in codeigniter but getting error
Fatal error: Cannot redeclare class Pdf in C:\xampp\htdocs\project\system\libraries\Pdf.php on line 6
here code example
path : "project\system\libraries\Pdf.php"
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once dirname(__FILE__) . '/tcpdf/tcpdf.php';
class Pdf extends TCPDF
{
function __construct()
{
parent::__construct();
}
}
/* End of file Pdf.php */
/* Location: ./application/libraries/Pdf.php */
also Download latest
TCPDF
also want to know which is best TCPDF or DOMPDF to generate quick PDF ?
When using/making custom libraries, smart move would be (as CI makers advise too) to put it in APPPATH.'libraries/' because of upgrading version could overwrite/delete added files from system directory. That is why application folder is used for - to make any kind of custom files related to CI framework not touching default files/directories.
Other than that, error you've got there says there is more than one or better say there is two classes with same name. PHP doesn't allow that and every used file and/or class must have unique name. Possible solution should be renaming your file to
BASEPATH.'Pdf_lib.php'
and class name accordingly.
But again, if you are not limited with something else, move your class to
APPPATH.'libraries/Pdf_lib.php'
with following directories and subdirectories and files of tcpdf and use it from there.
I fix this error using bellow method. its happen coz same class is call from other TCPDF included file here's the solution
path : "project\system\libraries\Pdf.php"
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once '/tcpdf/tcpdf.php';
if (!class_exists('Pdf')) {
class Pdf extends TCPDF
{
function __construct()
{
parent::__construct();
}
}
}
Codeigniter is showing blank page after adding comments in any method. eg add //redirect('home'); in controller index method.
Also error reporting can not be enabled from index.php
but after enabling log errors i get this error in error_log file
PHP Parse error: syntax error, unexpected 'endif' (T_ENDIF) in /home/username/public_html/project/application/controllers/Home.php on line 1
here is my controller
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function index(){
echo 'Home Page';
//redirect('home');
}
public function test(){
echo 'Test';
}
}
After spending hours and hours i figure out that there was some error on my FTP Client(FileZilla).
On uploading a file via FileZilla it convert all the code into one line therefor if there is any comments then shows error.
Change first line to :
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
As I am very pleased of the command show_404() which you can call everywhere to show a 404-Error-Page, I did want to implement a show_403() for requests without permissions.
I created the file application/core/MY_Exceptions.php and added the following code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Exceptions extends CI_Exceptions {
public function __construct()
{
parent::__construct();
}
public function show_403($page = '', $log_error = TRUE)
{
//do some stuff
echo "test";
}
}
Then I'll call it in a controller application/controllers/Welcome.php like this:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
// show_404(); // <-- this works!
show_403(); // <-- this works not!
}
}
and I keep getting the following error in the browser, when I access the controllers index method:
Fatal error: Call to undefined function show_403()
As you may have noticed I even tested this on a vanilla installation of CodeIgniter, so you should be able to reproduce this error with just these two files.
I know that I could load the extension manually, but that has not the elegance of using show_403() wherever and whenever I want...
Routing is set up correctly, CodeIgniter is version 3.0.3, PHP is version 5.6.12., filesystem permissions to application/core/MY_Exceptions.php have even been set to 777 for debugging purposes.
Your exception class is not loaded, so its giving that error.
Load your class like this
$excep = load_class('Exceptions', 'core', $this->config->item('subclass_prefix'));
echo $excep->show_403();// will echo test
One more suggesion is to use helpers for this,
Add your code in application/helpers with file name error_helper
public function show_403($page = '', $log_error = TRUE)
{
//do some stuff
echo "test";
}
Calling your function
$this->load->helper('error_helper');
echo show_403();
<?php
if (!defined('BASEPATH', ''))
exit('no direct script access allowed');
class Hello extends CI_Controller {
public function index() {
echo "this is my index function";
}
public function one() {
$this->load->view('one');
}
}
?>
I am the new user of codeIgniter.First i download code igniter software,then place it to the xampp/htdocs.Then i create a simple codeIgniter code which are show in the above.I don't understand where this code is to be save.I save this code in the Codeignitor/appilication.but it give me the below answer:
no direct script access allowed
What are the fault.please help me anyone.
Have you read the User Guide of Codeigniter? This is your Controller code and it should be placed inside appilication/controllers folder by name hello.php. See this.
Before starting of any framework it would be good if you understanding directory structure, and what is the role of files.
Where should keep HTML, CSS, JS and logic and model part.
As you creating controller then it should save under
application/controllers/ControllerName.php
Here is the guideline for controller
Also refer tutorials
http://tutorialcodeigniter.com
http://codesamplez.com/development/codeigniter-basic-tutorial
Open config.php within CodeIgniter\system\application\config.
Change base site url http://localhost/foldername
I have following code:
class MY_Router extends CI_Router {
function __construct() {
parent::__construct();
log_message('debug', "My Child Router Class is here");
} }
I have run the web page and saw log, but didn't find any log message there.
My purpose is that, instaed of default router, I want to use custom made router.
Can some one guide me what and where Iam doing wrong. And how it can be rectified.
Thanks in advance
If you are simply testing to see if the class if being constructed properly, it may be easier to just call an error message. If the error message displays then you're good to go.
show_error("My child router class is up and running.");
Also, ensure that your file is named "MY_Router.php" and is placed in your /application/core directory.
The user guide entry relating to extending core classes can be found at http://codeigniter.com/user_guide/general/core_classes.html