separating Model folder, view folder and controller folder in codeigniter

99封情书 提交于 2019-12-13 01:54:25

问题


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


回答1:


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.




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/17833876/separating-model-folder-view-folder-and-controller-folder-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!