I'm learning and writting MVC by php. I set value of variable from file controller and i want to get value in file views but it's not working. My controller:
class BooksController extends Controller
{
function list()
{
$books = $this->model->get_all();
render('view/book/list');
}
}
My views list.php:
<?php
var_dump($books);
?>
My function render:
function render($file_name,$extent="php"){
$file_name = 'view/'.$file_name.'.'.$extent;
if(!file_exists($file_name))
throw new Exception("Can't open file ".$file_name);
require_once $file_name;
}
Result: NULL
I trying replace:
render('view/book/list');//not work
to
require_once('view/book/list.php');//work
Pls tell me, What is wrong?
You are passing view/book/list.php to
$file_name = 'view/'.$file_name.'.'.$extent;
Which will result in $filename = 'view/view/book/list.php.php'
Dumping your variables on the screen or in a log file might help you in the future in situations like that. See http://php.net/manual/en/function.var-dump.php
Try
render('book/list');
You have 2 times view
when it goes in your function render and pass $books
in your function render
also.
class BooksController extends Controller
{
function list()
{
$books = $this->model->get_all();
render('book/list',$books);
}
}
And in your function render
function render($file_name,$data,$extent="php"){
$file_name = 'view/'.$file_name.'.'.$extent;
$books = $data;
if(!file_exists($file_name))
throw new Exception("Can't open file ".$file_name);
require_once $file_name;
}