Guest User

Untitled

a guest
Jan 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. <?php
  2.  
  3. class Database
  4. {
  5. /**
  6. * Database username
  7. *
  8. * @var string
  9. */
  10. protected $username;
  11.  
  12. /**
  13. * Database password
  14. *
  15. * @var string
  16. */
  17. protected $password;
  18.  
  19. /**
  20. * Database host
  21. *
  22. * @var string
  23. */
  24. protected $host;
  25.  
  26. /**
  27. * Database name
  28. *
  29. * @var string
  30. */
  31. protected $name;
  32.  
  33. /**
  34. * Database constructor;
  35. */
  36. public function __construct()
  37. {
  38. try {
  39.  
  40. // iniate $c... I'm not sure how you're getting the value..
  41. $c = [
  42. 'settings' => [
  43. 'db' => [
  44. 'host' => '',
  45. 'dbname' => '',
  46. 'user' => '',
  47. 'pass' => '',
  48. ],
  49. ],
  50. ];
  51. $this->host = $c['settings']['db']['host'];
  52. $this->name = $c['settings']['db']['dbname'];
  53. $this->username = $c['settings']['db']['user'];
  54. $this->password = $c['settings']['db']['pass'];
  55.  
  56. return $this->connect();
  57. }
  58. }
  59.  
  60. /**
  61. * Connect to your database
  62. *
  63. * @return pdo
  64. */
  65. public function connect()
  66. {
  67. try{
  68. $pdo = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->name,
  69. $this->username, $this->password);
  70.  
  71. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  72. $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  73.  
  74. return $pdo;
  75. } catch (Exception $ex){
  76. return $ex->getMessage();
  77. }
  78.  
  79. }
  80.  
  81. }
  82.  
  83. /**
  84. * My Database
  85. *
  86. * @class
  87. */
  88. protected $db;
  89.  
  90. /**
  91. * Class constructor
  92. */
  93. public function __construct(Database $database){
  94. $this->db = $database;
  95. }
  96.  
  97. // Database settings
  98. $config['db']['host'] = '127.0.0.1';
  99. $config['db']['username'] = 'root';
  100. $config['db']['password'] = '';
  101. $config['db']['database'] = 'test';
  102. $config['db']['charset'] = 'utf8';
  103. $config['db']['collation'] = 'utf8_unicode_ci';
  104.  
  105. $container['db'] = function (Container $container) {
  106. $settings = $container->get('settings');
  107. $host = $settings['db']['host'];
  108. $dbname = $settings['db']['database'];
  109. $username = $settings['db']['username'];
  110. $password = $settings['db']['password'];
  111. $charset = $settings['db']['charset'];
  112. $collate = $settings['db']['collation'];
  113. $dsn = "mysql:host=$host;dbname=$dbname;charset=$charset";
  114. $options = [
  115. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  116. PDO::ATTR_PERSISTENT => false,
  117. PDO::ATTR_EMULATE_PREPARES => false,
  118. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  119. PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES $charset COLLATE $collate"
  120. ];
  121.  
  122. return new PDO($dsn, $username, $password, $options);
  123. };
  124.  
  125. protected $db;
  126.  
  127. public function __construct(Container $container)
  128. {
  129. $this->db = $container->get('db');
  130. }
  131.  
  132. public function internalUserDetails(Request $request, Response $response)
  133. {
  134. $this->db ...
  135. }
  136.  
  137. $app->get('/', function (Request $request, Response $response, array $args) {
  138. $db = $this->get('db');
  139. // do something
  140.  
  141. return $response;
  142. });
Add Comment
Please, Sign In to add comment