CodeIgniter默认视图

时间:2016-08-03 15:18:50

标签: php codeigniter

我有一个名为Page

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

class Page 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($page = 'home')
    {
        if ( ! file_exists(APPPATH.'views/'.$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($page, $data);
        $this->load->view('templates/footer', $data);
    }
}

但目前只通过转到http://www.example.com/

加载home.php页面

我试图这样做,如果我去example.com/test它会加载test.php

这是我的路线

$route['default_controller'] = 'page';
$route['(:any)'] = 'page/$1';

我是php框架的新手,我不知道如何实现这一目标。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:3)

更改

$route['(:any)'] = 'page/$1';

$route['(:any)'] = 'page/index/$1';

答案 1 :(得分:0)

Codeigniter路由的工作方式是将example.com/test指向控制器文件夹中名为Test.php的控制器的索引函数。

以下是codeigniter网址路由的约定:

http://www.example.com/controller/function_in_that_controller/parameters/in/that/function

http://www.example.com/controller/,默认调用索引函数;因为在控制器之后的url中没有定义函数。