我有一个名为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);
}
}
但目前它只加载home.php页面http://www.example.com/
我正在努力做到这一点,所以如果我去example.com/test,它会加载test.php
这是我的路线
$route['default_controller'] = 'page';
$route['(:any)'] = 'page/$1';
我是php框架的新手,不知道如何实现这一点。任何帮助都将不胜感激。
更改
$route['(:any)'] = 'page/$1';
至
$route['(:any)'] = 'page/index/$1';
Codeigner路由的工作方式是将example.com/test
指向控制器文件夹中名为Test.php
的控制器的索引函数。
以下是代码点火器url路由的约定:
http://www.example.com/controller/function_in_that_controller/parameters/in/that/function
在http://www.example.com/controller/
,默认情况下调用索引函数;因为在控制器之后的url中没有定义函数。