CodeIgniter Forums
Routing Not Working - Only Index Route Accessible - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Routing Not Working - Only Index Route Accessible (/showthread.php?tid=91728)



Routing Not Working - Only Index Route Accessible - rahul_ci - 09-26-2024

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 = '';



RE: Routing Not Working - Only Index Route Accessible - ozornick - 09-26-2024

Invalid namespace in routes. App\Controllers


RE: Routing Not Working - Only Index Route Accessible - rahul_ci - 09-26-2024

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');



RE: Routing Not Working - Only Index Route Accessible - ozornick - 09-26-2024

See output in terminal ./spark routes


RE: Routing Not Working - Only Index Route Accessible - rahul_ci - 09-27-2024

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


RE: Routing Not Working - Only Index Route Accessible - InsiteFX - 09-29-2024

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]