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