Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. $app->get('/content', AppControllersContentController::class);
  2.  
  3. <?php
  4.  
  5. namespace AppControllers;
  6.  
  7. class ContentController
  8. {
  9. protected $container;
  10.  
  11. // Passes the DIC to get the model.
  12. function __construct($container)
  13. {
  14. $this->container = $container;
  15. }
  16.  
  17. function __invoke($request, $response, $args)
  18. {
  19.  
  20. $datas = $this->container->get('contentModel');
  21.  
  22. $args['content'] = $datas->testContent();
  23.  
  24. // get the template renderer and pass response and datas to the template file.
  25. return $this->container->get('renderer')->render($response, 'content.php', $args);
  26. }
  27.  
  28. <?php
  29.  
  30. namespace AppModels;
  31.  
  32. class Model
  33. {
  34. // Passes the DIC to get db later.
  35. function __construct($container)
  36. {
  37. $this->container = $container;
  38. }
  39.  
  40.  
  41. protected function executeQuery($sql, $params = null) {
  42. if ($params == null)
  43. {
  44. $result = $this->container->get('db')->query($sql); // direct execution
  45. } else {
  46. $result = $this->container->get('db')->prepare($sql); // prepared execution
  47. $result->execute($params);
  48. }
  49. return $result;
  50. }
  51.  
  52. }
  53.  
  54. <?php
  55.  
  56. namespace AppModels;
  57.  
  58. Class ContentModel extends Model
  59. {
  60.  
  61. public function testContent()
  62. {
  63. $testDatas = "Hello world";
  64. return $testDatas;
  65. }
  66.  
  67. public function getContent()
  68. {
  69. $sql = 'SELECT * FROM posts';
  70. $posts = $this->executeQuery($sql);
  71. return $posts;
  72. }
  73.  
  74.  
  75. }
  76.  
  77. <?php
  78.  
  79. use SlimApp;
  80.  
  81. return function (App $app) {
  82. $container = $app->getContainer();
  83.  
  84. // View renderer
  85. $container['renderer'] = function ($c) {
  86. $settings = $c->get('settings')['renderer'];
  87. return new SlimViewsPhpRenderer($settings['template_path']);
  88. };
  89.  
  90. // Database
  91. $container['db'] = function ($c) {
  92. $db = $c['settings']['db'];
  93. $pdo = new PDO('mysql:host=' . $db['host'] . ';dbname=' . $db['dbname'],
  94. $db['user'], $db['pass']);
  95. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  96. $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  97. return $pdo;
  98. };
  99.  
  100.  
  101. // Model data stored
  102. $container['contentModel'] = new AppModelsContentModel();
  103.  
  104. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement