Guest User

Untitled

a guest
Apr 16th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. <?php
  2.  
  3. class db {
  4.  
  5. private $dbhost = "mysql.hostinger.com.ar";
  6. private $dbuser = "(mi usuario)";
  7. private $dbpass = "(mi pass)";
  8. private $dbname = "(mi bd)";
  9.  
  10. public function connect(){
  11. $mysql_connect_str = "mysql:host=$this->dbhost;dbname=$this->dbname";
  12. $dbConnection = new PDO($mysql_connect_str, $this->dbuser, $this->dbpass);
  13. $dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  14. return $dbConnection;
  15. }
  16. }
  17.  
  18. <?php
  19.  
  20. use PsrHttpMessageServerRequestInterface as Request;
  21. use PsrHttpMessageResponseInterface as Response;
  22.  
  23.  
  24. $app->group('/usuario/', function () {
  25.  
  26. $app = new SlimApp;
  27.  
  28. $this-> get('', function(Request $request, Response $response){
  29.  
  30. $sql = "select * from usuario";
  31.  
  32. try{
  33. $db = new db();
  34.  
  35. $db = $db->connect();
  36.  
  37. $stmt = $db->query($sql);
  38. $usuario = $stmt->fetchAll(PDO::FETCH_OBJ);
  39. $db = null;
  40. echo json_encode($usuario);
  41.  
  42. } catch(PDOException $e){
  43. echo '{"error": {"text": '.$e->getMessage(). '}';
  44. }
  45. });
  46.  
  47. RewriteEngine On
  48. # Some hosts may require you to use the `RewriteBase` directive.
  49. # If you need to use the `RewriteBase` directive, it should be the
  50. # absolute physical path to the directory that contains this htaccess file.
  51. #
  52. # RewriteBase /
  53. Header add Access-Control-Allow-Origin "*"
  54. Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
  55. Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
  56. RewriteCond %{REQUEST_FILENAME} !-f
  57. RewriteRule ^(.*)$ index.php [QSA,L]
  58.  
  59. <?php
  60. use PsrHttpMessageServerRequestInterface as Request;
  61. use PsrHttpMessageResponseInterface as Response;
  62.  
  63. require '../vendor/autoload.php';
  64. require '../app/config/db.php';
  65.  
  66.  
  67. $app = new SlimApp(["settings" => $config]);
  68. $container = $app->getContainer();
  69. $capsule = new Capsule;
  70. $capsule->addConnection($config['db']);
  71. $capsule->setEventDispatcher(new Dispatcher(new Container));
  72. $capsule->bootEloquent();
  73.  
  74. require '../app/route/usuario.php';
  75.  
  76. $app->run();
Add Comment
Please, Sign In to add comment