【问题标题】:404 Codeigniter page error when calling method调用方法时出现 404 Codeigniter 页面错误
【发布时间】:2023-03-11 22:54:01
【问题描述】:

我的代码点火器有问题,我有以下代码(user_stats.php):

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class User_stats extends MX_Controller
{
    function __construct() {
        parent::__construct();
    }
    function user_stats() {
        echo "Hello";
    }
    function detect_location() {
        echo "World";
    }   
}

如果我通过 url 调用该方法,例如http://test/user_stats/detect_location 我得到了输出 World,但是当我加载 http://test/user_stats/user_stats 时,我得到一个错误 404 页面。我检查了日志,出于某种原因,请求是针对http://test/user_stats/index(尝试加载http://test/user_stats/user_stats 时)。我的 htaccess 文件有以下代码:

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

配置文件配置为:

$config['index_page'] = '';

$config['uri_protocol'] = 'AUTO';

$config['url_suffix'] = '';

任何解决此问题的帮助将不胜感激。

谢谢

【问题讨论】:

    标签: .htaccess codeigniter http-status-code-404 config


    【解决方案1】:

    将函数名user_stats()改成index()并打开config/routes.php文件 并在其中添加这一行:

    $route['user_stats/user_stats'] = "user_stats/index";
    

    注意:正如@oldlearner 所说,该函数的作用类似于构造函数,并且在 php5 中已弃用。

    【讨论】:

      【解决方案2】:

      基本上你的类名是User_stats,函数名是user_stats。具有相同类名的函数名被视为构造函数。如果您删除您的function __construct() 并点击此网址http://test/user_stats/detect_location,您将获得输出

      Hello World 
      

      本例 user_stats 是您的构造函数。
      实际上你有两个构造函数,如果你保留function __construct(),function user_stats 是没用的。对于具有相同参数的类,您不能有两个构造函数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-23
        • 1970-01-01
        • 2023-01-15
        • 2020-11-28
        • 1970-01-01
        相关资源
        最近更新 更多