Tugas 9
Membuat Website CRUD dengan Codeigniter
Pada tugas kali ini saya
membuat website menggunakan codeigniter 4. Code Editor yang saya gunakan adalah
Visual Studio Code, dan saya host web secara local menggunakan XAMPP.
Langkah-langkah :
1. Download dan
install XAMPP di komputer anda
2. Install
codeigniter pada folder htdocs di folder xampp
3. Pada folder
project yang dibuat, buka file app/config/Routes.php. Tambahkan codes
berikut:
$routes->get('/', 'Home::index');
$routes->group('admin', function($routes){
$routes->get('mahasiswa', 'mhs::index');
$routes->get('mahasiswa/(:segment)/preview', 'mhs::preview/$1');
$routes->add('mahasiswa/new', 'mhs::create');
$routes->add('mahasiswa/(:segment)/edit', 'mhs::edit/$1');
$routes->get('mahasiswa/(:segment)/delete', 'mhs::delete/$1');
4. Pada app/Models tambahkan model baru, misal MhsModel.php dengan codes dibawah:
<?php
namespace App\Models;
use CodeIgniter\Model;
class MhsModel extends Model
{
protected $table = 'mahasiswa';
protected $primaryKey = 'NRP';
protected $useAutoIncrement = false;
protected $allowedFields = ['NRP', 'Nama', 'Alamat', 'NoHP', 'Jurusan'];
}
5.Buat controller baru pada app/Controllers, misal
Mhs.php dengan codes dibawah:
<?php namespace App\Controllers;
use \App\Models\MhsModel;
use CodeIgniter\Exceptions\PageNotFoundException;
class Mhs extends BaseController
{
public function index()
{
$news = new MhsModel();
$data['newses'] = $news->findAll();
echo view('admin_list', $data);
}
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
public function create()
{
// lakukan validasi
$validation = \Config\Services::validation();
$validation->setRules(['NRP' => 'required']);
$isDataValid = $validation->withRequest($this->request)->run();
// jika data valid, simpan ke database
if($isDataValid){
$news = new MhsModel();
$news->insert([
"NRP" => $this->request->getPost('NRP'),
"Nama" => $this->request->getPost('Nama'),
"Alamat" => $this->request->getPost('Alamat'),
"NoHP" => $this->request->getPost('NoHP'),
"Jurusan" => $this->request->getPost('Jurusan'),
]);
return redirect('admin/mahasiswa');
}
// tampilkan form create
echo view('admin_create');
}
//--------------------------------------------------------------------------
public function edit($id)
{
// ambil artikel yang akan diedit
$news = new MhsModel();
$data['news'] = $news->where('NRP', $id)->first();
// lakukan validasi data artikel
$validation = \Config\Services::validation();
$validation->setRules([
'NRP' => 'required',
'Nama' => 'required'
]);
$isDataValid = $validation->withRequest($this->request)->run();
// jika data vlid, maka simpan ke database
if($isDataValid){
$news->update($id, [
"NRP" => $this->request->getPost('NRP'),
"Nama" => $this->request->getPost('Nama'),
"Alamat" => $this->request->getPost('Alamat'),
"NoHP" => $this->request->getPost('NoHP'),
"Jurusan" => $this->request->getPost('Jurusan'),
]);
return redirect('admin/mahasiswa');
}
// tampilkan form edit
echo view('admin_edit', $data);
}
//--------------------------------------------------------------------------
public function delete($id){
$news = new MhsModel();
$news->delete($id);
return redirect('admin/mahasiswa');
}
}
6. Tambahkan views yang sesuai, yaitu:
admin_create.php, admin_edit.php, dan admin_list.php.
Comments
Post a Comment