If you want to work in CodeIgniter 4 you need download and install CodeIgniter 4 using Composer or manually. Once installation is done you can create project in CodeIgniter 4 and start working on it. In this topic I will show how to install and create project in CodeIgniter 4 and also test for database connection using MySQL.
Installing CodeIgniter 4 using composer is very easy and also it is a preferred way to install CodeIgniter 4. If you do not have composer you need to install it first. Composer is a tool which can manage the libraries you need to install/update for PHP. You can download composer from https://getcomposer.org. It is quick and easy. Once composer is installed go to command prompt and type composer. If it is installed successfully, it will show options and commands available for composer as below. If you can see them that means composer is installed.
Now, run windows command prompt and go to the desired folder where you want CodeIgniter 4 to be installed for the project. For me, I created a folder named "projects" in D: drive. So, I go to
D:/projects in command prompt. As I am installing it with project name as ci4-test, I will run below command from
D:/projects in command prompt.
> composer create-project codeigniter4/appstarter ci4-test
Below is a screenshot of of the command I used:
D:/projects/ci4-test will be my root directory for the project. Run the command when you are ready. It will take 2-3 minutes to complete the installation. When installation is complete, a folder named ci4-test (for my case) will be created under
D:/projects and CodeIgniter 4 will be installed with a skeleton application. There will be a set of files and folders as given below:
If you do not want to install using composer, you can download the latest version of CodeIgniter 4 and extract it to your project root folder. You can download it from https://codeigniter.com/download. It is a zip file; extract it to make it project root folder. After extract is complete below will be the folders and files in project root folder.
Basic Configuration and Testing
Before we test it, let us do some basic setup or verify few things. These setups are necessary irrespective of composer or manual installation.
- Make sure
'mbstring' extensions are installed. Please check CodeIgniter user guide about server requirements.
php.ini (xampp/php/php.ini) in notepad and find the lines with
extension=curl, extension=intl, extension=mbstring, all these lines should be uncommented to enable these extensions.
- Open the file
App.php from app/Config folder in notepad and update baseURL as
http://localhost:8080/ and make index page as blank.
- We need to update the environment as development. Copy
env file located in project root folder to
.env. For my case
env file is located as shown below and I copied it to
.env file in notepad and go to the line containing
"# CI_ENVIRONMENT = production". Make a copy of this line and paste it as new line, then update the environment from production to development and remove # from the beginning of the line. See below:
Test using PHP Local Web Server
You can use PHP built-in web server as local Development Server and test your application. Go to command prompt and type in below command from your project root folder. For me I have my project root folder as
d:/projects/ci4-test, so I go to this folder and enter below command line:
> php spark serve
After you enter this command line, local server will be started in port 8080.
So, now you can run http:// localhost:8080 in browser and see if you get the below screen. If you see this, then your installation is complete.
Test For Database Connection
If you want to connect to MySQL database and display data in a view from a database table, you need to setup database connection and create controller, model and view.
Setup database connection. I will use my demo database and to create a table with sample data.
Use below scripts to create a table and insert data in it in MySQL database.
After you create the table, please verify if table structure is same as above. Also make sure all rows are populated. I have just inserted few rows in the table for displaying purpose. Below are the rows in this table:
Update Database.php in app/Config
As I mentioned before, I am using a database named 'demo' in localhost with user 'root', so below is our database connection setup in
Database.php. Update $default as below:
We will create a new Controller, a Model and a View for our testing. Our new Controller is
Product.php, Model is
ModelProduct.php and View is
Basically, controller calls a model method to get all products and then send to a view. The view displays the data in a html table. Code for controller, model and view are given below, you can copy and save them in the respective folders with the same name.
Update default controller. open
app/Config/Routes.php and update default controller as Product:
Also change below line to include Product controller.
We are all set to test for database connection now. Go to command prompt and from your project root, start PHP Local Server by using command line "php spark serve" as described before. Then, in browser run
http://localhost:8080/. You should see below screen displaying the products in a html table:
If you can see above page, then testing for database connection is complete.
Installing and Testing CodeIgniter 4 using XAMPP (Apache Web Server)
If you are using XAMPP then you can install CodeIgniter 4 in a project folder under
xampp/htdocs. It could be a composer or manual installation. For me, I installed using composer in xampp/htdocs/ci4-test. So this is my project root folder.
Once installation is done, do the same basic configurations as explained above except for baseURL. Here, use
"public $baseURL = 'http://localhost/';" in
App.php from app/Config folder.
Testing in Apache Web Server in Windows (XAMPP)
Check few things for Apache Web server
xampp\apache\conf\httpd.conf in notepad and make sure the line
"LoadModule rewrite_module modules/mod_rewrite.so" is uncommented.
- Also make sure "AllowOverride All" and "Require all granted" options are available for the element
<Directory "D:/xampp/htdocs"> in the same file (xampp\apache\conf\httpd.conf).
- For virtual hosting, open
xampp\apache\conf\extra\httpd-vhosts.conf in notepad and make sure the line "LoadModule vhost_alias_module modules/mod_vhost_alias.so" is uncommented. Also add an element in this file at the end for a server name. For my case I gave the server name as ci4test.com, so I added below element in the same file for virtual hosting:
Document Root should be
"D:/xampp/htdocs/ci4-test/public" and my server name is ci4test.com.
- Now add an etc/hosts entry for the server name. Open
C:/Windows/System32/drivers/etc/hosts file in notepad and add an entry as below:
Now, restart Apache Web Server from XAMPP control panel and run
http://ci4test.com in browser.
To test database connection for Apache Web Server in XAMPP, follow the same steps described above for database testing.