【问题标题】:Getting from an id from url in codeigniter从codeigniter中的url获取id
【发布时间】:2016-10-28 13:05:27
【问题描述】:

我想调用一个函数 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/$1';

控制器是:

<?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;
        }
    }
    ?>

但是,上面的网址根本不起作用。我得到:

404 页面未找到

找不到您请求的页面。

请帮我解决问题。

【问题讨论】:

  • 非常感谢,我在这里感觉自己像个傻瓜。另外,您能帮我从路由字段中删除$route['question/get'] = 'question_controller/get';
  • 这样添加这些路由是没有用的。只需检查空或数字
  • 关于get() 方法,这些路线看起来不错。但是我不确定$route['question'] = 'question_controller'; 是否应该工作,因为那里没有显示index() 方法。您的应用程序是否使用了一些重定向/重写 apache 规则?

标签: php codeigniter http-status-code-404


【解决方案1】:

这样添加那些路由是没有用的

试试这个

在路线中

$route['question'] = 'question_controller';
#$route['question/get'] = 'question_controller/get'; // remove
#$route['question/(:num)'] = 'question_controller/get/$1'; // 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";
    }
}

因此,当您通过http://localhost/project/index.php/question/get/1http://localhost/project/index.php/question/get 时,它将达到相同的功能。它只能达到有值或没有值

【讨论】:

猜你喜欢
  • 2014-02-06
  • 2023-03-24
  • 2012-03-28
  • 1970-01-01
  • 2020-09-08
  • 2016-02-14
  • 1970-01-01
  • 1970-01-01
  • 2012-05-10
相关资源
最近更新 更多