Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.77 KB | None | 0 0
  1. <?php
  2. // models
  3. // models/news.php
  4. class newsModel
  5. {
  6.     public $items = [
  7.         ['id'=>1, 'text'=>'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod'],
  8.         ['id'=>2, 'text'=>'tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,'],
  9.         ['id'=>3, 'text'=>'quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo'],
  10.         ['id'=>4, 'text'=>'consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse'],
  11.         ['id'=>5, 'text'=>'cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non'],
  12.         ['id'=>6, 'text'=>'proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'],
  13.     ];
  14.     public function getItemByID($id)
  15.     {
  16.         return (isset($this->items[$id])) ? $this->items[$id] : false;
  17.     }
  18. }
  19.  
  20. // models/mypage.php
  21. class mypageModel
  22. {
  23.     public $title = 'My Page';
  24.     public $date = '18-08-2017';
  25.     public $text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod';
  26. }
  27.  
  28. }
  29. // views
  30. // views/news.php
  31. class newsView
  32. {
  33.     public function item()
  34.     {
  35.         return ''
  36.             .'<div>'
  37.                 .'<h1>'.$this->data['id'].'</h1>'
  38.                 .'<p>'.$this->data['text'].'</p>'
  39.             .'</div>';
  40.     }
  41.     public function list()
  42.     {
  43.         $html = '';
  44.         foreach ($this->data as $key => $item) {
  45.             $html .= $this->item($item);
  46.         }
  47.         return $html;
  48.     }
  49. }
  50.  
  51. // views/mypage.php
  52. class mypageView
  53. {
  54.     public $data;
  55.  
  56.     public function __construct($model)
  57.     {
  58.         $this->data = $model;
  59.     }
  60.     public function justPage()
  61.     {
  62.         return ''
  63.             .'<div>'
  64.                 .'<h1>'.$this->data->date.' - '.$this->data->title.'</h1>'
  65.                 .'<p>'.$this->data->text.'</p>'
  66.             .'</div>';
  67.     }
  68. }
  69.  
  70. // views/errors.php
  71. class errorsView
  72. {
  73.     public function NotFound($msg = '')
  74.     {
  75.         return '<h1>404 Not Found</h1>'
  76.             .'<p>'.$msg.'</p>';
  77.     }
  78. }
  79.  
  80. // controllers
  81. // controllers/index.php
  82. class indexController
  83. {
  84.     public function index()
  85.     {
  86.         return 'index/index page';
  87.     }
  88. }
  89.  
  90. // controllers/errors.php
  91. class errorsController
  92. {
  93.     public function NotFound($msg = '')
  94.     {
  95.         $view = new errorsView;
  96.         $result = $view->NotFound($msg);
  97.         return $result;
  98.     }
  99. }
  100.  
  101. // controllers/news.php
  102. class newsController
  103. {
  104.     public function index()
  105.     {
  106.         return 'news index page';
  107.     }
  108.     public function list()
  109.     {
  110.         $model = new newsModel;
  111.         $view = new newsView($model);
  112.         $result = $view->list();
  113.         return $result;
  114.     }
  115. }
  116.  
  117. // controllers/mypage.php
  118. class mypageController
  119. {
  120.     public function index()
  121.     {
  122.         return $this->example();
  123.     }
  124.     public function example()
  125.     {
  126.         $model = new mypageModel;
  127.         $view = new mypageView($model);
  128.         $result = $view->justPage();
  129.         return $result;
  130.     }
  131. }
  132.  
  133. $map->get('my.routing', '/{controller}/{action}', function ($request) {
  134.     $classname = $request->getAttribute('controller');
  135.     if (!$classname) {
  136.         $classname = 'index';
  137.     }
  138.     $classname = strtolower($classname);
  139.     $classname .= 'Controller';
  140.  
  141.     if (!class_exists($classname)) {
  142.         $error = new errorsController;
  143.         return $error->NotFound('Controller: "'.$classname.'" not found');
  144.     }
  145.  
  146.     $action = $request->getAttribute('action');
  147.     if (!$action) {
  148.         $action = 'index';
  149.     }
  150.     $action = strtolower($action);
  151.  
  152.     $controller = new $classname;
  153.     if (!method_exists($controller, $action)) {
  154.         $error = new errorsController;
  155.         return $error->NotFound('Method: "'.$action.'" in Controller: "'.$classname.'" not found');
  156.     }
  157.  
  158.     $result = $controller->$action();
  159.     return $result;
  160. });
  161.  
  162. /*
  163. /               > indexController->index()
  164. /test           > errorsController->NotFound('Controller: "test" not found')
  165. /news           > newsController->index()
  166. /news/test      > errorsController->NotFound('Method: "test" in Controller: "news" not found')
  167. /news/list      > newsController->list()
  168. /mypage         > mypageController->index()
  169. /mypage/example > mypageController->example()
  170. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement