CodeIgniter4
CodeIgniter is an Application Development Framework – a toolkit – for people who build web sites using PHP. Its goal is to enable you to develop projects much faster than you could if you were writing code from scratch, by providing a rich set of libraries for commonly needed tasks, as well as a simple interface and logical structure to access these libraries. CodeIgniter lets you creatively focus on your project by minimizing the amount of code needed for a given task.
Installation
CodeIgniter has two supported installation methods: manual download, or using Composer.
- We recommend the Composer installation because it keeps CodeIgniter up to date easily.
- If you would like the simple “download & go” install that CodeIgniter3 is known for, choose the manual installation.
Composer Installation
Composer can be used in several ways to install CodeIgniter4 on your system.
The first technique describes creating a skeleton project using CodeIgniter4, that you would then use as the base for a new webapp. The second technique described below lets you add CodeIgniter4 to an existing webapp,
Installation
In the folder above your project root:
> composer create-project codeigniter4/appstarter project-root
The command above will create a “project-root” folder.
If you omit the “project-root” argument, the command will create an “appstarter” folder, which can be renamed as appropriate.
If you don’t need or want phpunit installed, and all of its composer dependencies, then add the –no-dev option to the end of the above command line. That will result in only the framework, and the three trusted dependencies that we bundle, being composer-installed.
A sample such installation command, using the default project-root “appstarter”:
> composer create-project codeigniter4/appstarter –no-dev
Initial Configuration
After installation, a few initial configurations are required. See Initial Configuration for the details.
Upgrading
Whenever there is a new release, then from the command line in your project root:
> composer update
Build Your First Application
Overview
This blog is intended to introduce you to the CodeIgniter4 framework and the basic principles of MVC architecture. It will show you how a basic CodeIgniter application is constructed in a step-by-step fashion.
If you are not familiar with PHP, we recommend that you check out the W3Schools PHP Tutorial before continuing.
In this Blog , you will be creating a basic news application. You will begin by writing the code that can load static pages. Next, you will create a news section that reads news items from a database. Finally, you’ll add a form to create news items in the database.
This Blog will primarily focus on:
- Model-View-Controller basics
- Routing basics
- Form validation
- Performing basic database queries using CodeIgniter’s Model
The entire Blog is split up over several pages, each explaining a small part of the functionality of the CodeIgniter framework. You’ll go through the following pages:
- Introduction, this page, which gives you an overview of what to expect and gets your default application downloaded and running.
- Static pages, which will teach you the basics of controllers, views and routing.
- News section, where you’ll start using models and will be doing some basic database operations.
- Create news items, which will introduce more advanced database operations and form validation.
- Conclusion, which will give you some pointers on further reading and other resources.
Enjoy your exploration of the CodeIgniter framework.
Getting Up and Running
Installing CodeIgnier
You can download a release manually from the site, but for this Blog we will use the recommended way and install the AppStarter package through Composer. From your command line type the following:
> composer create-project codeigniter4/appstarter ci-news
This creates a new folder, ci-news, which contains your application code, with CodeIgniter installed in the vendor folder.
Setting Development Mode
By default, CodeIgniter starts up in production mode. This is a safety feature to keep your site a bit more secure in case settings are messed up once it is live. So first let’s fix that. Copy or rename the env file to .env. Open it up.
This file contains server-specific settings. This means you never will need to commit any sensitive information to your version control system. It includes some of the most common ones you want to enter already, though they are all commented out. So uncomment the line with CI_ENVIRONMENT on it, and change production todevelopment:
CI_ENVIRONMENT = development
Running Development Server
With that out of the way it’s time to view your application in a browser. You can serve it through any server of your choice, Apache, Nginx, etc, but CodeIgniter comes with a simple command that takes advantage of PHP’s built-in server to get you up and running fast on your development machines. Type the following on the command line from the root of your project:
> php spark serve
The Welcome Page
Now point your browser to the correct URL you will be greeted by a welcome screen. Try it now by heading to the following URL:
http://localhost:8080