1

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?

1
  • Try searching HMVC Codeigniter Commented Jul 24, 2013 at 12:53

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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
I highly recommend you to change your strategy, take a look at here for further info.
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');
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.
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);

Comments

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.