在codeigniter中分离模型文件夹,视图文件夹和控制器文件夹 [英] separating Model folder, view folder and controller folder in codeigniter

查看:139
本文介绍了在codeigniter中分离模型文件夹,视图文件夹和控制器文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以将codeigniter的MVC结构的模型和视图文件夹放置到不同的位置,而不考虑常规路径

  / views 
application / models

到某个其他位置,让说

  abc / views 
pqr / models

$ b b

在项目文件夹之外。


如果可能的话,我将如何实现它。



提前谢谢。

解决方案

没有功能可以自定义CodeIgniter当前稳定版本中的模型视图路径(在CI 3.x中,您可以更改视图路径以及应用程序

但您可以在典型的视图之外加载您的文件 c $ c>和模型文件夹。



文件的路径是相对的。因此,您可以使用 ../ 在路径中移动一个 UP 级别。



例如,如果 abc 文件夹放置在应用程序附近,则应使用 ../



查看下面的示例:



模型:

  class Model_name extends CI_Model {

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

}

strong>

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

}

strong>

 <?php echo $ var; > 

这是示例文件夹结构:

 应用程序
system
pqr
/ models
/model_name.php
abc
/ views
/view_name.php






strong>侧键:确保直接访问 pqr abc 目录受到限制。在其中添加 .htaccess 文件,其内容为从全部中拒绝。

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

解决方案

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.

这篇关于在codeigniter中分离模型文件夹,视图文件夹和控制器文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆