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.
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.
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.
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.