Codeigniter 使子目录控制器工作 [英] Codeigniter making sub-directory controllers work

查看:16
本文介绍了Codeigniter 使子目录控制器工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让页面加载我想要的控制器.

I'm having trouble making the page load the controller I want it too.

例如,我希望我的网站加载为 localhost/sitename/catergory1/catergory2,其中 catergory 1 是它自己的控制器,2 是一种方法.所以我尝试将其添加到我的 routes.php 中:

For example I want my site to load as localhost/sitename/catergory1/catergory2 where catergory 1 is its own controller and 2 is a method. So i've tried adding this to my routes.php:

$route['catergory1'] = 'catergory1/cat1';

我的控制器文件设置为:
-控制器
   -Home.php
   -catergory1 <- 子文件夹
      -cat1.php
我认为这会导致 codeigniter 在 catergory 1 文件夹中加载控制器cat1",但当我转到 localhost/sitename/catergory1 时,它只会加载我的默认控制器home".我试过把它放在 routes.php 的 uri 路由和保留路由部分,但它仍然不起作用.这可能真的很简单,但我对此很陌生.

with my controller file set up as:
-Controller
   -Home.php
   -catergory1 <- subfolder
       -cat1.php
which I thought would cause codeigniter to load the controller 'cat1' inside the catergory 1 folder but instead when I go to localhost/sitename/catergory1 it just loads my default controller 'home'. I've tried putting it in both the uri routes and reserved routes part of routes.php but it still won't work. It's probably something really easy but im new to this.

这里是控制器本身,以防万一它与它们有关:
家庭控制器:

Heres the controllers themselves just incase its something to do with them:
home controller:

class Home extends CI_Controller {  
    function index() {  
        $this->load->view('home');  
    }
}

Cat1 控制器:

class Cat1 extends CI_Controller{  
    function index() {  
        $this->load->view('cat1');  
    }  
}

我只是愚蠢而错过了一些简单的东西吗?
谢谢.

Am i just being stupid and missing something easy?
Thanks.

推荐答案

顺便说一下,您在 category1 中有一个拼写错误.

You have a typo in category1 btw.

如果我按照正确的方式,你有这个结构:

If I follow correctly you have this structure:

 controllers
 - home.php
  category1 
  - cat1.php

如果是这样,您对自己的路线感到困惑:

If so, you confused yourself with your route:

$route['category1 '] = 'category1/cat1/index';

需要定义方法index,这是基于你的'cat1.php'文件:

Need to define the method index, this is based on your 'cat1.php' file being:

class Cat1 extends CI_Controller{
   function index() {
      $this->load->view('cat1');
   }
}

我写过这个,因为人们通常对 CI 中的路由感到困惑:
http://blog.biernacki.ca/2011/12/codeigniter-uri-routing-issue-with-controller-folders/

I wrote about this, as people usually get confused with routes in CI:
http://blog.biernacki.ca/2011/12/codeigniter-uri-routing-issue-with-controller-folders/

这篇关于Codeigniter 使子目录控制器工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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