Welcome Guest, Not a member yet? Register   Sign In
Using 404_override with routes issue
#1

[eluser]Chromana[/eluser]
Ok so I have some confusion about how exactly to use the 404_override in routes.

This is my simplified routes file:
Code:
$route['(:any)'] = 'main_c';
$route['404_override'] = 'main_c/error_404';

I want to just be able to go to www.site.com/someName and have it load the page. If someName doesn't exist then the 404 page should be shown. However PHP errors are shown on the page instead of the 404 page.

In my main controller I have:
Code:
public function index() {
        $data['page'] = $this->uri->segment(1);
        if ($data['page'] == "")
            $data['page'] = "home";
        $this->load->view('main', $data);
}

And then in the main view file I have it load whatever the page was by using:
Code:
<?php  include($page . '.php'); ?>

I'm not really sure what I'm doing wrong here. I'm pretty sure it's because of that first line in the routes but I don't know what to change so that I can just have the index() of the controller load whatever page is given to it.
#2

[eluser]CroNiX[/eluser]
Read the very last thing (highlighted in pink) in the user guide for routing.
#3

[eluser]Chromana[/eluser]
It's true that I missed that. But reordering the routes so that default_controller and 404_override are at the top still produces the error where"A PHP Error was encountered" messages are shown on the page instead of the 404 page. For reference my routes looks like this after the change:

Code:
$route['default_controller'] = "main_c";
$route['404_override'] = 'main_c/error_404';
...
...
$route['(:any)'] = 'main_c';
#4

[eluser]Chromana[/eluser]
Also, I'd like to add that the tutorial shows the default_controller route as being last in the file (see the last code block on the page). I'm not sure who should be told about this inconsistency.
#5

[eluser]Aken[/eluser]
First, you don't even specify what PHP errors your getting, so start with that.

Second, doing the include() in your view file is ugly. If you already have a view file per page, why don't you just call those individual view files from your controller?

Third, you're never going to see the 404_override controller, because you've defined an (:any) route, which means any and all URIs will be routed to your main_c/index. 404_override works if a normal controller or route cannot be found. You either need to A) throw a 404 manually in your controller if the wildcard page does not exist, or B) don't use an :any route, and use the 404_override itself as the wildcard catch.

As it stands, you aren't doing any logic to check if a page exists or not. That's what you need to do, regardless of the solution you choose.
#6

[eluser]Chromana[/eluser]
Aken,

1.
The error is a simple "page not found" error:
Quote:A PHP Error was encountered
Severity: Warning
Message: include(pages/notARealFIleName.php) [function.include]: failed to open stream: No such file or directory
Filename: views/main.php
Line Number: 43

2.
Well I found a system which works pretty well (I don't know if it's the best... I haven't done too much back end stuff before). I basically have the main view file which has all of the structure of the general website. Within it are several include statements. E.g. one for the nav, one for the main content of the page (that's what the include statement in my OP was from), one for the footer etc. I guess it's just a case of me separating layout from content more.

And I know exactly the method you are describing. But I think it's nice to just be able to open the single main.php view file and be able to see the entire structure of a webpage minus the actual content. Also, I don't care about it being "ugly". However if this method causes performance issues I will consider other options!

3.
So, unless I'm mistaken, I need to either put a route for every single subpage or put a checking function into the index() of the controller. Is the PHP function
Code:
bool file_exists ( string $filename )
the best way to do this?
#7

[eluser]davidMC1982[/eluser]
The user guide has all the details for exactly what you are trying to do:

http://ellislab.com/codeigniter/user-gui...pages.html

You should also read the section on views so you don't have any of those includes() in your code (hint: you can load multiple views):

http://ellislab.com/codeigniter/user-gui...views.html

And here's the example controller for doing what you want:

Code:
public function view($page = 'home')
{
  
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
  // Whoops, we don't have a page for that!
  show_404();
}

$data['title'] = ucfirst($page); // Capitalize the first letter

$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);

}




Theme © iAndrew 2016 - Forum software by © MyBB