douzhen1234 2016-08-03 15:18
浏览 83
已采纳

CodeIgniter默认视图

I've got this controller called 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);
    }
}

but currently it only loads the home.php page by going to http://www.example.com/

I'm trying to make it so if I go to example.com/test it would load test.php

Here are my routes

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

I am new to php frameworks, and I'm not sure how to achieve this. Any help would be much appreciated.

  • 写回答

2条回答 默认 最新

  • dqlxtv1452 2016-08-03 15:24
    关注

    Codeigniter routing works in a way that it would point example.com/test to a the index function of a controller called Test.php in your controllers folder.

    The following is the conventions for codeigniter url routing:

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

    At http://www.example.com/controller/, the index function is called by default; since no function is defined in the url after the controller.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?