Codeigniter is a php framework that lets you build dynamic websites with very less amount of code. It uses model view controller(MVC) architecture for its file structure. The MVC architecture is great for building dynamic pages by. Since, we need not to code separately for the repetitive task. It has vast set of php library for commonly needed task. So, it greatly reduces the amount of time and code to develop a website.
The Codeigniter 4 is the latest version. The directory structure of codeigniter 4 includes /app, /public, /system, /writable. The /app directory includes all the codes of your dynamic website. You will be building your website within /app directory. Though it contains lots of sub directories but /Config, /Controllers, /Model, /Views are mainly important for us and we will study more about it here. The /public directory will be your document root.
Let's start by installing Codeigniter 4. Go to the official website of Codeigniter 4 and Download.
Extract the files and keep it in the Apache server document root. If you are running Apache on windows machine, then htdocs will be your document root.
Create a virtual host for your new project and make this virtual host point to your /public directory of project. For details, refer to official docs.
Now, run your apache http server. Say, name of the newly creted virtual host is myproject.local. Open browser and type myproject.local in the browser.
/app/Config – It contains application specific files which one need to configure for building a website using codeigniter.
/Config/App.php – It contains a public variable $baseURL. The default line contains public $baseURL = 'http://example.com/';. You need to configure it to make it to point to your website.
/Config/Database.php - It is the database confguration files which need to be configured to make a database connection.
public $default = [ 'DSN' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => 'Sanivaar1!', 'database' => 'test1', 'DBDriver' => 'MySQLi', 'DBPrefix' => '', 'pConnect' => true, 'DBDebug' => (ENVIRONMENT !== 'development'), 'charset' => 'utf8', 'DBCollat' => 'utf8_general_ci', 'swapPre' => '', 'encrypt' => false, 'compress' => false, 'strictOn' => false, 'failover' => , 'port' => 3306, ];
You need to configure hostname,username,password and database here.
/Config/Routes.php - Routing rules are defined in this file.
It contains an array $routes that lets you specify your own routing criteria.
Manually specifying Routes gives us a performance boost because in that case the application it need not scan all directories.
All the routes contains a directory structure that should match with the url.
All the routes contains a Controller and a function that will handle the url.
Let's take a basic example $routes->get('/jobs/create', 'Home::create');. Here, /jobs/create points to the create.php file of the /jobs directory which is inside the /Views. Home::create denotes the Controller and function respectively.
What this means, whenever the application sees any url that ends with /jobs/create, it will be handled by the create() function of Home Controller.
Useful links, One time temporary link for email verification
/app/Controllers - It is the directory where you actually build your php project. It is the component that actually binds the View with the Model. The Controller receives input from the users and performs different kind of operations like saving it into database. At the same time Controller also reads data from database and present it to database. Every Controller in the Codeigniter is basically a Class.
The other responsibility of the controller is to handle everything that pertains to HTTP requests - redirects, authentication, web safety, encoding, etc. The Controllers contains different functions to handle Views.
/app/Models - Models actually provide a way to Controllers to connect with the table in the database. The Models provides different helper methods to read, insert, update and delete records into the table.
/app/Views - It conatins simple HTML snippets with very few php code of simple logic. Every Controller function should point to a file in in Views directory. The View helps the developer in how to reperesent the data to the users.
The Controller controls the behaviour of data, the Model communicates with database and View actually displays the data. All combined help us build dynamic, scalabale and efficient websites.
Published on Sept 19, 2021