MEMBUAT SISTEM KLINIK DENGAN CODEIGNITER 4 - TEMPLATING LOGIN & REGISTER

3 min read 2 months ago
Published on Aug 29, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

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

  1. Install XAMPP or MAMP to create a local server:

  2. 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) or Applications/MAMP/htdocs (for MAMP).
  3. 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/';
      

Step 2: Setting Up the Database

  1. 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).
  2. 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
      );
      

Step 3: Configuring the Database Connection

  1. 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' => [],
      ];
      

Step 4: Setting Up User Authentication

  1. Create Controllers for Login and Register:

    • Generate a new controller for user management:
      php spark make:controller Auth
      
  2. Implement Registration Logic:

    • In Auth.php, create a method for handling user registration and saving user data in the database.
  3. Implement Login Logic:

    • Create a method for handling user login and validating credentials.
  4. Create Views for Login and Register Forms:

    • Create corresponding view files in app/Views/auth/ for login and registration forms.

Step 5: Implementing Templating

  1. Use a Templating Engine:

    • Consider using a templating engine like Blade or simply create header and footer views to include in your main views.
  2. Load Views in Controller:

    • Use the view() method in your controller to load your header, footer, and content views.
  3. 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.