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. Show Cake\View\View::element(string $elementPath, array $data, array $options =[] There are three arguments to the above function as follows −
Out of the 3 arguments, the first one is compulsory, while the rest are optional. ExampleCreate 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.phpElement 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 OutputUpon execution, the above URL will give you the following output.
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: BAB 2 Memulai CakePHP BAB 3 CakePHP dan Database BAB 4 Operasi CRUD BAB 5 Teknik CakePHP BAB 6 Components BAB 7 Proyek Membuat Web Blog Daftar Pustaka Buku Lainnya |