Cakephp: Creating and Sending Parameters Between Controller and View

Cakephp Step By Step Tutorial: We still wok with our first application at cakephp. But we will build controller and view better. Create parameters in controller and send it to view. It will make our application more flexible.
Create a new file books_controller.php within D:\xampp\htdocs\cakephp\app\controllers. Overwrite become like this:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
    class BooksController extends AppController {
        var $Books = 'Books';
        var $uses=null;
       function index() {
          $this->set('page_heading', 'Packt Book Store');
          $book  = array (
                   'book_title'   => 'Object Oriented Programming
                                                        with PHP5',
                   'author'     => 'Hasin Hayder',
                   'isbn'        => '1847192564',
                   'release_date' => 'December 2007'
                  );
          $this->set($book);
          $this->pageTitle = 'Welcome to the Packt Book Store!';
       }
    }
?>

Now create a folder books in side D:\xampp\htdocs\cakephp\app\views
And inside books create a file index.ctp. Rewrite with following code:
1
2
3
4
5
6
7
<h2><?php echo $page_heading; ?></h2>
   <dl>
   <lh><?php echo $book_title; ?></lh>
   <dt>Author:</dt><dd><?php echo $author; ?></dd>
   <dt>ISBN:</dt><dd><?php echo $isbn; ?></dd>
   <dt>Release Date:</dt><dd><?php echo $release_date; ?></dd>
   </dl>
Point your browser to http://localhost:85/cakephp/books.

. Bookmark the permalink.

Leave a Reply