Codeigniter 控制器路由不起作用

问题描述 投票:0回答:1

我是 codeigniter 的新手,并且有一个旧项目,我试图在我的 MAMP 服务器上本地运行。我遇到一个问题,虽然后端加载并连接到数据库,但控制器无法工作。

我正在使用 MAMP nginx php v7.1.33 和 codeigniter v2.1.4。

当我运行应用程序时,出现以下错误:

<b>Fatal error</b>:  Uncaught ArgumentCountError: Too few arguments to function user::index(), 0 passed in /Applications/MAMP/htdocs/dinengo/system/core/CodeIgniter.php on line 359 and exactly 3 expected in /Applications/MAMP/htdocs/dinengo/application/controllers/user.php:109
Stack trace:
#0 /Applications/MAMP/htdocs/dinengo/system/core/CodeIgniter.php(359): user-&gt;index()
#1 /Applications/MAMP/htdocs/dinengo/index.php(204): require_once('/Applications/M...')
#2 {main}
  thrown in <b>/Applications/MAMP/htdocs/dinengo/application/controllers/user.php</b> on line <b>109</b><br />

默认控制器是用户,其索引如下所示:

   public function index($user_id, $access_token, $employee_id) {
        echo 'Authenticating user ';
        if ($this->oauth->authenticate($user_id, $access_token)) {
            $result = $this->employee_m->getUser($employee_id);

            $avatar = $result->avatar;
            $pass_image = $result->pass_image;

            $base_url = base_url();
            $avatar = $base_url . "uploads/avatar/" . $avatar;

            $pass_image = $base_url . "uploads/pass/" . $pass_image;
            $result->avatar = $avatar;
            $result->pass_image = $pass_image;

            //echo json_encode($result);
            echo '{"success":' . json_encode($result) . '}';
        } else {

            echo '{"success":' . json_encode("This connection is untrusted") . '}';
        }
    }

为了解决上述错误,我将该方法更改为:

public function index($user_id=NULL, $access_token=NULL, $employee_id=NULL)

虽然解决了上述问题,但当我尝试运行 api 请求时,我得到:

<html>
    <head>
        <title>404 Not Found</title>
    </head>
    <body>
    <center>
        <h1>404 Not Found</h1>
    </center>
    <hr>
    <center>nginx/1.25.3</center>
    </body>
</html>

我进行了很多研究,甚至尝试更改 .hta 文件,认为这是访问问题,但没有成功。我当前的根 .htaaccess 是:

    DirectoryIndex index.php
    RewriteEngine on
 
    RewriteCond $1 !^(index\.php (.*)\.swf|forums|images|css|downloads|jquery|js|robots\.txt|favicon\.ico)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]

我的index.php是:

<?php
      ob_start();

    define('ENVIRONMENT', 'development');


     if (defined('ENVIRONMENT'))
     {
    switch (ENVIRONMENT)
    {
        case 'development':
            error_reporting(E_ALL);
            ini_set('display_errors', 1);
        break;
    
        case 'testing':
        case 'production':
            error_reporting(0);
        break;

        default:
            exit('The application environment is not set correctly.');
    }
       }

    $system_path = 'system';


    $application_folder = 'application';



    // Set the current directory correctly for CLI requests
    if (defined('STDIN'))
    {
        chdir(dirname(__FILE__  }

    if (realpath($system_path) !== FALSE)
    {
        $system_path = realpath($system_path).'/';
    }

    // ensure there's a trailing slash
    $system_path = rtrim($system_path, '/').'/';

    // Is the system path correct?
    if ( ! is_dir($system_path))
    {
            exit("Your system folder path does not appear to be set correctly. Please open the following file and correct this: ".pathinfo(__FILE__, PATHINFO_BASENAME));
    }

    // The name of THIS file
    define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME));

    // The PHP file extension
    // this global constant is deprecated.
    define('EXT', '.php');

    // Path to the system folder
    define('BASEPATH', str_replace("\\", "/", $system_path));

    // Path to the front controller (this file)
    define('FCPATH', str_replace(SELF, '', __FILE__));

    // Name of the "system folder"
    define('SYSDIR', trim(strrchr(trim(BASEPATH, '/'), '/'), '/'));


    // The path to the "application" folder
    if (is_dir($application_folder))
    {
        define('APPPATH', $application_folder.'/');
    }
    else
    {
        if ( ! is_dir(BASEPATH.$application_folder.'/'))
        {
            exit("Your application folder path does not appear to be set correctly.     Please open the following file and correct this: ".SELF);
        }

        define('APPPATH', BASEPATH.$application_folder.'/');
    }


      require_once BASEPATH.'core/CodeIgniter.php';`

my routes.php is:
    `$route['default_controller'] = "user";
    $route['404_override'] = '';
    $route['verify/(:any)'] = "/user/verify/$1";

我的文件结构是:file structure file structure

修复了参数太少的错误后,应用程序连接成功,但失败,任何 api 都会失败,除 index.php 之外的任何路由都会导致 404,如上所示。

我已经在这个问题上坚持了一段时间,并尝试了我能找到的关于这个问题的所有步骤,但没有运气。预先感谢您的帮助。

我设法解决了 404 问题,但无法让 api 工作。

没有与此项目关联的视图,因为它仅用于 api。

api .htaccess codeigniter model-view-controller http-status-code-404
1个回答
0
投票

.htaccess 文件不适用于 Nginx。 Nginx 使用自己的配置文件来处理重写规则和其他服务器配置,而不是 .htaccess。

这是为 CodeIgniter 设置 Nginx 的基本示例:

记住 nginx 的conf文件是/etc/nginx/default.conf

server {
    listen 80;
    server_name example.com; 
    root /path/to/your/codeigniter;

    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

测试一下,这个配置块确保如果没有找到匹配的文件或目录,所有请求都重定向到index.php,这对于CodeIgniter中的路由至关重要。

芦苇更多

https://codeigniter.com/user_guide/general/urls.html

© www.soinside.com 2019 - 2024. All rights reserved.