Showing header and footer in CodeIgniter custom error pages - php

In my CodeIgniter folder I have got custom error pages for each type of error in application/views/errors/html like error_404.php etc. So when I tried to update the layout and added <?php $this->load->view('header'); ?> on top then it started to show me errors like:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_Exceptions::$load
Filename: html/error_404.php
So I want to know how can I make it to work? I also tried to extends the Exception class from CI_controller but still no luck. Please help me to add my custom header to my custom error page. Thanks.
PS: I want to clear that even if I use some how PHP's include() function but I also want to use base_url function for e.g.
<script src="<?php echo base_url('assets/js/iconmenu.js'); ?>" type="text/javascript"></script> <br>
One more thing I want to clear that I am using Codeigniter 3.0 which provides all error's templates but don't allow to use $this->load function.

I don't know the reason for the above error. Might be it's because of core libraries are not loaded in case error 404 is happened.
You can try another way. See application/route.php and there is an option to override the 404 url, $route['404_override'] = '';
Example: Create a method error_404 in Welcome controller and update the route variable like $route['404_override'] = 'welcome/error_404';, now you can use whole helpers / libraries etc.
See routes
Hope this will help!

Related

CodeIgniter 4.0 shows me 404 error repeatedly

I wrote the corresponding controller through the CodeIgniter document, but the page always shows me 404. My question is why exactly the same URL can not be displayed, and why this framework can only show me 404 pages. I remember that ThinkPHP has a complete running link for error reporting, and I can't see the problem with only one 404.
this is my url
http://localhost/index.php/demo/test
this is my Demo.php code
`<?php
use CodeIgniter\Controller;
class Demo extends Controller
{
public function test()
{
echo 1;
}
}`
Demo. PHP files must be in the controller folder and there are no other directories, but CI reports 404 errors to me
Its because they dramatically changed routes, you need to define in \Config\Routes.php and check their wiki as I am in process of refactoring my scripts.
Add a route with this
$routes->get('pineapple','Demo::test');
ensure your server has mod_rewrite enabled and that you have .htacces file.
make sure that your base_url in app/Config/App.php is set with an ending slash on the url /

codeigniter 3.0 custom 404 not found error page

My problem is about custom error pages after upgrading to Codeigniter 3.0.
I used to have a Errors controller which handled 404 error page of the website - on this controller I have customized some parts of page, such as if user is logged in, it was showing username on the navigation bar and etc. (yes, on 404 error page).
After upgrading to CI 3.0, I have noticed that error pages are handled in different folder, I started to migrate to this way; but the problem is I can't load any variables to this page, such as session variables or can't even use any CI's functions on these pages.
I think this pages are supposed to be only HTML pages, but is there any way to load variables to these error pages?
You need to set in application/config/routes.php the following route
$route['404_override'] = 'my404';
Then you need to create a new controller in your controllers directory (application/controllers/)
<?php
class My404 extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->output->set_status_header('404');
$this->load->view('err404');//loading in custom error view
}
}
And create an err404 view. Thats all!
Refer :Reserved Routes
It's not working in codeigniter 3, I overwrite the default view (application/views/errors/html/error_404.php) and added my custom view, now show_404() is working fine.
that is working good, but if redirect to an error, for example 404 with function show_404, your controller wont load your view, for a real 404 in the show_404 method, you have to go to the core of codeigniter and touch it.
\system\core\Common.php
this is an example of code:
function show_404(){
$ci = get_instance();
$ci->output->set_status_header(404);
$ci->load->view('errors/error404_view');
echo $ci->output->get_output();
exit(4);
}
In Codeigniter 3, instead of calling show_404(), I just redirected to the custom error controller function.
e.g. for the above scenario
if (404 condition...) {
redirect("my404");
}
To work with the show_404 () function, you don't need to access the CI core and change how they are talking.
Just make the standard 404 view load your 404 view with $CI->load->view(...)
this will keep the original 404 code of the request and will not make an unnecessary redirect or core changes.
$CI =& get_instance();
$CI->load->view('error-404');

codeigniter helper function did not loaded

I try to "rebuild" again my web project in Codeigniter, but I totally forgot how to setting it..
In the last 3 years, this website contains no error and all works great. but when I try to reopen again, it contains errors like this :
Fatal error: Call to undefined function admin_info() in C:\xampp\htdocs\misnews\application\controllers\home.php on line 27
PS : the admin_info() functions are inside the helper, and I loaded it in autoload and added $this->load->helper('basic_helper); in the home controller
Can you help me?
Thanks
If your helper is named basic_helper.php
application / helpers / basic_helper.php
Then all you should need to do is $this->load->helper('basic');
You can also autoload helper in
application / config / autoload.php
When you need to use helper $this->basic->some_function();

Yii 2.0 static link to a controller

I'm very new to Yii 2.03 (and frameworks in general). I'm facing some problem now.
On the main page I have a menu on which I want to place (in the view) the link to the controller, but I don't know how, I'm getting " PHP Fatal Error – yii\base\ErrorException
Class 'CHtml' not found" or " PHP Fatal Error – yii\base\ErrorException
Class 'Html' not found"
http://localhost/web/index.php is the main page with the menu.
http://localhost/web/index.php?r=autori/index is the page with the generated CRUD, which works perfectly.
In the view I have <li>Autori</li> and I want to replace the '#' with a valid link, I don't care for SEO now. How can I edit the view to use the specific controller?
Thanks!
If you are indeed using Yii2, then try using:
<li>Autori</li>
You'll need to include this in the beginning of your view file, so it uses the correct namespace for the Url helper class:
<?php use yii\helpers\Url ?>
But pay attention to the framework version, as Sliq noted, CHtml is a Yii1 class.

CodeIgniter: show a view by typing its url

A newbie here: sorry if the question is too simple, been looking in tutorials but I don't seem to look properly.
I have the
$route['default_controller'] = "mainpage";
in routes.php working properly, and now I just want to view one of the php pages in views folder by typing its url:
http://myserver/folder/thepageIwantToSee
I get a 404 Not Found error. Do I have to use a controller? I just want to see it first before any linking, is there a way to do it?
Thanks a lot!
class yourcontroller extends CI_Controller {
function pageiwanttosee()
{
code
code
$this->load->view('the_view_page', $data);
}
}
Yes, you can write a controller that displays the view specified.
http://myserver/showfile/folder/thepageIwantToSee , where showfile is your controller.
Don't forget to set some security check or disable this on production site.

Categories