简体   繁体   English

子目录中的CodeIgniter视图

[英]CodeIgniter Views within sub directories

Class

 public function load($page = 'resources') { if ( ! file_exists(APPPATH.'views/resources/'.$page.'.php')) { // Whoops, we don't have a page for that! show_404(); } $data['title'] = ucfirst($page); // Capitalize the first letter $this->load->view('templates/header', $data); $this->load->view('resources/multiplication'.$page, $data); $this->load->view('templates/footer', $data); } 

Directory 目录

 -Application --views ---resources ----multiplication -----selector.php 

I'm trying to load selector.php with localhost://resources/load/selector but it just shows a 404. I can't get the classes to work with sub directories in the view folders. 我正在尝试使用localhost:// resources / load / selector加载selector.php,但它只显示404。我无法使这些类与视图文件夹中的子目录一起工作。

If I move selector into /resources, it loads no problem. 如果我将选择器移至/ resources,则不会加载任何问题。 How can I get load method to load selector.php? 我怎样才能获得加载方法来加载selector.php?

That's because before you load your view you have a condition to load the 404 error. 这是因为在加载视图之前,您必须先加载404错误。 You should remove that condition or edit it to your real path: 您应该删除该条件或将其编辑为实际路径:

public function load($page = 'resources')
    {
         if ( ! file_exists(APPPATH.'views/resources/multiplication/'.$page.'.php')) //Just added the multiplication to make it the right path
    {
            // Whoops, we don't have a page for that!
            show_404();
    }

    $data['title'] = ucfirst($page); // Capitalize the first letter

    $this->load->view('templates/header', $data);
    $this->load->view('resources/multiplication'.$page, $data);
    $this->load->view('templates/footer', $data);

    }

You are simply missing a / before $page in both circumstances: 在两种情况下,您只是在$page之前缺少/

public function load($page = 'resources')
    {
         if ( ! file_exists(APPPATH.'views/resources/multiplication/'.$page.'.php')) //Just added the multiplication to make it the right path
    {
            // Whoops, we don't have a page for that!
            show_404();
    }

    $data['title'] = ucfirst($page); // Capitalize the first letter

    $this->load->view('templates/header', $data);
    $this->load->view('resources/multiplication/'.$page, $data);
    $this->load->view('templates/footer', $data);

    }

FYI as Eduardo alluded to, the show_404() isn't really necessary and is actually more confusing. 正如爱德华多(Eduardo)所暗示的那样,仅供参考, show_404()并不是必需的,而且实际上更加令​​人困惑。 In CI, if a view doesn't exist it will tell you that it doesn't exist. 在CI中,如果视图不存在,它将告诉您该视图不存在。 However, if you want to avoid such a message what you are doing is fine. 但是,如果您想避免这样的消息,您在做什么就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM