Welcome Guest, Not a member yet? Register   Sign In
Does the view exsist?
#1

[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
#2

[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!
#3

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

[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);
}




Theme © iAndrew 2016 - Forum software by © MyBB