Cara menggunakan cakephp elements


Certain parts of the web pages are repeated on multiple web pages, but at different locations. CakePHP can help us reuse these repeated parts. These reusable parts are called Elements - help box, extra menu, etc. An element is basically a mini-view. We can also pass variables in elements.

Cake\View\View::element(string $elementPath, array $data, array $options =[]

There are three arguments to the above function as follows −

  • The first argument is the name of the template file in the /src/Template/element/ folder.

  • The second argument is the array of data to be made available to the rendered view.

  • The third argument is for the array of options. e.g. cache.

Out of the 3 arguments, the first one is compulsory, while the rest are optional.

Example

Create an element file at src/Template/element directory called helloworld.php. Copy the following code in that file.

src/Template/element/helloworld.php

<p>Hello World</p>

Create a folder Elems at src/Template and under that directory create a View file called index.php. Copy the following code in that file.

src/Template/Elems/index.php

Element Example: <?php echo $this->element('helloworld'); ?>

Make Changes in the config/routes.php file as shown in the following program.

config/routes.php

<?php
use Cake\Http\Middleware\CsrfProtectionMiddleware;
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;
$routes->setRouteClass(DashedRoute::class);
$routes->scope('/', function (RouteBuilder $builder) {
   $builder->registerMiddleware('csrf', new CsrfProtectionMiddleware([
      'httpOnly' => true,
   ]));
   $builder->applyMiddleware('csrf');
   $builder->connect('/element-example',['controller'=>'Elems','action'=>'index']);
   $builder->fallbacks();
});

Create an ElemsController.php file at src/Controller/ElemsController.php. Copy the following code in the controller file.

src/Controller/ElemsController.php

<?php
   namespace App\Controller;
   use App\Controller\AppController;
   class ElemsController extends AppController{
      public function index(){
      }
   }
?>

Execute the above example by visiting the following URL http://localhost/cakephp4/element-example

Output

Upon execution, the above URL will give you the following output.

Cara menggunakan cakephp elements


Cara menggunakan cakephp elements

Penulis Agus Saputra
Harga Buku Rp. 42.800
Jml Halaman 143
Penerbit CV. Lokomedia
Terbit Februari 2011

CakePHP hadir sebagai alternatif bagi Anda yang masih kesulitan mempelajari framework. Sesuai dengan namanya, CakePHP  menawarkan kemudahan dalam membuat aplikasi web dengan cepat (RAD: Rapid Application Development), menjadikan belajar framework se-enak menikmati kue favorit. Contohnya, dengan fitur Scaffolding, Anda bisa membuat operasi CRUD (Create, Read, Update, Delete) hanya dengan 3 baris kode, padahal kalau Anda membuatnya secara manual membutuhkan bahkan ratusan baris kode.

CakePHP merupakan framework yang memiliki segudang fitur juga sudah support Ajax dan ORM (Object Relational Model), namun terbatasnya panduan dan tutorialnya menjadi kesulitan tersendiri untuk mempelajarinya. Dengan adanya buku ini, ternyata belajar CakePHP tidak sesulit yang dibayangkan. Buku dimulai dengan Dasar-Dasar Framework CakePHP yang membahas mulai dari Definisi, Instalasi, Konfigurasi, Penanganan HTML dan Database, Operasi CRUD, Teknik CakePHP (Scaffolding, Paging, Upload File, dll), Components (Authentication & Session), dan terakhir ditutup dengan Proyek Lengkap Pembuatan Web Blog.

Bonus: CD

klik disini untuk melihat cover dalam ukuran besar.

Spesifikasi Daftar Isi:
Kata Pengantar
BAB 1 Pengenalan Dasar CakePHP

1.1. Apa itu CakePHP?
1.2. Keuntungan dan Kelemahan CakePHP
1.3. Fitur-Fitur CakePHP
1.4. Mengenal Konsep MVC
1.5. Struktur Folder CakePHP
1.6. Kebutuhan Tools Dasar CakePHP
       1.6.1. Instalasi Web Server
       1.6.2. Instalasi CakePHP

BAB 2 Memulai CakePHP
2.1. Menentukan Layout Awal
2.2. Welcome to CakePHP Framework
2.3. Penanganan HTML
       2.3.1. Charset
       2.3.2. CSS
       2.3.3. Meta
       2.3.4. docType
       2.3.5. Style
       2.3.6. Image
       2.3.7. Link
2.4. Penanganan Form
       2.4.1. Form Open
       2.4.2. Form Close
       2.4.3. Form Input Text
       2.4.4. Form Input Hidden
       2.4.5. Form Input Password
       2.4.6. Form Input Upload
       2.4.7. Form Textarea
       2.4.8. Form Label
       2.4.9. Form Dropdown
       2.4.10. Form Checkbox
       2.4.11. Form Radio
       2.4.12. Form Submit
       2.4.13. Form Button
       2.4.14. Form Reset

BAB 3 CakePHP dan Database
3.1. Membangun Database
3.2. Konfigurasi Database
3.3. Penanganan Database
       3.3.1. Fungsi First
       3.3.2. Fungsi All
       3.3.3. Fungsi Count
       3.3.4. Fungsi List
       3.3.5. Fungsi Neighbors
       3.3.6. Fungsi Threaded
3.4. Menampilkan Record
3.5. Mengurutkan Record
3.6. Menampilkan Kolom Secara Spesifik
3.7. Menampilkan Record Secara Spesifik

BAB 4 Operasi CRUD
4.1. Model
4.2. Controller
4.3. View
       4.3.1. Operasi Create (Menambah Data)
       4.3.2. Operasi Read (Membaca Data)
       4.3.3. Operasi Update (Mengubah Data)
       4.3.4. Operasi Delete (Menghapus Data)

BAB 5 Teknik CakePHP
5.1. Teknik Scaffolding
5.2. Teknik Pagination
5.3. Teknik Paginator (Sort By)
5.4. Teknik Validation
5.5. Menu Tree
5.6. Menangani Upload File
5.7. Menghapus File yang Telah di Upload

BAB 6 Components
6.1. Authentication
       6.1.1. Membuat Tabel User
       6.1.2. Membuat Model User
       6.1.3. Membuat Controller Global
       6.1.4. Membuat Controller User
       6.1.5. Membuat Form Login
       6.1.6. Menambah User
       6.1.7. Menambah Status Login
6.2. Session
       6.2.1. Write and Read Session
       6.2.2. Destroy Session

BAB 7 Proyek Membuat Web Blog
7.1. Menentukan Layout
7.2. Membuat Database dan Tabel
7.3. Konfigurasi Database
7.4. Membuat Controller Global (Links)
7.5. Membuat Halaman Articles untuk User
7.6. Membuat Halaman Comments untuk User
7.7. Membuat Halaman Login Admin
7.8. Manajemen Articles untuk Admin
       7.8.1. Menampilkan Articles untuk Admin
       7.8.2. Menambah Articles untuk Admin
       7.8.3. Menampilkan Detail Articles untuk Admin
       7.8.4. Mengedit Articles untuk Admin
7.9. Mempercantik Paging
7.10. Catatan Akhir CakePHP

Daftar Pustaka

Buku Lainnya