agentleFS
AnyOrgNameAnyOrgName/php-skills/skills/codeigniter/controllers/SKILL.md

controllers

Skill4 starsChanged 4 months agoMITView on GitHub

Build CodeIgniter 4 controllers — extend BaseController, map request lifecycle to methods, read request and response services, and return typed responses. Use when the user wants a CodeIgniter controller to extend BaseController to share helpers, models, and services across actions; the user needs to handle an incoming HTTP request in CodeIgniter 4 by writing a controller class and action method; the user needs a CodeIgniter controller action to return a view, JSON, or redirect using the response service. Keywords: codeigniter, controllers, basecontroller, request-lifecycle, response, mvc.

The file

We split this skill into the rules it gives an agent

7 rules. About 0 are common, 4 are distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈4

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Do not use this skill to place heavy business rules or persistence logic inside the controller.

    line 65
  2. Distinctive:

    Do not apply this skill to CodeIgniter 3 projects.

    line 66
  3. Very specific:

    Create App\Controllers\Widgets extends BaseController and add a public action method such as show($id) that the router can reach.

    line 78
  4. Very specific:

    Add a _remap($method, ...$params) method only when you must intercept how a controller dispatches to its actions.

    line 87
  5. Very specific:

    Read input with $this->request->getPost('key'), getGet(), or getVar() instead of touching $_POST, $_GET, or $_REQUEST directly

    line 89
  6. Distinctive:

    Keep each action small: read input, call a model or service for domain work, then return a response

    line 91
  7. Distinctive:

    Not applicable — no I/O, auth, or untrusted input.

    line 116

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md
<!-- generated: do not edit — source: knowledge/atoms/codeigniter/controllers/ --><!-- schema: atomVersion=3 skillVersion=2 graphVersion=1 --><!-- compile: @php-skills/compiler v1.0.0 (deterministic build) -->

CodeIgniter 4 Controllers

When to use

  • The user wants a CodeIgniter controller to extend BaseController to share helpers, models, and services across actions.
  • The user needs to handle an incoming HTTP request in CodeIgniter 4 by writing a controller class and action method.
  • The user needs a CodeIgniter controller action to return a view, JSON, or redirect using the response service.

When NOT to use

  • Do not use this skill to place heavy business rules or persistence logic inside the controller; that belongs in a model or service class.
  • Do not apply this skill to CodeIgniter 3 projects; CI3 uses CI_Controller, the loader, and a different request model.

Core guidance

  • Extend BaseController, not the framework Controller directly — Have app controllers extend App\Controllers\BaseController rather than CodeIgniter\Controller directly, and put shared helpers, models, and services in initController() or properties on BaseController.
  • Return the response object instead of echoing output — Return a value from the action — view(...), $this->response->setJSON(...), or redirect()->to(...) — instead of calling echo or print directly.

Quick recipe

  1. Create App\Controllers\Widgets extends BaseController and add a public action method such as show($id) that the router can reach. (verify: Visiting the mapped route reaches the action)
  2. Inside the action, read input via $this->request->getVar('id') and fetch data with model(WidgetModel::class)->find($id) instead of querying inline. (verify: The action returns model data for a known id)
  3. Return view('widgets/show', $data) for HTML or $this->response->setJSON($data) for an API, and use $this->response->setStatusCode(404) when a record is missing. (verify: A missing record yields a 404, not a 200 with an empty body)

Best practices

  • Add a _remap($method, ...$params) method only when you must intercept how a controller dispatches to its actions; otherwise let the router call the method
  • Read input with $this->request->getPost('key'), getGet(), or getVar() instead of touching $_POST, $_GET, or $_REQUEST directly
  • Keep each action small: read input, call a model or service for domain work, then return a response

Anti-patterns

Anti-patternWhy it failsDo instead
Fat controller with domain and query logic inlineFat controllers cannot be unit-tested without HTTP and duplicate logic across actions.Move data access to a model and rules to a service; the action orchestrates and returns a response.
Instantiating models with new instead of the serviceDirect construction bypasses shared configuration and connection reuse and makes dependencies hard to swap in tests.Resolve models via model(SomeModel::class) or inject them through BaseController.
Reading superglobals inside the controllerSuperglobals bypass request normalization and filtering and cannot be substituted in tests.Read input through the request service ($this->request->getPost() / getGet()).

See references/anti-patterns.md.

Common mistakes

  1. Request is not ready in the constructor — Reading $this->request inside a controller __construct() fails because the request is injected later. Fix: Do setup that needs the request in initController() or the action, not in the constructor.
  2. Echoing output breaks status codes and filters — Calling echo in an action sends body bytes before CodeIgniter finalizes the response. Fix: Return view(...) or a response object; let CodeIgniter emit the body during finalization.
  3. Every public method is a routable endpoint — With auto-routing, any public controller method becomes a URL-reachable endpoint. Fix: Mark helpers protected/private, or disable auto-routing and declare only intended routes explicitly.

Version notes

  • CI4 introduced BaseController and initController — CodeIgniter 4.0 replaced CI3 CI_Controller with a namespaced BaseController and an initController() lifecycle hook.

Security

  • Not applicable — no I/O, auth, or untrusted input.

Testing

  • Activation and decision behavior are verified in eval/scenarios/codeigniter/controllers.yaml.

Additional resources

  • references/anti-patterns.md
  • references/core.md
  • references/examples.md
  • references/pitfalls.md
  • references/recipes.md
  • references/version-differences.md

Knowledge graph

Related: codeigniter/routing, php/typed-properties

<!-- graph-fragment: graph/fragments/codeigniter/controllers.yaml -->

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.