CodeIgniter 4 Shield is an authentication and authorization provider designed to be flexible and easily customizable..

Codeigniter 4 Shield - Install and Configure Codeigniter 4 Shield

Table Of Content

1 Prerequisites

1.) PHP version of 8.2
2.) Composer
3.) CodeIgniter Shield

2 Introduction

In this article, we'll cover how to use Shield in CodeIgniter 4, including installation, configuration, and basic usage.

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 Configure MySql Database

Upon Successfull User Registration, the users data will be stored in the database. This process involves accessing the .env file to input and define the database credentials.

database.default.hostname = localhost
database.default.database = codeigniter_4
database.default.username = root
database.default.password = 
database.default.DBDriver = MySQLi
database.default.DBPrefix =
database.default.port = 3306

When we install CodeIgniter 4, we will have env file at root. To use the environment variables, we need to rename env to .env

Also we can do this in command Line.

Open project in Command Line

sudo cp env .env

Above command will create a copy of env file to .env file. Now we are ready to set environment variables.

Configure Development Mode
CodeIgniter starts up in production mode by default. You need to make it in development mode to debug or check any error if you are working with application.

Open .env file from root.

# CI_ENVIRONMENT = production
CI_ENVIRONMENT = development


Now application is in development mode.

4 Install Codeigniter 4 Shield

First, make sure your computer has a composer.
To install and configure CodeIgniter 4 Shield, run the following command:

composer require codeigniter4/shield

This command will download the Shield package and add it to your project.

5 Configure Codeigniter 4 Shield

After Successfull Codeigniter 4 Shield installation, you may need to setup Shield's configuration. Run the following command to setup the config file:

php spark shield:setup

This command will create a new file in app/Config/ named Auth.php, where you can configure CodeIgniter 4 Shield settings.

6 Configure Route

Define routes for the Shield in the Routes.php file
app/Config/Routes.php


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


7 Controller Filters

Protect All Pages
If you want to limit all routes, you need to add the following code in the app/Config/Filters.php file.

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

Forcing Password Reset
If your application requires a force password reset functionality, ensure that you exclude the auth pages and the actual password reset page from the before global.

public $globals = [
    'before' => [
        //...
        //...
        'force-reset' => ['except' => ['login*', 'register', 'auth/a/*', 'change-password', 'logout']]
    ]
];

8 Customizing Views

Shield provides some default views for authentication (like login, registration, etc.). You can customize these by copying them from the Shield package to your app/Views directory:
Use the following command to copy Shield views via Composer.

php spark shield:publish --views

This command will copy all the CodeIgniter 4 Shield sample code views to your app/Views/Auth/ folder, where you can modify them as needed.

9 Folder Structure

10 Run Web Server to Test the App

Use the following command to Test the App.

php spark serve

Visit the URL http://localhost:8080/index.php/login

11 Conclusion

By following this CodeIgniter 4 Shield tutorial, you can quickly install and configure CodeIgniter 4 Shield, customize its views, and secure your application with a solid authentication system.

Tags