Skip to content

Commit

Permalink
#24 Exemple code db
Browse files Browse the repository at this point in the history
  • Loading branch information
bfoujols committed Dec 28, 2023
1 parent ab3b636 commit 69a689b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/Config/routes.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
index:
uri: /
controller: Controller\HomeController
httpMethod: [GET]
testdb:
uri: /testdb
controller: Controller\DataBaseTestController
httpMethod: [GET]
41 changes: 41 additions & 0 deletions app/Controller/DataBaseTestController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Controller;

use Studoo\EduFramework\Core\Controller\ControllerInterface;
use Studoo\EduFramework\Core\Controller\Request;
use Studoo\EduFramework\Core\Service\DatabaseService;
use Studoo\EduFramework\Core\View\TwigCore;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;

class DataBaseTestController implements ControllerInterface
{
/**
* @param Request $request Requête HTTP
* @return string|null
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
* @throws \Exception
*/
public function execute(Request $request): string|null
{
// Connexion à la base de données
$getConnectDb = DatabaseService::getConnect();
// Requête SQL
$etudiantStmt = $getConnectDb->prepare("SELECT * FROM `etudiant`");
// Exécution de la requête
$etudiantStmt->execute();
// Récupération des données
$etudiants = $etudiantStmt->fetchAll();

return TwigCore::getEnvironment()->render('home/db.html.twig',
[
'titre' => 'Exemple avec connextion à la base de données',
'etudiants' => $etudiants
]
);
}
}
11 changes: 11 additions & 0 deletions app/Template/home/db.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends "base.html.twig" %}

{% block title %}{{ titre }}{% endblock %}

{% block content %}
<h1>{{ titre }}</h1>
{% for etudiant in etudiants %}
<p>{{ etudiant.prenom }} {{ etudiant.nom }}</p>
{% endfor %}
{% endblock %}

0 comments on commit 69a689b

Please sign in to comment.