码迷,mamicode.com
首页 > 其他好文 > 详细

CodeIgniter学习笔记(二)——CI中的控制器

时间:2015-07-08 00:28:25      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

通过CI创建自己的工程,只需要将CI压缩包中的application目录、system目录和index.php文件拷贝到自己的工程目录就可以了。自己的代码完全在application目录中编辑,system目录不要修改,以后CI出了新版本的时候,只需要替换掉system文件的内容就可以了,如果自行修改,升级就会遇到麻烦。

 

拷贝完成后,通过URL打开新工程的首页:http://localhost:8080/testCodeIgniter/

技术分享

通过这个页面,CI提示我们当前展示的视图是在welcome_message.php文件定义的,当前使用的控制器是Welcome.php

打开/application/controllers/Welcome.php文件,这个文件只有一个index方法,方法中加载了视图welcome_message

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

class Welcome 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 http://codeigniter.com/user_guide/general/urls.html
     */
    public function index()
    {
        $this->load->view(‘welcome_message‘);
    }
}
?>

视图文件welcome_message.php在/application/views目录下

通过URL访问控制器使用pathinfo,格式为:协议://域名/入口文件/控制器/方法名,对于私有方法、保护方法或以下划线开头的方法,不能通过pathinfo访问

在上面的控制文件Welcome.php中新增test方法:

public function test()
{
    echo "这是Welcome控制器的test方法";
}

通过pathinfo(http://localhost:8080/testCodeIgniter/index.php/Welcome/test)就可以调用到Welcome控制器的test方法

新建一个user控制器,包括一个index方法

<?php
class User extends CI_Controller 
{
    public function index()
    {
        echo ‘user---index‘;
    }
}
?>

控制器需要从CI_Controller类继承

通过pathinfo可以访问user控制器的index方法:http://localhost:8080/testCodeIgniter/index.php/user/index

注:兄弟连视频中提到pathinfo中区分大小写,经过使用CI3.0版本测试,是不区分大小写的

CodeIgniter学习笔记(二)——CI中的控制器

标签:

原文地址:http://www.cnblogs.com/iamsupercola/p/4628886.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!