从 codeigniter 中的 url 获取 id
Getting from an id from url in codeigniter
我想调用一个函数 get
并从以下 url
中获取 1 作为问题控制器中的某个值
http://localhost/project/index.php/question/get/1
我正在尝试的路由配置是:
$route['question'] = 'question_controller';
$route['question/get'] = 'question_controller/get';
$route['question/(:num)'] = 'question_controller/get/';
控制器是:
<?php
/**
*
*/
class Question_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper(array('form'));
$this->load->library('form_validation');
$this->load->helper('url');
$this->load->helper('security');
$this->load->model('Questions');
}
function get($q_id = null)
{
echo $q_id;
}
}
?>
但是,上面的url根本不起作用。我得到:
404 Page Not Found
The page you requested was not found.
请帮我解决问题。
像这样添加那些路由是没有用的
试试这个
在路线中
$route['question'] = 'question_controller';
#$route['question/get'] = 'question_controller/get'; // remove
#$route['question/(:num)'] = 'question_controller/get/'; // remove
在控制器中
function get($q_id = null)
{
if (!empty($q_id ) && is_int($q_id )) {
echo "$q_id with the Number";
} else {
echo "Its Empty or Its without Number";
}
}
So when you pass http://localhost/project/index.php/question/get/1
or http://localhost/project/index.php/question/get
it will reach same function. It can only reach with the value or without value
我想调用一个函数 get
并从以下 url
http://localhost/project/index.php/question/get/1
我正在尝试的路由配置是:
$route['question'] = 'question_controller';
$route['question/get'] = 'question_controller/get';
$route['question/(:num)'] = 'question_controller/get/';
控制器是:
<?php
/**
*
*/
class Question_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper(array('form'));
$this->load->library('form_validation');
$this->load->helper('url');
$this->load->helper('security');
$this->load->model('Questions');
}
function get($q_id = null)
{
echo $q_id;
}
}
?>
但是,上面的url根本不起作用。我得到:
404 Page Not Found
The page you requested was not found.
请帮我解决问题。
像这样添加那些路由是没有用的
试试这个
在路线中
$route['question'] = 'question_controller';
#$route['question/get'] = 'question_controller/get'; // remove
#$route['question/(:num)'] = 'question_controller/get/'; // remove
在控制器中
function get($q_id = null)
{
if (!empty($q_id ) && is_int($q_id )) {
echo "$q_id with the Number";
} else {
echo "Its Empty or Its without Number";
}
}
So when you pass
http://localhost/project/index.php/question/get/1
orhttp://localhost/project/index.php/question/get
it will reach same function. It can only reach with the value or without value