微型微框架中的index.php示例文件,它位于公共文件夹中? 我使用了使用composer和index.php文件的slim / slim-skeleton,但是没有用?
答案 0 :(得分:0)
确定。我得到了答案。
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../vendor/autoload.php';
$container = new \Slim\Container;
$container['db'] = getConnection();
$app = new \Slim\App($container);
$app->get('/', function ($request, $response, $args) {
$response->write("Welcome to Slim!");
return $response;
});
function getConnection() {
$dbhost="localhost";
$dbuser="xxx";
$dbpass="xxxxxxxx";
$dbname="xxxxxx";
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
// var_dump($dbh);
return $dbh;
}
$app->run();
?>