# MVC
## Modele Vue Controller
---
### Arborescence du projet
```txt
project_root/
│
├── app/
│
├── config/
│ └── config.php
│
└── public/
```
---
### Arborescence du projet
#### /public
```txt
public/
├── index.php
├── print_hello.php
└── css/
└── style.css
```
---
### Arborescence du projet
#### /public/print_hello.php
```php
index();
?>
```
---
### Arborescence du projet
#### /app
```txt[]
app/
│
├── controllers/
│
├── entities/
│
├── repositories/
│
├── services/
│
├── trait/
│
├── views/
│
└─ core/
```
---
### Arborescence du projet
#### /app/controllers
```php[1-4|6-10|12-15|17-20|22-25]
require_once '../app/core/Controller.php';
require_once '../app/services/HelloService.php';
class HelloWorldController extends Controller
{
private HelloService $helloService;
public function __construct()
{
$this->helloService = new HelloService();
}
public function index():void
{
$this->view('hello_world', $this->helloService->hello(), ['name' => 'Salim']);
}
public function indexJson():void
{
$this->json(['title' => $this->helloService->hello(), 'name' => 'Salim']);
}
public function redirectToJson()
{
$this->redirectTo('/json.php');
}
}
```
---
### Arborescence du projet
#### /app/entities et /app/repositories
##### Entities
- Classes représentant les tables de la base de données
##### Repositories
- Gestion des connexions à la base de données et exécution des requêtes SQL
---
### Arborescence du projet
#### /app/services
```php[]
view('hello_world', $this->helloService->hello(), ['name' => 'Salim','errors'=>['Nom incorrect']]);
}
```
```php[]
Hello
```
---
### Arborescence du projet
#### /app/core/
```php[1|2|5-17|19-24|26-29]
abstract class Controller {
protected $viewPath = '../app/views/'; // Chemin vers les vues
protected function view(string $viewName, string $title = 'Titre de la page', array $data = [], $status = 200) {
$filePath = $this->viewPath . $viewName . '.php';
if (file_exists($filePath)) {
// Extraire les données pour qu'elles soient disponibles dans la vue comme des variables
extract($data);
http_response_code($status);
require $filePath;
} else {
throw new Exception("Vue non trouvée : " . $filePath);
}
}
protected function json($data, $status = 200) {
header('Content-Type: application/json');
http_response_code($status);
echo json_encode($data);
exit();
}
protected function redirectTo($url) {
header("Location: $url");
exit();
}
}
```
---
### Arborescence complète du projet :
```txt[]
project_root/
│
├── app/
│ ├── controllers/
│ │ ├── ArticleController.php
│ │ ├── CategoryController.php
│ │ └── UserController.php
│ │
│ ├── entities/
│ │ ├── Article.php
│ │ ├── Category.php
│ │ └── User.php
│ │
│ ├── repositories/
│ │ ├── ArticleRepository.php
│ │ ├── CategoryRepository.php
│ │ └── UserRepository.php
│ │
│ ├── services/
│ │ ├── ArticleService.php
│ │ ├── CategoryService.php
│ │ └── UserService.php
│ │
│ ├── trait/
│ │ ├── FormTrait.php
│ │ └── AuthTrait.php
│ │
│ ├── views/
│ │ ├── article.php
│ │ ├── category.php
│ │ └── user.php
│ │
│ └─ core/
│ ├── Repository.php
│ └── Controller.php
│
├── config/
│ └── config.php
│
└── public/
├── index.php
├── print_hello.php
└── css/
└── style.css
```