1

can i place the model and view folders of the MVC structure of codeigniter to different location irrespective to the regular path

application/views
application/models

to some other location, lets say

abc/views
pqr/models

outside the project folder. if possible then how can i achieve it.

thank you in advance

tereško
  • 58,060
  • 25
  • 98
  • 150
dhpratik
  • 1,100
  • 4
  • 19
  • 43

4 Answers4

3

There's no feature to customize the models and views path in CodeIgniter current stable versions (while in CI 3.x you can change the view path as well as application and system).

But you can load your files outside of the typical views and models folders.

The path to the file is relative. So you can use ../ to go one UP level in path.

For example, If the abc folder is placed near application, you should use ../../abc to reach to that folder.

Take a look at the example below:

Model:

class Model_name extends CI_Model {

    public function baz($value='')
    {
        return $value;
    }

}

Controller:

class Foo extends CI_Controller {

    public function bar()
    {
        $this->load->model('../../pqr/models/model_name');

        $data['var'] = $this->model_name->baz('Yes It Works!');

        $this->load->view('../../abc/views/view_name', $data);
    }

}

View:

<?php echo $var; ?>

Here is the sample folder structure:

application
system
pqr
   /models
          /model_name.php
abc
   /views
         /view_name.php

As a Side-note: Make sure direct accessing to the pqr or abc directories is restricted. add a .htaccess file inside them with the content of Deny from all.

Hashem Qolami
  • 97,268
  • 26
  • 150
  • 164
  • this solution is correct if placed on a single server. suppose if a developer is creating a controller on say `192.168.1.2/test/` and ist respective view is being made on say `192.168.1.3/xyz` what should i do for such a case. is it possible now – dhpratik Jul 24 '13 at 13:35
  • I highly recommend you to change your strategy, take a look [at here](http://stackoverflow.com/questions/2752783/including-php-file-from-another-server-with-php) for further info. – Hashem Qolami Jul 24 '13 at 13:46
  • i got ur concern. but i am planning to implement this only for development phase. i tried using the method stated in the link you gave of editing the php.ini file. still not working. i am calling the following way, `$this->load->view('http:/ /192.168.1.3/t2/welcome_message');` – dhpratik Jul 24 '13 at 14:17
  • Because that is not the way `CI_Loader::view();` works. CI tries to include view files from `APPPATH.'views/'` and lots of checks are done like `file_exists();` before including the view file. By the way, you can `include()` your files manually, but before that, `extract()` your passing `$data`. – Hashem Qolami Jul 24 '13 at 14:44
1

To customize models and views outside the 'application' folder, follow these easy steps,

  • Create My_Loader.php file in 'application/core' directory
  • Copy the following code into the custom My_Loader.php

    class MY_Loader extends CI_Loader {
    
    function mymodel($model, $folder = '',$vars = array(), $return = FALSE) {
    
        array_push($this->_ci_model_paths, "");
        parent::model($model);
    }
    
    
    function myview($folder, $view, $vars = array(), $return = FALSE) {
            $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH . '../' . $folder . '/' => TRUE));
            return $this->_ci_load(array(
                    '_ci_view' => $view,
                    '_ci_vars' => $this->_ci_object_to_array($vars),
                    '_ci_return' => $return
            ));
    }
    
    • Save the file and in the controller, call and load the model (which resides outside the application folder) as:

    $this->load->mymodel('folder/model');

and for the view,

$this->load->myview('views','view_dir/view-php-file', $data);
Aleksander Lidtke
  • 2,876
  • 4
  • 29
  • 41
aeroarunn
  • 9
  • 2
1

There's no feature to customise the models and views path in CodeIgniter current stable versions (while in CI 3.x you can change the view path as well as application and system) by default.

But you can do this by changing it in file {CI DIR}/system/core/Loader.php and in the main index.php file.

Take a look at the example below:

Modify the file Loader.php in CI 3.x

The line 80 reads

protected $_ci_model_paths =    array(APPPATH);

Change it to

protected $_ci_model_paths =    array(MODELPATH);

And in the main index.php file, add

$model_folder = 'pqr';

and

define('MODELPATH', $model_folder);

to customise the views folder path, already given in CI 3.x

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
0

I am not sure that you can move views and models to different locations but you can change the location of application folder to the location of your choice.

You can move your application directory to different location and then open your index.php file and set the $system_folder and $application_folder variables with the new path values, preferably with a full path, e.g. '/www/MyUser/system'.

Reference: http://ellislab.com/codeigniter/user-guide/installation/index.html

Hope this is helpful.