CodeIgniter Forums
Does the view exsist? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Does the view exsist? (/showthread.php?tid=34623)



Does the view exsist? - El Forum - 10-05-2010

[eluser]gevans[/eluser]
Hey guys,

I'm currently building a large static site. I have a header and footer view for obvious reasons. The main view(s) are going to be stored in sub-folders to mimic the uri. This is being done as following;

Code:
$this->load->view($this->uri->uri_string());

The only issue is that, if someone mistypes or follows a bad uri they get the "Unable to load the requested file" error.

Is there a way to check a view exsists before attempting to load it?

Cheers,
gevans


Does the view exsist? - El Forum - 10-05-2010

[eluser]gevans[/eluser]
Ok, I went with the following solution;

Code:
$uri_string = $this->uri->uri_string();
$this->load->helper('file');
if(read_file('./' . $uri_string . '.php') === false)
{
    show_404($uri_string);
}
else
{
    $this->load->view($uri_string);
}

If anyone thinks of anything more suitable let me know!


Does the view exsist? - El Forum - 10-05-2010

[eluser]n0xie[/eluser]
You could just use file_exists()...


Does the view exsist? - El Forum - 10-05-2010

[eluser]gevans[/eluser]
That is very true, finished code (to date);

Code:
$uri_string = $this->uri->uri_string();
$uri_string = empty($uri_string)? 'home.php' : $uri_string .'.php';
if(file_exists('./application/views/' . $uri_string) === false)
{
    show_404($uri_string);
}
else
{
    $this->load->view($uri_string);
}