2017-03-18 128 views
2

我想在这些日子学习codeigniter,并且我正在努力根据路由理解视图,控制器和模型的结构以保持安全性。CodeIgniter:从子文件夹访问视图

那么,我们应该如何构建我们的视图,控制器和管理后端和用户后端模型,我的意思是文件夹,子文件夹和路由?

首先我想何况我怎么把我的文件,以便: 我的路线:

$route['default_controller'] = 'site/home'; 
$route['home'] = 'site/home'; 

所以这里默认的控制器Site.php:

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 

class Site extends CI_Controller { 

/** 
* Index Page for this controller. 
* 
* Maps to the following URL 
*  http://example.com/index.php/welcome 
* - or - 
*  http://example.com/index.php/welcome/index 
* - or - 
* Since this controller is set as the default controller in 
* config/routes.php, it's displayed at http://example.com/ 
* 
* So any other public methods not prefixed with an underscore will 
* map to /index.php/welcome/<method_name> 
* @see https://codeigniter.com/user_guide/general/urls.html 
*/ 
public function index() 
{ 
    $data['body']='site/home'; 
    $this->load->view('includes/template',$data); 
} 

public function home(){ 

    $data['body']='site/home'; 
    $this->load->view('includes/template',$data); 

} 

public function AnotherMethod(){ 

    $data['body']='site/AnotherPage'; 
    $this->load->view('includes/template',$data); 

} 

而我的观点是模板(views/includes/template.php):

<?php 
//load head 
$this->load->view('includes/header'); 

//load body 
$this->load->view($body); 

//load footer 
$this->load->view('includes/footer'); 

?> 

所以,如果我把这样的东西,我有问题从子文件夹访问视图。作为一个例子,

directory : views/site/userbackend/index.php 
or views/site/adminbackend/index.php 

,也有根据管理后台和用户后端控制器和模型子文件夹。

我将如何访问它们?

我在这里只是想显示什么样的我有.. 所以现在所有的问题的答案可以,如果你只告诉我到场景的专家怎么办和结构部件有哪些亟待解决的问题是最佳实践。

我知道我已经使这个问题在许多事情上很复杂,我很抱歉,如果你想让我对一个主题更清楚,告诉我,我会更新我的问题。

需要帮助的人在这里。 在此先感谢!

+0

http://developers.ph/codeigniter/hmvc-pattern-codeigniter-php-framework/这可能有帮助 –

+0

@MalikMudassar感谢您的链接,我会研究它。 –

回答

0

的观点,都是提供给所有控制器,如果:

  1. 查看文件是否存在
  2. 您指定了正确的路径

对于views/site/userbackend/index.php应该$data['body'] = 'site/userbackend/index';


如果我简单的项目,我有这样的结构:

controllers 
    backend 
     Admin.php 
     Admin_users.php 
     ... 
    Main.php 
    News.php 
    ... 
views 
    backend 
     main 
     header.php 
     footer.php 
     users 
     view_list.php 
     view_form.php 
    frontend 
    main 
     header.php 
     footer.php 
     sidebar.php 
    news 
     view_list.php 
     view_item.php 
    mainpage.php 
    ... 

在view_list.php:

$this->load->view('frontend/main/header'); 
<news list> 
$this->load->view('frontend/main/footer'); 

我练HMVC。

+0

在你的情况下,你能简单地告诉我如何请求管理员控制器在路由的帮助下加载后端视图(view_list.php)吗? –

+0

CodeIgniter中的路由仅适用于控制器。在我的项目中,我总是使用直接路径进行查看。 但是,如果你愿意,你可以做你自己的路由查看。您甚至可以使用函数视图()和条件将CI_Loader扩展为MY_Loader。如果你想知道当前的控制器和方法: '$ router =&load_class('Router'); $ controller = $ router-> fetch_class(); $ method = $ router-> fetch_method();' –