我正在学习如何创建API并将其与Vue.js应用程序连接。 我需要创建用户的注册表单。 如果我使用PHP创建API然后将其连接到我的Vue.js以便注册用户,这是否正确? API将连接到MySQL db。
这是我的db.php类:
class DB {
private $dbhost = 'localhost';
private $dbuser = 'root';
private $dbpass = '';
private $dbname = 'organization';
public function Connect() {
$mysqli = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
if ($mysqli->connect_errno) {
echo "Не удалось подключиться к MySQL: (".$mysqli->connect_errno.") ".$mysqli->connect_error;
}
return $mysqli;
}
}
这是路线之一:
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
$app = new \Slim\App;
$app->get('/api/customers', function(Request $request, Response $response) {
$db = new DB();
$db = $db->Connect();
$sql = "SELECT * FROM workers";
$result = $db->query($sql);
echo json_encode($result->fetch_assoc());
$db->close();
});