Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

CodeIgniter 4 cannot find a route when a form is submitted

ErrorCan't find a route for 'registrations/index'.

Short answer

CodeIgniter 4 only runs routes you define, or auto routes if you enabled them. A form POST fails when there is no post() route for the form's action, when the route is defined with get(), or when the controller namespace or class name is wrong. Define the POST route in app/Config/Routes.php.

  • Applies to CodeIgniter 4.5, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List the routes CodeIgniter knows and look for your form's action URL with the POST method.

    php spark routes
  2. 2Match what you see, then jump to its fix

    1. 1The page opens with GET, but submitting the form gives the errorOnly a GET route exists for that URLFix
    2. 2The route is listed, but the controller is not foundThe controller name or namespace in the route is wrongFix
    3. 3There is no line for the URL in Routes.php and the controller method existsYou relied on auto routing, which is off by defaultFix

What does the error look like?

  • Can't find a route for 'registrations/index'.
  • 404 Page Not Found after submitting a form

Cause 1 of 3

Only a GET route exists for that URL

1Check

The routing guide gives each HTTP verb its own method, and only routes matching the current request method are stored for it. A POST to a URL defined only with get() has no match.

grep -n "registrations" app/Config/Routes.php

2Fix

Add a post() route for the form's action and point it at the method that handles the data.

$routes->get('registrations', 'Registrations::index');
$routes->post('registrations', 'Registrations::create');

3Verify

List the routes again and submit the form.

php spark routes

Confirmed by codeigniter.com

Did this fix it?

Cause 2 of 3

The controller name or namespace in the route is wrong

1Check

A controller without a leading backslash gets App\Controllers prepended, and the file must start with a capital letter and match the class name, as the controllers guide states.

ls app/Controllers
grep -n "^namespace\|^class" app/Controllers/Registrations.php

2Fix

Match the file name, class name and namespace, or use a fully qualified name with a leading backslash.

// app/Controllers/Registrations.php
namespace App\Controllers;
class Registrations extends BaseController
{
    public function create() { /* ... */ }
}

3Verify

Submit the form again.

php spark routes

Confirmed by codeigniter.com

Did this fix it?

Cause 3 of 3

You relied on auto routing, which is off by default

1Check

The routing guide says auto routing has been disabled by default since 4.2.0, and the improved version is controlled by autoRoute in app/Config/Routing.php. Legacy auto routing accepts any HTTP verb and is discouraged.

grep -n "autoRoute" app/Config/Routing.php

2Fix

Prefer an explicit route. Enable improved auto routing only if you accept its rules.

$routes->post('registrations/create', 'Registrations::create');

3Verify

Submit the form again.

php spark routes

Confirmed by codeigniter.com

Did this fix it?

How do I stop it coming back?

  • Define routes explicitly and keep the form's action URL generated from the same route name.
  • Run php spark routes in CI and fail if a needed POST route is missing.
  • Match controller file names to class names exactly.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.