<?php
namespace App\Controller;
use App\Controller\AppController;
use App\View\AppView ;
use Cake\View\Helper;
class BookController extends AppController {
public function initialize()
{
parent::initialize() ;
$this->loadModel("Books");
$this->viewBuilder()->layout("booklayout");
$this->loadComponent('Csrf');
}
public function index()
{
$books = $this->Books->find("all" ,["order"=>["book_id"=>"desc"]]);
$this->set("title" , "Book List");
$this->set("books" , $books);
}
public function delete($id){
$this->autoRender = false ;
$book = $this->Books->get($id);
$this->Books->delete($book);
$this->Flash->set('book has been deleted' ,["element"=>"book_success"]);
$this->redirect(["action"=>"index"]);
}
index.ctp file
<div class="panel panel-primary">
<div class="panel-heading">Book List
<?php echo $this->Html->link(
"Create Book" ,
"/Book/create/" ,
[
"class"=>"btn btn-info pull-right" ,
"style"=>"margin-top :-6px"
]
);
?>
</div>
<div class="panel-body">
<table class="table">
<thead>
<tr>
<th>Sr. No</th>
<th>Book Name</th>
<th>Author</th>
<th>Email Address</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$count = 1;
foreach($books as $key=>$book){ ?>
<tr>
<td><?= $count++;?></td>
<td><?= $book->name; ?></td>
<td><?= $book->author; ?></td>
<td><?= $book->email ;?></td>
<td>
<?php
echo $this->Html->link(
"Edit" ,
"/book/edit/".$book->book_id ,
[
"class"=>"btn btn-info"
]
);
echo $this->Html->link(
"delete" ,
"/book/delete/".$book->book_id ,
[
"class"=>"btn btn-danger"
]
);
?>
</td>
</tr>
<?php }?>
</tbody>
</table>
</div>
</div>
Where exactly does that error occur? The code you have shown here looks like it should work, so I wonder if it’s in an event handler somewhere.