php - How to connect mysql DB Slim 3 -
i'm new slim3 , can body guide me how connect mysql database , output results table.
there many examples sliim v2 , want make in in v3
you can use container pass around objects routes. assuming using closures handle routes in slim, this
$app = new \slim\app(); $container = $app->getcontainer(); $container["db"] = new pdo(...); // replace preferred db access method $app->get("/", function(\slim\http\request $req, \slim\http\response $res, array $args) { $connection = $this->get("db"); // continue request });
a brief explanation of above code: generic declaration of usual objects ($app
, $container
, etc). $container object (by default) pimple, dependency injection container implements \arrayaccess. allows treat object array. using ability can set value container, stored , allow access later. there, create basic route triggered when requesting /
, use closure respond request. closure has been binded container object slim, allowing access containers methods $this
variable. there can treat object if had created in function.
Comments
Post a Comment