MEMBUAT SISTEM KLINIK DENGAN CODEIGNITER 4 - TEMPLATING LOGIN & REGISTER
Table of Contents
Introduction
In this tutorial, you will learn how to create a clinic system using CodeIgniter 4, focusing on the login and registration templating process. This guide will walk you through the essential steps to set up your application, making it suitable for beginners who want to dive into web development with PHP frameworks.
Step 1: Setting Up Your Environment
-
Install XAMPP or MAMP to create a local server:
- Download and install XAMPP from apachefriends.org or MAMP from mamp.info.
- Start the server.
-
Download CodeIgniter 4:
- Visit the CodeIgniter website and download the latest version of CodeIgniter 4.
- Extract the downloaded files into the
htdocs
directory (for XAMPP) orApplications/MAMP/htdocs
(for MAMP).
-
Configure the Base URL:
- Open the
app/Config/App.php
file. - Set the base URL by modifying the following line:
public $baseURL = 'http://localhost/your_project_name/';
- Open the
Step 2: Setting Up the Database
-
Create a Database:
- Open phpMyAdmin in your browser (usually at
http://localhost/phpmyadmin
). - Click on "Databases" and create a new database for your clinic system (e.g.,
clinic_db
).
- Open phpMyAdmin in your browser (usually at
-
Create Tables for Users:
- Use the following SQL command to create a
users
table:CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(100), password VARCHAR(255), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
- Use the following SQL command to create a
Step 3: Configuring the Database Connection
- Edit the Database Configuration:
- Open
app/Config/Database.php
. - Update the database connection settings to match your local setup:
public $default = [ 'DSN' => '', 'hostname' => 'localhost', 'username' => 'root', // or your database username 'password' => '', 'database' => 'clinic_db', 'DBDriver' => 'MySQLi', 'DBPrefix' => '', 'pConnect' => false, 'DBDebug' => (ENVIRONMENT !== 'production'), 'cacheOn' => false, 'cachedir' => '', 'DBCollat' => 'utf8_general_ci', 'DBCharset' => 'utf8', 'DBTempDir' => '', 'DBOptions' => [], ];
- Open
Step 4: Setting Up User Authentication
-
Create Controllers for Login and Register:
- Generate a new controller for user management:
php spark make:controller Auth
- Generate a new controller for user management:
-
Implement Registration Logic:
- In
Auth.php
, create a method for handling user registration and saving user data in the database.
- In
-
Implement Login Logic:
- Create a method for handling user login and validating credentials.
-
Create Views for Login and Register Forms:
- Create corresponding view files in
app/Views/auth/
for login and registration forms.
- Create corresponding view files in
Step 5: Implementing Templating
-
Use a Templating Engine:
- Consider using a templating engine like Blade or simply create header and footer views to include in your main views.
-
Load Views in Controller:
- Use the
view()
method in your controller to load your header, footer, and content views.
- Use the
-
Style Your Forms:
- Use CSS frameworks like Bootstrap to style your login and registration forms.
Conclusion
You've successfully set up a basic clinic system using CodeIgniter 4, including user registration and login functionalities. To enhance your application, consider implementing features such as password recovery, user roles, and a dashboard for clinic management. Explore more on CodeIgniter's documentation for advanced functionalities and best practices.