每当我尝试添加一个新 Controller 并通过 URL 访问各种公共(public)函数时,而不是仅重定向到 index() 函数而不是根据 URL 请求访问函数
示例代码是
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class courses extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('user_model');
define('CurrPage', 'Courses');
}
public function index()
{
echo "is in Index";
}
public function categoryInfo()
{
echo "is in cat";
}
public function c()
{
echo "is in c";
}
}
测试是 Controller
当输入 URL "..../test/"
输出是
is in Index
当输入 URL "..../test/c/"
输出是
is in Index
当输入 URL "..../test/categoryInfo/"
输出是
is in Index
config/routes.php 中的代码
$route['default_controller'] = "home";
$route['404_override'] = 'courses';
$route['v/(:any)']= "video_single/index";
$route['userInfo/(:any)']= "userInfo/index";
最佳答案
尝试将其添加到 config/routes.php
$controllers = array('test' );
foreach($controllers as $controller) {
$route[$controller] = $controller."/index";
$route[$controller."/(:any)"] = $controller."/$1";
}
关于php - CodeIgniter Controller 总是调用索引函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31218898/