agyanadda@gmail.com

CodeIgniter 4 CRUD Application

Database and Models 1 min read Updated Dec 3, 2026 Topic 15 of 30

CRUD — Create, Read, Update, Delete — is the base of every admin panel. Here is the full flow for posts.

Routes

  • GET posts → list
  • GET posts/new → create form
  • POST posts → save new
  • GET posts/(:num)/edit → edit form
  • POST posts/(:num)/update → save changes
  • POST posts/(:num)/delete → delete

Using POST for update and delete keeps them out of reach of simple links and lets CSRF protection check them. (You can also use $routes->resource() or presenter() for ready-made routes.)

Controller flow

  1. Validate input with $this->validate($rules)
  2. On failure: redirect()->back()->withInput() (errors are kept)
  3. On success: save with the model, then redirect with a flash message: ->with('success', 'Saved')

Not found

If find($id) returns null, throw PageNotFoundException::forPageNotFound() to show a 404 page.

In the views

  • <?= csrf_field() ?> in every form
  • old('title', $post['title'] ?? '') to refill fields
  • session('success') to show the message

Example

Example — Posts controller: store, update, delete PHP
<?php
namespace App\Controllers;

use App\Models\PostModel;
use CodeIgniter\Exceptions\PageNotFoundException;

class Posts extends BaseController
{
    private array $rules = ['title' => 'required|max_length[255]', 'body' => 'required'];

    public function create()
    {
        if (! $this->validate($this->rules)) {
            return redirect()->back()->withInput();
        }
        model(PostModel::class)->insert($this->request->getPost(['title', 'body']));

        return redirect()->to('posts')->with('success', 'Post created');
    }

    public function update(int $id)
    {
        $model = model(PostModel::class);
        $model->find($id) ?? throw PageNotFoundException::forPageNotFound();

        if (! $this->validate($this->rules)) {
            return redirect()->back()->withInput();
        }
        $model->update($id, $this->request->getPost(['title', 'body']));

        return redirect()->to('posts')->with('success', 'Post updated');
    }

    public function delete(int $id)
    {
        model(PostModel::class)->delete($id);

        return redirect()->to('posts')->with('success', 'Post deleted');
    }
}

// Routes.php
// $routes->get('posts', 'Posts::index');
// $routes->post('posts', 'Posts::create');
// $routes->post('posts/(:num)/update', 'Posts::update/$1');
// $routes->post('posts/(:num)/delete', 'Posts::delete/$1');

Invalid input sends the user back to the form with the errors and old values. A valid save redirects to /posts with a one-time success message. Updating an id that does not exist shows a 404 page.

Test Yourself With Exercises

Exercise 1 Multiple Choice

What does redirect()->back()->withInput() do?

Exercise 2 True / False

Every POST form should include csrf_field().

Exercise 3 Fill in the Blank

Refill a form field with ___('title') after a failed submit.

Frequently Asked Questions

A GET link can be triggered by search engine crawlers, browser prefetching or a link on another site. A POST form with a CSRF token makes sure only a real user on your site can delete data.