0

I am new to codeigniter and have an old project I am trying to run locally on my MAMP server. I am running into an issue where although the backend loads and connects to the database, the controllers are not working.

I am using MAMP nginx php v7.1.33 and codeigniter v2.1.4.

When I run the application, I get the following error:

<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 />

the default controller is user and the index for that looks like:

   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") . '}';
        }
    }

to resolve the above error, I changed that method to: public function index($user_id=NULL, $access_token=NULL, $employee_id=NULL)

Although that resolved the above issue, When I try to run an api request, I get:

<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>

I have researched a lot and even tried to change the .hta file thinking its an access issue but had no luck. My current root .htaaccess is:

    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]

my index.php is:

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

After fixing the too few arguments error, the application connects successfully but fails any api fails and any route other then to the index.php leads to 404 as shown above.

I have been stuck on this for a while and tried all the treads on this issue I could find with no luck. Thanks in advance for the help.

I managed to fix then 404 issue but am unable to get the api to work.

There are no views associated with this project as it is just for api.

After debugging a bit more, I see that every route other than the base url returns 404. The base url returns 200. logs

4
  • Apache Configuration The issue might be related to your Apache configuration. Make sure mod_rewrite is enabled and properly configured. Verify that the .htaccess file is present and properly configured to redirect all requests to index.php Commented Sep 27, 2024 at 18:29
  • Thanks for the response. I am a beginner with Apache and codeignitor. Can you guide me on where and how to change the apache configuration? I am using NGINX as when I use Apachi I get the following error: The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator at [email protected] to inform them of the time this error occurred, and the actions you performed just before this error. More information about this error may be available in the server error log. Commented Sep 27, 2024 at 18:44
  • I'd start analyzing the error message and start debugging. Error says Uncaught ArgumentCountError: Too few arguments to function user::index(), your index() function expects 3 arguments, why are they not received???? Commented Sep 27, 2024 at 18:54
  • debugging a bit more, I see that every route other than the base url returns 404. The base url returns 200. Commented Sep 28, 2024 at 8:23

1 Answer 1

0

The .htaccess file does not work with Nginx. Instead of .htaccess, Nginx uses its own configuration file to handle rewrite rules and other server configurations.

Here's a basic example of setting up Nginx for CodeIgniter

Remember that file configuration nginx is /etc/nginx/defualt.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;
    }
}

Test this, this configuration block ensures that all requests are redirected to index.php if no matching files or directories are found, which is essential for routing in CodeIgniter.

reed more

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

nginx and codeigniter "passing uri" does not work

Sign up to request clarification or add additional context in comments.

2 Comments

it did not work, but upon digging more into it, I discovere nginx error logs which show the following: [error] 56297#0: *19 open() "/Applications/MAMP/htdocs/backend/users/authenticate" failed (2: No such file or directory), client: 127.0.0.1, server: , request: "POST /dinengo/users/authenticate HTTP/1.1", host: "localhost:8888"
Your answer is partially incorrect; nginx can use simple statements in an .htaccess file: this is why among other platforms, Wordpress, Magento and Shopware use .htacess files, regardless of the webserver configuration; these files will be used by both Nginx and Apache2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.