Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // models
- // models/news.php
- class newsModel
- {
- public $items = [
- ['id'=>1, 'text'=>'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod'],
- ['id'=>2, 'text'=>'tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,'],
- ['id'=>3, 'text'=>'quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo'],
- ['id'=>4, 'text'=>'consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse'],
- ['id'=>5, 'text'=>'cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non'],
- ['id'=>6, 'text'=>'proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'],
- ];
- public function getItemByID($id)
- {
- return (isset($this->items[$id])) ? $this->items[$id] : false;
- }
- }
- // models/mypage.php
- class mypageModel
- {
- public $title = 'My Page';
- public $date = '18-08-2017';
- public $text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod';
- }
- }
- // views
- // views/news.php
- class newsView
- {
- public function item()
- {
- return ''
- .'<div>'
- .'<h1>'.$this->data['id'].'</h1>'
- .'<p>'.$this->data['text'].'</p>'
- .'</div>';
- }
- public function list()
- {
- $html = '';
- foreach ($this->data as $key => $item) {
- $html .= $this->item($item);
- }
- return $html;
- }
- }
- // views/mypage.php
- class mypageView
- {
- public $data;
- public function __construct($model)
- {
- $this->data = $model;
- }
- public function justPage()
- {
- return ''
- .'<div>'
- .'<h1>'.$this->data->date.' - '.$this->data->title.'</h1>'
- .'<p>'.$this->data->text.'</p>'
- .'</div>';
- }
- }
- // views/errors.php
- class errorsView
- {
- public function NotFound($msg = '')
- {
- return '<h1>404 Not Found</h1>'
- .'<p>'.$msg.'</p>';
- }
- }
- // controllers
- // controllers/index.php
- class indexController
- {
- public function index()
- {
- return 'index/index page';
- }
- }
- // controllers/errors.php
- class errorsController
- {
- public function NotFound($msg = '')
- {
- $view = new errorsView;
- $result = $view->NotFound($msg);
- return $result;
- }
- }
- // controllers/news.php
- class newsController
- {
- public function index()
- {
- return 'news index page';
- }
- public function list()
- {
- $model = new newsModel;
- $view = new newsView($model);
- $result = $view->list();
- return $result;
- }
- }
- // controllers/mypage.php
- class mypageController
- {
- public function index()
- {
- return $this->example();
- }
- public function example()
- {
- $model = new mypageModel;
- $view = new mypageView($model);
- $result = $view->justPage();
- return $result;
- }
- }
- $map->get('my.routing', '/{controller}/{action}', function ($request) {
- $classname = $request->getAttribute('controller');
- if (!$classname) {
- $classname = 'index';
- }
- $classname = strtolower($classname);
- $classname .= 'Controller';
- if (!class_exists($classname)) {
- $error = new errorsController;
- return $error->NotFound('Controller: "'.$classname.'" not found');
- }
- $action = $request->getAttribute('action');
- if (!$action) {
- $action = 'index';
- }
- $action = strtolower($action);
- $controller = new $classname;
- if (!method_exists($controller, $action)) {
- $error = new errorsController;
- return $error->NotFound('Method: "'.$action.'" in Controller: "'.$classname.'" not found');
- }
- $result = $controller->$action();
- return $result;
- });
- /*
- / > indexController->index()
- /test > errorsController->NotFound('Controller: "test" not found')
- /news > newsController->index()
- /news/test > errorsController->NotFound('Method: "test" in Controller: "news" not found')
- /news/list > newsController->list()
- /mypage > mypageController->index()
- /mypage/example > mypageController->example()
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement