Advertisement
Guest User

Untitled

a guest
Jul 15th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. app
  2. -- controllers
  3. ---- MainController.php
  4. ---- PostController.php
  5. -- core
  6. ---- App.php
  7. ---- Controller.php
  8. ---- Model.php
  9. ---- View.php
  10. -- models
  11. ---- PostModel
  12. -- views
  13. ---- posts.php
  14. .htaccess
  15. index.php
  16.  
  17. // View.php
  18.  
  19. <?php
  20.  
  21.  
  22. class View
  23. {
  24. public function render($template, $data = null)
  25. {
  26. include 'app/views/'.$template;
  27. }
  28. }
  29.  
  30.  
  31. // Model.php
  32.  
  33. <?php
  34.  
  35.  
  36. class Model
  37. {
  38. public $connect;
  39.  
  40. public function __construct()
  41. {
  42. $this->connect = new PDO('mysql:host=localhost;dbname=app', 'root', '');
  43.  
  44. }
  45.  
  46. }
  47.  
  48.  
  49. // Controller.php
  50.  
  51. <?php
  52.  
  53. class Controller
  54. {
  55. public $view;
  56. public $model;
  57.  
  58. public function __construct()
  59. {
  60. $this->view = new View();
  61. $this->model = new Model();
  62.  
  63. }
  64.  
  65. }
  66.  
  67. // PostModel.php
  68.  
  69. <?php
  70.  
  71.  
  72. class PostModel extends Model
  73. {
  74.  
  75. public function getPosts()
  76. {
  77. $this->connect->query('SELECT * FROM articles')->fetchAll(PDO::FETCH_OBJ);
  78.  
  79. }
  80.  
  81. }
  82.  
  83.  
  84. // PostController
  85.  
  86. <?php
  87.  
  88.  
  89. class PostController extends Controller
  90. {
  91. public function __construct()
  92. {
  93. $this->model = new PostModel();
  94. $this->view = new View();
  95. }
  96.  
  97. public function indexAction()
  98. {
  99. $posts = $this->model->getPosts();
  100. $this->view->render('posts.php', $posts);
  101. }
  102.  
  103. }
  104.  
  105.  
  106. // posts.php
  107.  
  108. <?php foreach ($posts as $post): ?>
  109. <h1><?= $post->title ?></h1>
  110. <?php endforeach; ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement