CodeIgniter Forums
Access custom application folder from controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Access custom application folder from controller (/showthread.php?tid=59380)



Access custom application folder from controller - El Forum - 09-27-2013

[eluser]adamtong[/eluser]
Hi,

I followed the instructions in the use's guide.

At this page: http://ellislab.com/codeigniter/user-guide/tutorial/static_pages.html

You can find this code:

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

}

Because I choosed a different application and system folders, the file_exists test failes (Just by commenting that test the views show correctly).

In my opinion a solution would be to put the variable that containes the value of the application folder full path defined in index.php instead of putting just "application". But in this case that variable should be accessible from the controller.

How can I fix this?


Access custom application folder from controller - El Forum - 09-28-2013

[eluser]noideawhattotypehere[/eluser]
Code:
file_exists('./application/views/pages/'.$page.'.php')
notice the " ./ " at beginning, this will work.
if your application folder is renamed simply rename it here aswell... there is a constant like APPPATH or somehing like that, dont remember exactly.


Access custom application folder from controller - El Forum - 09-28-2013

[eluser]adamtong[/eluser]
Hi,

Adding the dot before did not fix the problem. The problem is that the function file_exists checks only the current directory or a full path. I fixed the problem by replacing that function by stream_resolve_include_path.

Thanks anyway


Access custom application folder from controller - El Forum - 09-28-2013

[eluser]CroNiX[/eluser]
Why not just use the CI constants defined at the bottom of index.php before it loads the bootstrap, which CI uses all over internally?
Code:
file_exists(APPPATH . 'views/pages/' . $page . '.php')

or better yet, specifically for views:
Code:
file_exists(VIEWPATH . $page . '.php')



Access custom application folder from controller - El Forum - 09-28-2013

[eluser]adamtong[/eluser]
Hi,

The VIEWPATH constant does not seem to be accessible from the controller.

Hewever the APPPATH solution that you proposed, works like a charm.

Tagged as solution.

Thanks