Gunakan form dibawah untuk mencari aplikasi yg anda butuhkan via internet
Google

Senin, 12 Mei 2008

Bake dan Scaffolding Pada CakePhp

Dalam website resmi CakePhp(http://www.cakephp.org) Cakephp disebut sebagai Rapid Application Framework Salah satu alasannya adalah karena Cake memiliki fitur Scaffold dan Bake.

Bake
adalah fitur CakePhp yang mampu menggenerate model, view, dan controller (MVC) secara otomatis sesuai dengan database yang ada pada konfigurasi. Bake cocok digunakan sebagai template, karena Bake akan otomatis menghasilkan kode kode umum yang sering digunakan (kode kode CRUD) yang bisa diubah ataupun ditambahkan sesuai kebutuhan.

Untuk menggunakan fitur ini cukup mudah masuklah pada direktori cake/scripts pada instalasi CakePhp lalu ketikkan ketikkan “php bake.php”. Misal akan dibuat aplikasi yang menggunakan tabel Log.

Contoh penggunaan Bake pada pembuatan model

[M]odel[C]ontroller[V]iewPlease select a class to Bake: (M/V/C)>m—————————————————————

Model Bake:
—————————————————————
Possible models based on your current database:

1. Log

Enter a number from the list above, or type in the name of another model.

> 1

Would you like to supply validation criteria for the fields in your model? (y/n)[y]

> n

Would you like to define model associations (hasMany, hasOne, belongsTo, etc.)? (y/n)[y]

> n

—————————————————————
The following model will be created: —————————————————————

Model Name: Log

DBConnection: default

Model Table: logs

Validation: Array

(

)
—————————————————————
Look okay? (y/n)

[y] > y

Creating file /var/www/html/cake//app/models/log.php

File /app/models/log.php exists, overwrite? (y/n/q):y

Do you want to bake unit test files anyway? (y/n)[y] > n

Hasil eksekusi bake di atas akan

menghasilkan file app/model/log.php

<?
class Log extends AppModel{
}
?>

Contoh penggunaan bake pada pembuatan controller
Please select a class to Bake: (M/V/C)> c---------------------------------------------------------------

Controller Bake:
---------------------------------------------------------------
Controller name? Remember that Cake controller names are plural.

> logs

Would you like bake to build your controller interactively?

Warning: Choosing no will overwrite Logs controller if it exist. (y/n)[y]

> y

Would you like this controller to use other models besides ‘Log’? (y/n)[n]

> n

Would you like this controller to use other helpers besides HtmlHelper? (y/n)[n]

> n

Would you like this controller to use any components? (y/n)[n]

> n

Would you like to include some basic class methods (index(), add(), view(), edit())? (y/n)[n]

> y

—————————————————————

The following controller will be created:

—————————————————————

Controller Name: logs

—————————————————————

Look okay? (y/n)[y]

> y

Creating file /var/www/html/cake//app/controllers/logs_controller.php

File /app/controllers/logs_controller.php exists, overwrite? (y/n/q):y

Wrote /app/controllers/logs_controller.php

Cake test suite not installed.

Do you want to bake unit test files anyway? (y/n)[y]

> n

Hasil pengeksekusian di atas akan menghasilkan file

app/controllers/Logs_Controller.php


Penggunaan bake pada pembuatan view

Please select a class to Bake: (M/V/C)---------------------------------------------------------------

View Bake:

---------------------------------------------------------------
Controller Name? (plural)

> logsWould you like bake to build your views interactively

?Warning: Choosing no will overwrite views if it exist.

(y/n)[y]

> y

Would you like to create some scaffolded views (index, add, view, edit) for this controller?

NOTE: Before doing so, you’ll need to create your controller and model classes

(including associated models). (y/n)[n]

> y

Creating file /var/www/html/cake//app/views/logs/index.thtml

Wrote /app/views/logs/index.thtml

Creating file /var/www/html/cake//app/views/logs/view.thtml

Wrote /app/views/logs/view.thtml

Creating file /var/www/html/cake//app/views/logs/add.thtml

Wrote /app/views/logs/add.thtml

Creatingfile /var/www/html/cake//app/views/logs/edit.thtml

Wrote /app/views/logs/edit.thtml—————————————————————Note:- If you’re using a non-domain install, change URL pathsfrom /controller/action to /cake_install/controller/action—————————————————————View Scaffolding Complete.

Pengeksekusian di atas akan menghasilkan file file :


app/views/logs/add.thtml
app/views/logs/index.thtml
app/views/logs/edit.thtml
app/views/logs/view.thtml


Scaffolding memiliki fungsi yang hampir sama denganBake, hanya saja dengan scaffolding tidak ada kode yang mesti kita buat hanya membuat kelas model dan controller. Lalu mendeklarasikan variabel scaffold pada controller.
Contoh model yang menggunakan fitur scaffolding
<?
class Log extends AppModel{
}
?>

Contoh controller yang menggunakan fitur scaffolding

<?php
class LogsController extends AppController
{
var $scaffold;
}
?>


Kedua kelas di atas akan otomatis menghasilkan aplikasi web sederhana dengan kemampuan CRUD (Create Update Delete).

Kesimpulan:

CakePhp adalah sebuah Rapid ApplicationFramework yang bisa mempercepat
pembuatan sebuah aplikasi web. Salah satu fitur yang dimiliki CakePhp
adalah Bake dan Scaffolding. Dengan Bake dapat dibuat template untuk
masing masing model, view, dan controller sehingga developer tidak
perlu membuat kode dari awal. Sedang Scaffolding sangat berguna dalam membuat aplikasi web sederhana dengan fitur CRUD.

Tidak ada komentar: