J'ai entrepris la conversion de mon skeleton de transaction vers le modèle MVC tant décrié par de nombreux modérateurs de ce site.
Il y a 2 tutos disponibles et j'ai choisi le second car a une approche objet quitte à s'inspirer plus tard du 1er....
Mais j'ai d'abord des erreurs de parsing à solutionner.
Voici mon index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test MVC</title>
</head>
<body>
<?php
echo "test MVC" ;
require_once ($_SERVER["DOCUMENT_ROOT"] . "/class/controller.php") ;
$front = Controller::getInstance()->dispatch();
public function launch(Request $request, Response $response)
==>17 { $variable = 'essai de variable';
$response->addVar('unevariable',$variable);
$this->render(dirname(__FILE__).'/show.php');
$this->printOut();
}
echo " successfully" ;
?>
</body>
</html> |
Et j'ai une erreur que voici;
PHP Parse error: syntax error, unexpected T_PUBLIC in F:\WebSites\mvc\index.php on line 17
Voici le controller.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| <?php
require_once ($_SERVER['DOCUMENT_ROOT'] . "/class/request.php") ;
require_once ($_SERVER['DOCUMENT_ROOT'] . "/class/response.php") ;
class Controller
{ private $_defaults = array('module' => 'index', 'action' => 'index');
private $_request;
private $_response;
private static $_instance = null;
private function __construct()
{
$this->_request = new Request();
$this->_response = new Response();
}
public static function getInstance()
{ if (is_null(self::$_instance))
self::$_instance = new self();
return self::$_instance;
}
public function dispatch($defaults = null)
{ $parsed = $this->_request->route();
$parsed = array_merge($this->_defaults, $parsed);
$this->forward($parsed['module'], $parsed['action']);
}
public function forward($module, $action)
{ $command = $this->_getCommand($module, $action);
$command->launch($this->_request, $this->_response);
}
private function _getCommand($module, $action)
{ if (!file_exists($path = "$module/$action.php"))
throw new Exception("Commande inconnue $module/$action.php");
require($path);
$class = $action.'Action';
return new $class($this);
}
public function getResponse()
{
return $this->_response;
}
public function redirect($url)
{
$this->_response->redirect($url);
}
public function render($file)
{
$view = new View();
$this->_response->setBody($view->render($file,$this->_response->getVars()));
}
} |
Le code request et response.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| <?php
class Request {
protected $_request_parameters;
protected $_browser_capabilities;
public function __construct () {
$this->_request_parameters = $_REQUEST;
$this->_browser_capabilities = get_browser($_SERVER['HTTP_USER_AGENT']);
}
public function __get ($key) {
return isset($this->_request_parameters[$key]) ? $this->_request_parameters[$key] : null;
}
public function __set ($key, $value) {
$this->_request_parameters[$key] = $value;
}
public function getRequestParameters () {
return $this->_request_parameters;
}
public function addAll ($collection = array()) {
if (!empty($collection))
$this->_request_parameters = array_merge($this->_request_parameters, (array)$collection);
}
public function getBrowserCapabilities() {
return $this->_browser_capabilities;
}
}
?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <?php
abstract class Action
{
protected $_controller;
public function __construct($controller)
{
$this->_controller = $controller;
}
abstract public function launch(Request $request, Response $response);
public function render($file)
{
$this->_controller->render($file);
}
public function printOut()
{
$this->_controller->getResponse()->printOut();
}
protected function _forward($module, $action)
{
$this->_controller->forward($module, $action);
}
protected function _redirect($url)
{
$this->_controller->redirect($url);
}
}
?> |
Le code view.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <?php
class View
{
public function render($file, $assigns = array())
{
extract($assigns);
ob_start();
require($file);
$str = ob_get_contents();
ob_end_clean();
return $str;
}
}
?> |
Tous ces codes ont été copiés/collés du document source.
Raison aussi pour laquelle je ne comprends pas mon erreur !
Merci à ceussss qui me donneront un coup de main ![:)](https://www.developpez.net/forums/images/smilies/icon_smile.gif)
Partager