Skip to content

Latest commit

 

History

History
118 lines (71 loc) · 4.13 KB

File metadata and controls

118 lines (71 loc) · 4.13 KB

Auto Routing & Your First Endpoint

In this section, we enable CodeIgniter's Improved Auto Routing feature and create a simple JSON endpoint to confirm everything is wired correctly.

The previous tutorial showed how to define routes manually in app/Config/Routes.php. While powerful and flexible, this can be tedious for RESTful APIs with many endpoints that follow a common pattern. Auto-Routing simplifies this by mapping URL patterns to controller classes and methods based on conventions, and its focus on HTTP verbs works well for RESTful APIs.

By default, Auto-Routing is turned off. Enable it so your controllers automatically handle REST-style methods.

Open app/Config/Feature.php and confirm this flag is true (this is the default):

public bool $autoRoutesImproved = true;

The "Improved" auto router is more secure and reliable than the legacy version, so it's recommended for all new projects.

Then, in app/Config/Routing.php, confirm auto-routing is enabled:

public bool $autoRoute = true;

That's all you need for CodeIgniter to automatically map your controller classes and to URIs like GET /api/ping or POST /api/ping.

To understand how a basic API endpoint works, let's generate a controller to serve as our first API endpoint. This will provide a simple "ping" response to confirm our setup is correct.

php spark make:controller Api/Ping

This creates app/Controllers/Api/Ping.php.

Edit the file so it looks like this:

.. literalinclude:: code/001.php

Here we:

  • Use the :php:class:`ResponseTrait`, which already includes REST helpers such as :php:meth:`respond()` and proper status codes.
  • Define a getIndex() method. The get prefix means it responds to GET requests, and the Index name means it matches the base URI (/api/ping).

Start the development server if it isn't running:

php spark serve

Now visit:

  • Browser: http://localhost:8080/api/ping
  • cURL: curl http://localhost:8080/api/ping

Expected response:

{
    "status": "ok"
}

Congratulations — that's your first working JSON endpoint!

When you request /api/ping:

  1. The Improved Auto Router finds the App\Controllers\Api\Ping class.
  2. It detects the HTTP verb (GET).
  3. It calls the corresponding method name: getIndex().
  4. :php:trait:`ResponseTrait` provides helper methods to produce consistent output.

Here's how other verbs would map if you added them later:

HTTP Verb Method Name
GET /api/ping POST /api/ping DELETE /api/ping getIndex() postIndex() deleteIndex()

By default, CodeIgniter uses the :php:class:`CodeIgniter\\Format\\Format` class to automatically negotiate the response format. It can return responses in either JSON or XML depending on what the client requests.

The :php:trait:`ResponseTrait` sets the format to JSON by default. You can change this to XML if desired, but this tutorial will focus on JSON responses.

.. literalinclude:: code/002.php

The respond() method can return additional data:

.. literalinclude:: code/003.php

You now have a working endpoint tested in both the browser and cURL. In the next section, we'll create our first real database resource. You'll define a migration, seeder, and model for a simple books table that powers the API's CRUD endpoints.