CREATE TABLE student( id bigserial NOT NULL, name character varying(255) NOT NULL, PRIMARY KEY (id) );
insert into student(name) values ('zhangsan'); insert into student(name) values ('lisi'); insert into student(name) values ('wangwu'); insert into student(name) values ('zhaoliu');
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use App\Service\DataService;
class DataController extends AbstractController { /** * @Route("/data", name="data") */ public function index(DataService $dserv) { $students = $dserv->findAll();
return$this->json([ 'data' => $students ]); } }
DataService.php
<?php
namespace App\Service;
use Doctrine\DBAL\Driver\Connection;
class DataService { private $conn;
public function __construct(Connection $conn) { $this->conn = $conn; }
/** * Finds all countries */ public functionfindAll() {