CodeIgniter CI框架前后台搭建------主控制器拆分为前后台控制器

CodeIgniter   CI框架前后台搭建------主控制器拆分为前后台控制器 终于找到的最好的方法了

在告诉你最好的方法的之前,先说一句话,这个是CI的特点就是这个,不要盲目视图改变。

如果想在mvc中分开前后台,最好的方法是方便在mvc三个目录下建好两文件 admin和home。访问是加上文件夹比如 $this->smarty->display('admin/index.html')。 你试试吧。

方法一 (最理想方法看方法三

http://www.phpwell.com/?p=535

方法二(不是很理想)

管理你的应用程序

默认情况下,你会将应用程序放入application/中,并且可能用 CodeIgniter 只管理这一个应用程序。当然,多个应用程序共享一个 CodeIgniter, 甚至对 application 文件夹进行重命名或更换路径也是可行的。

对应用程序文件夹重命名

如果你要对 application 进行重命名, 你需要打开 index.php 文件,对变量 $application_folder 进行更改:

$application_folder = "application";
更改你的应用程序的文件夹路径

你可以将 application文件夹从system 文件夹中挪放到服务器的其他的位置。但是你还要更改 index.php 文件里将$application_folder变量设置为服务器的全路径。

$application_folder = "/Path/to/your/application";
在一个 CodeIgniter 下运行多个应用程序

如果你想要多个应用程序共享同一个 CodeIgniter, 你要将 application 下所有的文件夹放在不同的应用程序的文件夹内。

例如,你要建立两个应用程序 "foo" 和 "bar",你的应用程序文件夹的结构可能会像下面的这样:

applications/foo/
applications/foo/config/
applications/foo/controllers/
applications/foo/errors/
applications/foo/libraries/
applications/foo/models/
applications/foo/views/
applications/bar/
applications/bar/config/
applications/bar/controllers/
applications/bar/errors/
applications/bar/libraries/
applications/bar/models/
applications/bar/views/
要选择使用某个应用程序,你需要打开主 index.php 文件,并且设置 $application_folder 变量为目标路径。例如,通过如下设置,就可以选择使用 "foo" 应用程序:

$application_folder = "applications/foo";
注意:  每一个应用程序都会需要它自己的index.php文件来调用他的目标程序。你可以随意对 index.php 文件进行命名。


到这并不是最好的 **********保证数据库配置的重用性*************************
方法三

<?php
class  Public_Controller extends  MY_Controller  {
    function __construct()  {
        parent::__construct();
        }
    }
?>

Admin_Controller.php:

<?php
class  Admin_Controller extends  MY_Controller  {
    function __construct()  {
        parent::__construct();
        }
    }
?>

这时候要注意在MY_Controller.php里把Public_Controller.php、Admin_Controller.php include进来,如果没有就会出现
Fatal error: Class ‘Public_Controller’ not found in xxx…的错误讯息。

<?php
class  MY_Controller  extends  Controller  {
    function __construct()  {
        parent::__construct();
        }
    }
// 把MY_Controller.php、Public_Controller.php、Admin_Controller.php都放在applicaton/libraries目录下。
include('Public_Controller.php');
include('Admin_Controller.php');
?>

另一个方法,是把这些controller class都写在MY_Controller.php

<?php
class  MY_Controller extends Controller  {
    function __construct()  {
        parent::__construct();
        }
    }
class  Public_Controller extends  MY_Controller  {
    function __construct()  {
        parent::__construct();
        }
    }
class  Admin_Controller extends  MY_Controller  {
    function __construct()  {
        parent::__construct();
        }
    }
?>


 

  

原文地址:https://www.cnblogs.com/itcx/p/CI-using.html