Currently, Codeigniter 4 is the latest release of Codeigniter framework. It is a PHP framework and built for the web development. Also, you can create RESTful API using this framework. From this post, I have started a new series on Codeigniter4 from very scratch. Firstly, I will show you how you can install Codeigniter4 in Windows and Linux platform. Codeigniter4 installation is quite simple in any operating system. After updating the Codeigniter version, in the latest release it is much similar to the Laravel framework. Here, serveral new features are added. Most interesting is now you can manage Codeigniter 4 application using the command line just like the Laravel. Now, you can download codeigniter4 directly. So, let’s move to the installation process quickly.
Before creating the new project in Codeigniter 4 framework, you will require the below configurations.
- PHP >= 7.3
- MySQL (version > 5)
- Apache/Nginx Server
- VS Code Editor
- Composer (Optional)
- XAMPP/WAMP (for windows user)
Install Codeigniter4 in Windows and Linux
For installing Codeigniter4, you can download it from it’s official website. However, you can install it using the composer. So, you have both options. Either you can download it’s zip file or you may use the composer CLI to install it. I will show you the both options here. Firstly, we will see the first option to install CodeIgniter4 by downloading it.
Download and Install Codeigniter4
You can download the latest version of Codeigniter framework from it’s official website. You will see the CodeIgniter 4 (future) version on it’s official download page. Here, we will download the CodeIgniter 4.
For the windows user, I assume you already have XAMPP or WAMP installed in your system. XAMPP user, simply paste this downloaded framework inside the xampp/htdocs folder. If you have installed WAMP then paste it inside the wamp/www folder. After pasting the folder, you will require to extract the .zip file.
Also, it will require to rename the folder. Actually, by default, the name of the folder is the framework-4.1.1 (as per the latest version). So, here, I will rename it. After that, you can run it directly from the localhost as a PHP application runs.
Run CodeIgniter 4 in Localhost
For running the codeigniter 4 in the browser, you can localhost/folderpath/appname. In my case, I pasted the downloaded file inside a folder name codeigniter. So, here this is the absolute path in the browser. I have opened it in the browser and you can see the result.
Here, in the above result, you can see, we haven’t entered into the application completely. So, for that, you will have to click on the public/ directory. The CodeIgniter 4 application bootstrapping starts from the index.php file. And this index.php file is contained inside the public folder. I will explain the folder structure in the next step.
When, you will enter intot he public/ directory then it will show you some error as showing below.
So, let’s fix this issue. Firstly, open the application into the editor. Here, I am using VS Code editor. Therefore, I have opened the folder inside it. Now, search for env file. The env file is the environment file of the application here.
Configure CodeIgniter Application
Here, you will have to follow these steps-
- Rename env file to .env file.
- Uncomment CI_ENVIRONMENT
- Change CI_ENVIRONMENT = production to development
In the .env file, look for the CI_ENVIRONMENT. By default, it is commented, so you have to uncomment it by removing the # symbol.
After renaming the env file, I just uncommented the CI_ENVIRONMENT and changed the production to development.
After the above operations, you can refresh the page. After refreshing the page, you will see the below exception.
In the above result, you can see the CodeIgniter cache exception. So, let’s find out the source of this exception.
How to Fix CodeIgniter Cache Exception
Before, fixing this issue, firstly find out the source of the cache exception. If you are a Linux user then you will have to give the rwx permission to this directory. This is basically the permission issue in this directory. By default, in the Linux, writable folder has not the write permission. So, here, we will have to set the permission.
sudo chmod -R 777 writable/
The above command will set the read, write and execute permission to the writable folder and its subfolder. This permission is given in the recursive mode.
So, finally, we are able to run the homepage. Now, we will see the second way of CodeIgniter4 installation.
Install CodeIgniter4 Using Composer
If you want to install CodeIgniter4 using the CLI, then you will have to go through the composer. Here, using the composer you can create a project in CodeIgniter 4. So, let’s open the command prompt or terminal. Then hit the below command there.
composer create-project codeigniter4/appstarter my-blog
Here, the above command will install CodeIgniter4 in the specified directory. The project name will be my-blog.
After finishing the installation, let’s run the application using the spark. Spark is just like artisan in Laravel. Here, in the CodeIgniter 4 you can manage the CodeIgniter 4 CLI using the spark. It is a new package management system for the CodeIgniter.
Run CodeIgniter 4 Application Using Spark
To run the CodeIgniter 4 application using the command line, you can hit the below command.
php spark serve
After executing the above command, you will see the below response in the terminal. Here, CodeIgniter development server started on the
http://localhost:8080. This is the default port of the CodeIgniter 4 application.
You have to open the above URL in the browser to see the running application result.
Let’s compare the folder structure of both project directory. Firstly, we installed CodeIgniter by downloading from it’s official site. Secondly, we installed CodeIgniter 4 using the composer.
Compare the CodeIgniter 4 Directories
The first project folder is downloaded by it’s official website.
The second one is installed using the composer. So, here you may find the differences in both project folders.
Differences of Project Folder
Here, some of the measure differences you may find in both folders.
- vendor directory missing in first folder.
- tests directory is missing in first folder.
- system directory missing in second folder.
You may generate the vendor directory by running the composer update command.
CodeIgniter 4 Project Folder Structure
- The first folder is app. This folder contains all the important folders and subfolders which required the application.
- The app/Config folder contains all the configuration files of the application.
- Inside the app/Config/Boot folder there are files for setting environment for development, production and testing.
- The app/Controllers folder will contain all the functions which contain logical codes. These functions will be linked with the routes.
- Next folder is app/Database. The app/Database folder has two subfolders. The Migrations folder contain the schema of the database table. The Seeds folder is helpful for putting dummy records in the tables. These two features were not available in the previous version of CodeIgniter.
CodeIgniter Helpers Directory
- You can create and use Helpers in CodeIgniter using the Helper directory.
- Also, you can create multilingual or set Localization using Language folder.
- The next important folder is Models and Views. The Models contain all the database related query and operations. Basically, the models help the synchronization between the controllers and database.
- The Views contain the HTML templates which are used to send and populate the data.
- The public folder contains the index.php, and .htaccess file. By default, the user has the access of the public folder. You can upload the images and keep into this folder.
- If you want to create the tests case for your application, then you can create inside the tests folder.
- The vendor directory contains all the necessary files on which the framework runs.
- In the writable folder, you will have the cache, debugbar, logs, session, etc.
So, these are the overview of the folder strucutre in CodeIgniter 4.
Generate Vendor Directory in CodeIgniter 4
You can create vendor directory in CodeIgniter 4 by using composer. Open the project directory and hit the below command in the terminal.
The above command will generate the missing vendor directory in the project folder.
In this post, we installed the CodeIgniter 4 in two ways. The first way, is simply download and extract the .zip file. The second way is through the composer dependency manager. So, you can follow both options as per your convenience. In CodeIgniter 4, you can use CLI, just like the other PHP framework. The CodeIgniter 4 is very much similiar to the Laravel framework. You can create database migration, seeder, and all. I will discuss all these features in the upcoming CodeIgniter posts. So, say tuned with us.