Why Use CodeIgniter 4 Shield?

             

Shield stands out among PHP auth libraries for several key reasons:

  • Official & maintained — No third-party dependency risks; updates with CodeIgniter core.
  • Security-first design — Built-in protection against common vulnerabilities (CSRF, brute-force, etc.).
  • Lightweight yet powerful — Small footprint, no bloat, but covers 90% of typical auth needs out-of-the-box.
  • Highly customizable — Extend views, actions, authenticators, or even replace components easily.
  • Modern PHP compatibility — Works seamlessly with PHP 8.1+, namespaces, and Composer ecosystem.
  • Active community — Backed by CodeIgniter forums, GitHub discussions, and regular releases (v1.0+ stable in 2025–2026).

If you're building admin panels, membership sites, APIs, or any app needing user accounts, Shield saves weeks of custom development compared to rolling your own auth system.



How to Install and Configure CodeIgniter 4 Shield – Complete Tutorial

Table Of Content

1 Prerequisites

  • PHP ≥ 8.2
  • Composer (latest version recommended)
  • A fresh CodeIgniter 4 project
  • MySQL/MariaDB database (or compatible)
  • Basic terminal/command-line knowledge

2 How to Install CodeIgniter 4 Shield (Step-by-Step)

In this updated guide, you'll learn how to install CodeIgniter 4 Shield, run setup commands, configure essential files, protect routes, customize views, and test your secure auth system. Ideal for developers building modern PHP web applications.

3 Create / Install a Codeigniter 4 Project

3.1 Install Codeigniter 4 Project

First, ensure your computer has Composer installed. Use the following command to install CodeIgniter 4 using Composer:

composer create-project codeigniter4/appstarter ci-4-shield-auth-app

Then, navigate to your project directory:

cd ci-4-shield-auth-app

3.2 Set Up Your Environment and Database

Rename env to .env (or cp env .env on Linux/macOS).

Update .env for development mode and database:

CI_ENVIRONMENT = development

database.default.hostname = localhost
database.default.database = your_db_name
database.default.username = your_db_user
database.default.password = your_db_password
database.default.DBDriver = MySQLi

Create the database in MySQL if it doesn't exist.

4 Install Codeigniter 4 Shield

composer require codeigniter4/shield

(Note: If you see stability errors, temporarily allow dev versions in composer.json if needed, but prefer stable releases.)

5 Set Up Shield Configuration

Run the setup command (this publishes config, runs initial setup steps):

php spark shield:setup

This creates app/Config/Auth.php (and related files).

Important additional steps from official docs (2026):

  • Run migrations to create Shield tables (users, auth_identities, etc.):
php spark migrate
  • Autoload helpers in app/Config/Autoload.php (recommended for easy access):
public $helpers = ['auth', 'setting'];
  • Configure email in app/Config/Email.php or .env (critical for activation, password reset, 2FA):
public string $fromEmail = 'no-reply@yourdomain.com';
public string $fromName  = 'Your App';

(Use SMTP like Gmail, Mailgun, etc., for production.)

6 Add Shield Routes

p>In app/Config/Routes.php:

$routes->group('auth', static function ($routes) {
    service('auth')->routes($routes);
});

Or simply (common shorthand):

service('auth')->routes($routes);

7 Protect Routes with Filters

In app/Config/Filters.php, protect all pages except auth routes:

public $globals = [
    'before' => [
        'session' => ['except' => ['login*', 'register', 'auth/*', 'logout']],
    ],
];

For forced password reset (optional):

'force-reset' => ['except' => ['login*', 'register', 'auth/*', 'change-password', 'logout']],

8 Customize Authentication Views

Copy default views to your app for customization:

php spark shield:publish --views

Edit files in app/Views/Auth/ (login.php, register.php, etc.) to match your site's design.

9 Folder Structure

10 Test Your Application

Start the development server:

php spark serve

Visit:

  • http://localhost:8080/login
  • http://localhost:8080/register

Register a user, log in, and verify protected pages redirect unauthenticated users to login.

11 Conclusion

Congratulations! You've successfully installed and configured CodeIgniter 4 Shield. Your app now has a robust, secure authentication system ready for production use. Explore advanced features like roles/permissions, actions (2FA, email verification), and custom providers in the official Shield documentation.
Revathi M - PHP and CodeIgniter Developer

Written by Revathi M

PHP Developer & Technical Writer · 10+ years building web applications with CodeIgniter and Laravel

Revathi specializes in PHP backend development, authentication systems, and REST API design. She writes practical, production-tested tutorials at Get Sample Code to help developers build secure applications faster.

Frequently Asked Questions

You need PHP 8.2 or higher, Composer installed globally, and a fresh CodeIgniter 4 project.

Run `composer create-project codeigniter4/appstarter project-name`, then navigate into the project directory with `cd project-name`.

Copy `env` to `.env`, set `CI_ENVIRONMENT = development`, and update the database settings in `.env` (e.g., hostname, database name, username, password, DBDriver as MySQLi).

Run `composer require codeigniter4/shield`.

Run `php spark shield:setup`. This generates `app/Config/Auth.php`.

In `app/Config/Routes.php`, add `service('auth')->routes($routes);`.

In `app/Config/Filters.php`, add 'session' to the global before filters, excluding routes like login, register, and auth actions.

Run `php spark shield:publish --views` to copy default views to `app/Views/Auth/`, then edit them as needed.

Start the server with `php spark serve`, then visit `http://localhost:8080/login`.

Configure SMTP settings in `app/Config/Email.php` or `.env`. Common issues include incorrect host/port, credentials, or firewall blocks. Test with a valid SMTP provider like Gmail or SendGrid.

Ensure you ran `php spark shield:setup` and migrated databases if needed (Shield installs its own migrations). Check Composer autoload with `composer dump-autoload`.

In `app/Config/Auth.php`, update `$validFields` to include 'username', and adjust validation rules accordingly.