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

经过CI建立本身的工程,只须要将CI压缩包中的application目录、system目录和index.php文件拷贝到本身的工程目录就能够了。本身的代码彻底在application目录中编辑,system目录不要修改,之后CI出了新版本的时候,只须要替换掉system文件的内容就能够了,若是自行修改,升级就会遇到麻烦。php

 

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

`4D%7NDFSY1V}96]HU_K5]S

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

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

<?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目录下codeigniter

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

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

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

经过pathinfo(http://localhost:8080/testCodeIgniter/index.php/Welcome/test)就能够调用到Welcome控制器的test方法this

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

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

控制器须要从CI_Controller类继承spa

经过pathinfo能够访问user控制器的index方法:http://localhost:8080/testCodeIgniter/index.php/user/index

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

相关文章
相关标签/搜索