dhpratik
dhpratik

Reputation: 1138

Separating model folder, view folder and controller folder in CodeIgniter

Can I place the model and view folders of the MVC structure of CodeIgniter to different locations irrespective to the regular path?

Move:

application/views
application/models

to some other location, let's say:

abc/views
pqr/models

outside the project folder? If possible, how can I achieve it?

Upvotes: 1

Views: 5405

Answers (4)

Anshu Gupta
Anshu Gupta

Reputation: 11

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

Upvotes: 1

aeroarunn
aeroarunn

Reputation: 9

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

Upvotes: 1

Hashem Qolami
Hashem Qolami

Reputation: 99464

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.

Upvotes: 3

Prasad Paranjape
Prasad Paranjape

Reputation: 151

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.

Upvotes: 0

Related Questions