Himanshu SharmaSoftware engineer
Let’s talk

CodeIgniter 4 · Lesson 6 of 30

CodeIgniter routes: map methods and paths explicitly

Declare explicit bookshop routes with HTTP verbs and numeric placeholders, then confirm the handlers through Spark.

15 minRequests and presentationUpdated 14 November 2026

Awaiting technical review. This lesson is readable but not yet listed in search engines.

By the end of this lessonDeclare explicit bookshop routes with HTTP verbs and numeric placeholders, then confirm the handlers through Spark.

After this lesson you can map explicit HTTP methods and paths to bookshop actions, then inspect the resulting route table. A route is an entry point into the application. Designing it deliberately gives you a clear inventory of what visitors can request.

Method and path form a contract

A GET request should retrieve information without changing catalogue records. A POST request can submit a deliberate change. Choosing different controller names does not enforce this distinction; route declarations must use the intended verbs. CodeIgniter's URI Routing guide documents verb-specific routes and placeholder matching.

Place these declarations in app/Config/Routes.php after its PHP opening tag. Preserve the starter's home route if you want its welcome page. The Books controller arrives in the next lesson, so inspect the map now and expect the controller-dependent URLs to work only after that file exists.

php
$routes->setAutoRoute(false);
$routes->get('books', 'Books::index');
$routes->get('books/new', 'Books::new');
$routes->get('books/(:num)', 'Books::show/$1');

These declarations were loaded in the verified application. The numeric placeholder passes the captured path component into show. It constrains the path shape; it does not prove that the corresponding book exists, that the caller may see it or that the value is appropriate for every possible database query.

Keep fixed and variable routes distinct

The fixed books/new path is the form page. The numeric detail route handles books/1. Because new is not numeric, the detail pattern does not match it. Broader segment placeholders can introduce ambiguity, making order more important. Prefer the narrow pattern that describes your identifier instead of making every path component a general string.

The controller mapping Books::show/$1 has two parts: the action name and the captured argument. Do not omit the argument forwarding and then assume the controller will receive an identifier. Conversely, do not pass arbitrary fragments into class names or filesystem paths. Request data is data, not executable routing configuration.

bash
php spark routes

Example output captured on 2026-11; yours will differ. Selected real table cells are transcribed below without borders or the banner.

text
GET  books             \App\Controllers\Books::index
GET  books/new         \App\Controllers\Books::new
GET  books/([0-9]+)     \App\Controllers\Books::show/$1

The report displays the compiled numeric pattern. That is normal; you wrote a named placeholder and the router expanded it. Look at the handler as well as the path when checking a route. A correctly spelled path can still call the wrong action.

Avoid unintended alternative entry points

This course disables auto routing and uses defined routes only. An explicit route map is easier to review when teaching filters and CSRF because it limits alternate ways to reach the same controller method. Do not enable legacy auto routing to make a missing route “work” without understanding the resulting access surface.

Later we add POST routes for changes and a read-only resource route for JSON. Those additions do not mean a GET request should call create or delete. Keep mutation routes separate, protect browser submissions with CSRF checks and review permissions before public exposure. A route existing is not evidence that a caller is allowed to use it.

Understand not found versus not allowed

A path that does not match a route is different from a route whose controller cannot find a book. Both can lead to a missing-resource response, but the diagnostic causes differ. First ask whether the request reached the intended action. Then ask whether the requested record exists.

A request using a method for which you did not define a matching route may also fail routing. Do not write tests that assume a particular status solely from a general HTTP preference without checking the framework's configured behavior. What matters for our safety boundary is that the wrong method cannot perform the write.

The official URI Routing guide was fetched and the declarations above were inspected with Spark in 4.7.6. The choices of books/new and numeric identifiers are the project's contract rather than universal URL rules.

Common mistakes

Using add for every route can unintentionally accept methods you did not intend. Enabling auto routing to bypass a typo expands the surface you must inspect. Confusing placeholder matching with record validation leaves missing-book behavior undefined. Finally, checking only the route path and ignoring its handler can leave the right URL pointing at the wrong operation.

Exercise

Predict whether GET /books/new and GET /books/12 match the numeric detail route. Then decide whether the route map above permits a catalogue deletion and explain which file should be inspected to verify your answer.

Show an answer

The new path matches its fixed route, not the numeric placeholder. The numeric path matches show and forwards 12. No deletion route is declared here. Inspect app/Config/Routes.php and confirm the effective route report with Spark rather than inferring behavior from a controller filename.

Previous lesson · Next lesson · Related: security boundaries

Key points

  • A route maps both method and path.
  • Numeric placeholders constrain syntax, not permissions or record existence.
  • The course keeps auto routing disabled.

Check yourself

1. What does (:num) guarantee?
2. Which method should read a catalogue without changing it?
3. Why keep auto routing disabled here?