📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials CodeIgniter 4 Routing in CI4

Routing in CI4

5 min read Quiz at the end
Define routes explicitly using get/post/resource/group with filters, named routes, and constraints.

Routing in CodeIgniter 4

// app/Config/Routes.php
$routes->get("/", "Home::index");
$routes->get("/about", "Page::about");

// Resource routes
$routes->resource("posts");           // full CRUD
$routes->presenter("photos");         // HTML CRUD

// Parameters
$routes->get("/users/(:num)", "User::show/$1");
$routes->get("/posts/(:segment)", "Post::show/$1");

// Route groups
$routes->group("admin", ["filter" => "auth"], function($r) {
    $r->get("/", "AdminDashboard::index");
    $r->resource("users", ["controller" => "AdminUsers"]);
});

// Named routes
$routes->get("/login", "Auth::login", ["as" => "login"]);

// HTTP verb shortcuts
$routes->post("/users", "User::store");
$routes->put("/users/(:num)", "User::update/$1");
$routes->delete("/users/(:num)", "User::delete/$1");

// Auto-routing (legacy — disabled by default in CI4.2+)
$routes->setAutoRoute(false);
Topic Quiz · 3 questions

Test your understanding before moving on

1. Which command creates a CI4 migration?
💡 php spark make:migration create_posts_table generates a new migration file.
2. How do you run CI4 migrations?
💡 php spark migrate applies all pending migration files.
3. Which command lists all registered routes?
💡 php spark routes displays a table of all registered routes.