Table of Contents
Generate Controller Class
Format :
php spark make:controller [options]
Options:
- –bare: Extends from CodeIgniter\Controller instead of BaseController.
- –restful: Extends from a RESTful resource. Choices are controller and presenter. Defaults to controller.
- –namespace: Set the root namespace. Defaults to value of APP_NAMESPACE.
- –suffix: Append the component suffix to the generated class name.
- –force: Set this flag to overwrite existing files on destination.
Sebagai contoh misalkan kita ingin generate file controller dengan nama PostController, kita run command di bawah ini.
php spark make:controller PostController
Terdapat keterangan ada file baru yang berhasil dibuat, yaitu file :
- Controllers/PostController.php.
Selanjutnya kita coba buka file Controllers/PostController.php di text editor. Kita bisa lihat ada class controller, yaitu PostController dengan satu method default index().
Pada baris kode di atas, PostController secara default extends atau merupakan class turunan dari class BaseController.
Nah, misalkan kita ingin buat class dengan tujuan yang berbeda, misalnya untuk Restful resource. Kita bisa tambahkan opsi –restful.
php spark make:controller ProductController --restful
Sekarang kita coba buka file Controllers/ProductController.php di text editor.
Mirip seperti php artisan make:controller ProductController –resource di Laravel
Generate Model Class
Format
php spark make:model [options]
Options :
- –dbgroup: Database group to use. Defaults to default.
- –return: Set the return type from array, object, or entity. Defaults to array.
- –table: Supply a different table name. Defaults to the pluralized class name.
- –namespace: Set the root namespace. Defaults to value of APP_NAMESPACE.
- –suffix: Append the component suffix to the generated class name.
- –force: Set this flag to overwrite existing files on destination.
Sebagai contoh, kita coba buat class model baru dengan nama PostModel. Kita run command berikut ini.
php spark make:model PostModel
Kita buka file hasilnya Models/PostModel.php di text editor dan kurang lebih hasil generate-nya seperti baris kode di bawah ini :
Generate Migration File
Format
php spark make:migration [options]
Options :
- –session: Generate a migration file for database sessions.
- –table: Set the table name to use for database sessions. Defaults to ci_sessions.
- –dbgroup: Set the database group for database sessions. Defaults to default group.
- –namespace: Set the root namespace. Defaults to value of APP_NAMESPACE.
- –suffix: Append the component suffix to the generated class name.
- –force: Set this flag to overwrite existing files on destination.
Contohnya kita coba buat file migration untuk `Post` table. Kita run command berikut ini :
php spark make:migration Post
Kita buka file hasilnya app/Database/Migrations/_Post.php di text editor dan edit hasil generate-nya sbb :
forge->addField([
'id' => [
'type' => 'TINYINT',
'unsigned' => true,
'auto_increment' => true,
],
'title' => [
'type' => 'VARCHAR',
'constraint' => '50',
],
'content' => [
'type' => 'VARCHAR',
'constraint' => '200',
],
'created_at' => [
'type' => 'TIMESTAMP',
'default' => new RawSql('CURRENT_TIMESTAMP'),
],
'updated_at' => [
'type' => 'TIMESTAMP',
'default' => new RawSql('CURRENT_TIMESTAMP'),
],
'deleted_at' => [
'type' => 'TIMESTAMP',
'default' => new RawSql('CURRENT_TIMESTAMP'),
],
]);
$this->forge->addKey('id', true);
$this->forge->createTable('posts');
}
public function down()
{
$this->forge->dropTable('posts');
}
}
Untuk menabahkan table ke database sesuai file di atas jalankan perintah berikut
php spark migrate
Perintah di atas akan mendeploy keseluruhan file migration yang belum pernah dijalankan
Sementara untuk menjalankan file yang dipilih perinahnya sbb :
php spark make:migration Menu
Generate Seeder
Format :
make:seeder [options]
Options :
- –namespace: Set the root namespace. Defaults to value of APP_NAMESPACE.
- –suffix: Append the component suffix to the generated class name.
- –force: Set this flag to overwrite existing files on destination.
Database seeding adalah cara sederhana untuk menambahkan data ke dalam database. Biasanya database seed ini sangat bermanfaat pada saat development, terutama saat kita perlu sample data.
Untuk generate sebuah seeder class, kita jalankan perintah sbb :
php spark make:seeder PostSeeder
Selanjutnya kita coba buka file app/Database/Seeds/PostSeeder.php sbb :
save(
[
'title' => $faker->text,
'content' => $faker->text,
'created_at' => Time::createFromTimestamp($faker->unixTime()),
'updated_at' => Time::now()
]
);
}
}
}
Lalu jalankan perintah berikut di terminal
php spark db:seed PostSeeder
Command Generate Scaffolding
Pada CodeIgniter 4 tersedia command untuk generate beberapa class sekaligus, seperti controller, model, entity, migration dan seeder dengan satu command, yaitu make:scaffold command.
Sebagai contoh di sini kita coba generate class yang berhubungan dengan objek Post, kita run command berikut ini :
php spark make:scaffold Post --suffix --restful
Perintah di atas akan mengenerate beberapa class sekaligus sbb :
- App\Controllers\PostController.php
- App\Models\PostModel.php
- App\Database\Migrations\_PostMigration.php and
- App\Database\Seeds\PostSeeder.php
Note : tanpa menggunakan options –suffix semua file/class akan bernama Post.php dimana akan membuat error jika dipanggil dari class yang bernama sama tanpa menggunakan alias. Sementara –restful akan menambahkan beberapa function crud pada Controller