Example
Make changes in the config/routes.php file as shown in the following program.config/routes.php
<?php use Cake\Core\Plugin; use Cake\Routing\RouteBuilder; use Cake\Routing\Router; Router::defaultRouteClass('DashedRoute'); Router::scope('/', function (RouteBuilder $routes) { $routes->connect('extend',['controller'=>'Extends','action'=>'index']); $routes->fallbacks('DashedRoute'); }); Plugin::routes();Create a ExtendsController.php file at src/Controller/ExtendsController.php. Copy the following code in the controller file.
src/Controller/ExtendsController.php
<?php namespace App\Controller; use App\Controller\AppController; class ExtendsController extends AppController{ public function index(){ } } ?>Create a directory Extends at src/Template and under that folder create a View file called header.ctp. Copy the following code in that file.
src/Template/Extends/header.ctp
<div align = "center"><h1>Common Header</h1></div> <?= $this->fetch('content') ?>Create another View under Extends directory called index.ctp. Copy the following code in that file. Here we are extending the above view header.ctp.
src/Template/Extends/index.ctp
<?php $this->extend('header'); ?>
This is an example of extending view.
Execute the above example by visiting the following URL.http://localhost:85/CakePHP/extend
Output
Upon execution, you will receive the following output.
No comments:
Post a Comment