Welcome Guest, Not a member yet? Register   Sign In
Routing Not Working - Only Index Route Accessible
#1

Hello everyone,

I'm facing an issue with routing in CodeIgniter 4. Only the index route is working, and none of my other controller methods are accessible via URLs.
Here’s what I’ve done so far:
app/Config/Routes.php
Code:
<?php

use CodeIgniter\Router\RouteCollection;

/**
* @var RouteCollection $routes
*/
$routes->post('/', 'Home::index');
$routes->get('getLeadsByDateRange', 'App\Controller\Home::getLeadsByDateRange');
$routes->post('/lead', 'App\Controller\LeadController::index');
$routes->get('/lead/getLeadsByDateRange', 'LeadController::getLeadsByDateRange');

.htaccess
Code:
<IfModule mod_rewrite.c>
        Options +FollowSymlinks
        RewriteEngine On

        # If you installed CodeIgniter in a subfolder, you will need to
        # change the following line to match the subfolder you need.
        # http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
        RewriteBase /

        # Redirect Trailing Slashes...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_URI} (.+)/$
        RewriteRule ^ %1 [L,R=301]

        # Rewrite "www.example.com -> example.com"
        RewriteCond %{HTTPS} !=on
        RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
        #RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
        RewriteRule ^(.*)$ index.php/$0 [L]


        # Checks to see if the user is attempting to access a valid file,
        # such as an image or css document, if this isn't true it sends the
        # request to the front controller, index.php
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA]

        # Ensure Authorization header is passed along
        RewriteCond %{HTTP:Authorization} .
        RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>


/app/Controller/Home.php
Code:
<?php

namespace App\Controllers;
use App\Models\LeadBackupModel;
use CodeIgniter\I18n\Time;
use CodeIgniter\Controller; // Extending the base controller

class Home extends BaseController
{
        public function __construct()
        {
                header('Access-Control-Allow-Origin: *');
                header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
                header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
                $method = $_SERVER['REQUEST_METHOD'];
                if($method == "OPTIONS") {
                    die();
                }
        }

        public function index()
        {
          return view('welcome_message');
        }

        public function getLeadsByDateRange()
        {
          return view('welcome_message');
;

                }
}


app/Config/App.php
Code:
public string $indexPage = '';
Reply
#2

Invalid namespace in routes. App\Controllers
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#3

(This post was last modified: 09-26-2024, 08:51 AM by rahul_ci.)

Hi,
I tried changing the values as well, still not working.
Just the Home index route is working apart from this all three on them are "404 not found"

Code:
$routes->post('/', 'Home::index');
$routes->get('getLeadsByDateRange', 'Home::getLeadsByDateRange');
$routes->post('lead', 'LeadController::index');
$routes->get('lead/getLeadsByDateRange', 'LeadController::getLeadsByDateRange');
Reply
#4

See output in terminal ./spark routes
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#5

(09-26-2024, 11:35 AM)ozornick Wrote: See output in terminal ./spark routes

I reinstalled the project and found out I was making a mistake by copying the .htaccess and index.php files from the public folder to the app folder. Now I’ve copied them to the root folder, and the project is working fine. However, index.php is still showing in the URL.
Reply
#6

Code:
    # Remove index.php from URL - .htaccess
    RewriteCond %{HTTP:X-Requested-With}    !^XMLHttpRequest$
    RewriteCond %{THE_REQUEST}              ^[^/]*/index\.php [NC]
    RewriteRule ^index\.php(.*)$            $1 [R=301,NS,L]
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB