• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
external view folder ?


I'm looking for a possibility to set up a second view folder. This second folder should reside beneath the system folder in the toplevel. Right now, I'm using a view that simply includes another file (in the root folder level).

| |-config
| |-controllers
| |-errors

| |- views
| | |->reroute.php
|-templates <- second view folder

Is there a more elegant way to accomplish my task? The problem is that I want
to keep my users out of the System folder.

Thnx in advance

[eluser]Armchair Samurai[/eluser]
If you want to keep your users out of the system folder, why not move it above the docroot?

[eluser]Vince Stross[/eluser]
I have accomplished this by extending the Loader class. The following would allow you to call your views from anywhere. I call mine from another folder on the server. I do this because my CI application is used on many different sites and this way I can have a single code base.

create a file called, MY_Loader.php in your application/libraries folder with the following code:

class MY_Loader extends CI_Loader{

    function MY_Loader(){

    function my_view($view,$view_path,$vars=array(),$return=false){
        $file_ext = pathinfo($view,PATHINFO_EXTENSION);
        $view = ($file_ext == '') ? $view.EXT : $view;

            '_ci_path' => $view_path.'/'.$view,
            '_ci_vars' => $this->_ci_object_to_array($vars),
            '_ci_return' => $return

        return $this->_ci_load($data);

You would use this in your controller like so:

$this->load->my_view('<view file>','<view path>',$data,true/false);

The functionality is exactly the same as $this->load->view(); except you can specify a path to pull the view file from without hacking the core. The beauty of doing it this way is that you can still use the '$this->load->view()' function when you want to and use '$this->load->my_view()' when you want to get tricky! Wink

If you're not familiar with extending core libraries: just an FYI, there is no reason to explicity load anything - your function is available as long as the prefix is set right in the config.php file and your file name is exactly the same. (i.e. 'MY_Loader.php' (correct) versus 'my_loader.php' (wrong) or 'MY_loader.php' (wrong))


if you use linux you can do this from command prompt and it will just work.
cd /path/to/docroot
ln -sf /path/to/system/views templates
nootice it's creating a link called templates in docroot pointing to your view templates
in my application folder and my system folder (if you moved application out of system, otherwise just system)I have a h.taccess file with this in it:
option -indexes
RewriteEngine on
deny from all
this effectively blocks any entry to the system and application folders and if you have a folder in your application/views/assets you can add an htaccess file with the same as above only
Allow from all
this does everything you would want if I understand correctly with barely any code at all. Hope this helps. Have a wonderful day.

Wow, thnx for all the response.

@BeyondCiv: This looks like perfect! Thanks for that.

@yingyes: Unfortunatly I have no shell access to this server.

[eluser]Vince Stross[/eluser]
glad to help... just so you know though, you could still do what yingyes proposes by creating your links with PHP. I use shared hosting that runs on Linux and they told me I couldn't have shell access to create sym links.

Well, check the PHP docs at : http://us.php.net/manual/en/function.symlink.ph

The 'symlink()' function does it just fine! If you are interested in hard links, there's a function for that too. Read this doc page: http://us.php.net/manual/en/function.link.php.

I prefer to use the extended library method because it will work on any platform and doesn't require creating links when moving the application to a different server, etc.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.