CodeIgniter 4 cannot find a route when a form is submitted
Can'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.
Fix it fast
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 routes2Match what you see, then jump to its fix
- 1
The page opens with GET, but submitting the form gives the errorOnly a GET route exists for that URLFix - 2
The route is listed, but the controller is not foundThe controller name or namespace in the route is wrongFix - 3
There is no line for the URL in Routes.php and the controller method existsYou relied on auto routing, which is off by defaultFix
- 1
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.php2Fix
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 routesConfirmed by codeigniter.com
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.php2Fix
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 routesConfirmed by codeigniter.com
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.php2Fix
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 routesConfirmed by codeigniter.com
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix