I put the line
$route['404_override'] = 'my404';
in the routes.php file, and I made a controller with a name my404:
<?php
include 'page.php';
class My404 extends Page {
private $page;
public function __construct() {
$page = new stdClass();
parent::__construct();
}
public function index() {
// $this->page->page_title = "Page not found!";
$this->page_view($this->load->view('my404', $this->page, true));
}
}
?>
and I made a View with a name of my404
It is working fine when I make a syntax error in the controller name, but it won't work if I write a wrong method and a Server error will occur instead of the customized 404 page, have I missed something in the routes.php file ?
From Codeigniter user guide:
This route indicates which controller class should be loaded if the requested controller is not found.
Apparently that rout will only work when controller isn't found.
you can instead override the default error_404 page at 'application/errors/error_404.php'.
Related
I have a Codeigniter Project where i add this to my routes.php File
Routes.php
$route['default_controller'] = "site/landing";
$route['404_override'] = 'site/landing/page_not_found';
My directory structure is as
controllers --- site -- landing.php
Landing.php
class Landing extends MY_Controller {
public function index(){
# Some Code Here
}
public function page_not_found(){
#load 404 page Here..
}
}
My_Controlelr.php
class MY_Controller extends CI_Controller {
#Some COde Here..
}
Now when i go to some non existing URL like mydomain.com/ajsdhajksbcasbkjakjghdakjsdh
I get this error
Please make sure the controller specified in your Routes.php file is
valid.
How can I set a "default" view and/or controller so it will be always called no matter the route?
I have tried with:
(routes.php)
Route::get('/*', function(){
return View::make('master');
});
and:
Route::get('*', function(){
return View::make('master');
});
But gives me NotFoundHttpException;
Related question: Is there a better way to do this than setting it in the routes.php?
Thanks!
Laravel have something called Controller Layouts, then in your controller you can create a variable named $layout and set it to a master or default view:
class Controller extends BaseController
{
protected $layout = 'layout.master';
public function getIndex() {
$data[];
$this->layout->content = View::make('myview', $data);
}
Now in the layout.master view you can access the $content variable.
I am trying to learn more about CI. Yesterday I tried to implement MY_Controller.php. I read the instructions in user guide But I could not get what is the advantage of it? And one more thing I did not understand the idea use it.
I have written application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
protected $data = array();
function __construct() {
parent::__construct();
}
function render_page($view) {
//do this to don't repeat in all controllers...
$this->load->view('templates/header', $this->data);
//menu_data must contain the structure of the menu...
//you can populate it from database or helper
$this->load->view($view, $this->data);
$this->load->view('templates/footer', $this->data);
}
}
This my home controller application/controllers/home.php
class Home extends MY_Controller {
public function view($page = 'home')
{
$this->load->helper('text');
$this->data['records']= $this->services_model->getAll();
if ( ! file_exists(APPPATH.'/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->render_page('pages/'.$page)
}
}
and my view is in in my application/views/pages/home.php.
config/routes.php:
$route['default_controller'] = 'home/view';
$route['(:any)'] = 'home/view/$1';
Now I get 404 error. My questions are:
1) why do I get 404 error?
2) if I add the about page, am I supposed to add a new controller or use the home controller?
MY_Controller Controller put into the application/core folder
Then use
MY_Controller folder path application/core/MY_Controller.php
**Home Controller ** folder path application/controllers/Home.php
class Home extends MY_Controller
to
class Home extends CI_Controller
The reason you're getting a 404 is, you did not have an index function. CodeIgniter by default loads the index function when a controller is called.
It doesn't really matter, but I would suggest you to open a new controller.
It is now working. I fixed by a clean directory structure. But i cant manage to see template/header and footer in the source code.
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.
I'm working with code igniter and for some reason, the url http://mysite.com/account/100 gives me a 404 error but http://mysite.com/account actualy works. Here's what my controller account.php looks like.
class account extends Controller {
function account()
{
parent::Controller();
}
function index()
{
echo 'hello';
echo $this->uri->segment(2);
}
}
Any idea what's wrong?
I just tested a simple account class like you have and it is trying to call 100 as a method of account, instead your URL after index.php should be account/index/100 and it works fine.
This can be solved using routing for example.
$route['account/(:num)'] = "accounts/index/$1";
is what you are looking for. You can look over the URI Routing user guide
for more info.
CodeIgniter requires that your controller name be capitalized, and the filename lowercase so try changing your controller to.
class Account extends Controller { }
and your filename account.php
Add this route and you are good
$route['account/(:any)'] = "account/index/$1";
This is not working because when you request http://example.com/account, it looks index() method.
But when you request http://example.com/account/100, is looking for 100() method. Which is not present.
You may tweak the code like this.
class account extends Controller {
public function account()
{
parent::Controller();
}
public function index()
{
echo 'hello';
}
public function id(){
echo $this->uri->segment(2);
}
public function alternative($id){
echo $id;
}
}
you'll call url like this: http://example.com/account/id/100
or you can do like this
http://example.com/account/alternative/100