Я пытаюсь понять шаблон MVC, и у меня есть некоторые проблемы с ним. вот пример, который работает и является полуавтоматическим шаблоном MVC.Принципы MVC понимают, почему
class model {
public $string;
public function __construct(){
$this->string = 'awsome MVC';
}
}
class view {
private $model;
private $controller;
public function __construct($controller, $model){
$this->controller = $controller;
$this->model = $model;
}
public function output(){
return '<b>'.$this->model->string.'</b>';
}
}
class controller {
private $model;
public function __construct($model){
$this->model = $model;
}
}
$model = new Model();
$controller = new controller($model);
$view = new view($controller, $model);
echo $view->output();
У меня проблемы с этим, почему это вид обработки больше, чем контроллер? ниже у меня есть мой (я думаю) сценарий MVC, и я надеюсь, что вы, ребята, можете указать, что я делаю неправильно, и почему приведенный выше пример лучше.
<?php
/*
* this is the view
*/
class template
{
function html($input){
return 'template : <b>'.$input.'</b>';
}
}
/*
* this is the model
*/
class database
{
function output(){
return 'this is a news title';
}
}
/*
* this is the basic controller
*/
class basicController
{
public $model;
public $view;
public function loadModel($model){
$this->model = new $model;
}
public function loadView($view){
$this->view = new $view;
}
}
/*
* this is the extended controller
*/
class newsController extends basicController
{
public function __construct(){
$this->loadModel('database');
$this->loadView('template');
}
public function showAction(){
return $this->view->html($this->model->output());
}
}
/*
* calling
*/
$controller = new newsController;
echo $controller->showAction();
?>
Где именно вы обнаружили, что * «официальный официальный образец MVC» *, потому что это дерьмо. Просмотр не должен иметь доступ к контроллеру. –
http://r.je/mvc-in-php.html – SinisterGlitch
http://www.sitepoint.com/the-mvc-pattern-and-php-1/ – SinisterGlitch