Skip to content

Commit 43045b3

Browse files
committed
j ai crier un interface de crud avec l inplementation des methode crud dynamic
1 parent 31fd548 commit 43045b3

File tree

18 files changed

+242
-116
lines changed

18 files changed

+242
-116
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.idea
2+
/.env
3+
/vendor

app/Core/config/Paths.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
define('PROJECT_ROOT', dirname(dirname(__DIR__ . '/../')));
32

43
define('SITE_NAME', 'your-site-name');
54

app/Core/config/database.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
<?php
22
//the database connections
3+
namespace MVC\connexion;
34

4-
class Database
5+
6+
class connexion
57
{
68

7-
private $host = DB_HOST;
8-
private $user = DB_USER;
9-
private $password = DB_PASSWORD;
10-
private $db_name = DB_NAME;
9+
1110

1211
private $pdo;
1312
private $stmt;

app/Core/utils/Alert.php

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,53 @@
1-
<?php ?>
1+
<?php
2+
namespace app\utils;
3+
4+
class Alert{
5+
6+
7+
public static function rederction($type){
8+
9+
10+
11+
12+
}
13+
public function sweetAlert($type){
14+
15+
echo '<script type="text/javascript">
16+
17+
$(document).ready(function(){
18+
19+
swal({
20+
position: "top-end",
21+
type: "success",
22+
title: "Your work has been saved",
23+
showConfirmButton: false,
24+
timer: 1500
25+
})
26+
});
27+
28+
</script>';
29+
30+
}
31+
32+
}
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
?>

app/Core/utils/loader.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
<?php
22

3-
define('PROJECT_ROOT', dirname(dirname(__DIR__ . '/../')));
4-
5-
require_once PROJECT_ROOT.'../../Router/Router.php'
6-
7-
8-
3+
define('PROJECT_ROOTS', dirname(dirname(__DIR__ . '')));
4+
echo PROJECT_ROOTS;
5+
require_once PROJECT_ROOTS.'\Router\Router.php';
96

7+
require_once PROJECT_ROOTS.'\Controllers\ErrorController.php';
8+
require_once PROJECT_ROOTS.'\Controllers\PageController.php';
109

1110

1211

app/Router/Application.php

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +0,0 @@
1-
<?php
2-
namespace app\Routing;
3-
4-
require_once __DIR__ .'../../vendor/autoload.php';
5-
use App\Routing\router;
6-
7-
8-
class Application{
9-
10-
public Router $router;
11-
12-
13-
public function run()
14-
{
15-
echo $this->router->resolve();
16-
}
17-
18-
19-
20-
21-
22-
23-
24-
25-
26-
}
27-
28-
29-
30-
31-
32-
33-
34-
35-
36-
37-
38-
39-
40-
?>

app/Router/Crud.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace MVC\Model;
4+
5+
use MVC\connexion\connexion;
6+
use MVC\interfaces\Crud as CrudInterface;
7+
use PDO;
8+
9+
abstract class Crud implements CrudInterface
10+
{
11+
public function __construct()
12+
{
13+
new connexion();
14+
}
15+
16+
public function insert(string $table,array $data):int
17+
{
18+
$columns = implode(', ', array_keys($data));
19+
$values = implode(', ', array_fill(0, count($data), '?'));
20+
21+
$sql = "insert into $table($columns) values ($values)";
22+
$stmt = connexion::$pdo->prepare($sql);
23+
$stmt->execute(array_values($data));
24+
25+
return connexion::$pdo->lastInsertId();
26+
}
27+
public function select(string $table,int $id):object
28+
{
29+
$sql = "SELECT * FROM $table WHERE id = ?";
30+
$stmt = connexion::$pdo->prepare($sql);
31+
$stmt->execute([$id]);
32+
33+
return $stmt->fetch(PDO::FETCH_OBJ);
34+
}
35+
public function selectAll(string $table):array
36+
{
37+
$sql = "SELECT * FROM $table";
38+
$stmt = connexion::$pdo->prepare($sql);
39+
$stmt->execute();
40+
41+
//return $stmt->fetch(PDO::FETCH_OBJ);
42+
return $stmt->fetchAll(PDO::FETCH_OBJ);
43+
}
44+
45+
public function update(string $table,int $id,array $data):int
46+
{
47+
$setClause = implode(' = ?, ', array_keys($data)) . ' = ?';
48+
$sql = "UPDATE $table SET $setClause WHERE id = ?";
49+
$stmt = connexion::$pdo->prepare($sql);
50+
$stmt->execute(array_merge(array_values($data), [$id]));
51+
52+
return $stmt->rowCount();
53+
}
54+
55+
public function delete(string $table,int $id):int
56+
{
57+
$sql = "DELETE FROM $table WHERE id = ?";
58+
$stmt = connexion::$pdo->prepare($sql);
59+
$stmt->execute([$id]);
60+
61+
return $stmt->rowCount();
62+
}
63+
}

app/Router/Response.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +0,0 @@
1-
<?php
2-
3-
namespace app\Routing;
4-
5-
class Response
6-
{
7-
public function setStatusCode(int $code)
8-
{
9-
http_response_code($code);
10-
}
11-
12-
public function redirect(string $url)
13-
{
14-
header('Location: '.$url);
15-
}
16-
}

app/Router/Router.php

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,61 @@
11
<?php
2+
namespace app\Routes;
23

3-
class Router {
4-
private $Controller = 'pages';
5-
private $methode = 'index';
6-
private $param=[];
4+
class Router
5+
{
6+
private $controller = 'Page';
7+
private $method = 'index';
8+
private $param = [];
79
public function __construct()
810
{
9-
$uri = $this->getUrl();
10-
if(isset($uri[0])){
11-
if(file_exists('../app/controllers/'.ucwords($uri[0]).'Controller.php')){
11+
$url = $this->getUrl();
12+
13+
if (isset($url[0])) {
14+
$controllerClass = ucwords($url[0]);
15+
var_dump(PROJECT_ROOTS.'\\Controllers\\' . $controllerClass . 'Controller');
16+
var_dump($controllerClass);
17+
$class=class_exists(PROJECT_ROOTS.'\\Controllers\\' . $controllerClass . 'Controller');
18+
var_dump($class);
19+
if ($class) {
20+
$this->controller = $controllerClass;
21+
}
1222

13-
$this->Controller =ucwords($uri[0]) ;
14-
unset($uri[0]);
1523

16-
}
24+
$controllerClass = '\\app\\controllers\\' . $this->controller . 'Controller';
25+
$this->controller = new $controllerClass;
1726

18-
require_once PROJECT_ROOT .'\app\controlles'. $this->Controller;
27+
if (isset($url[1])) {
28+
29+
if (method_exists($this->controller, $url[1])) {
30+
$this->method = $url[1];
31+
}
32+
}
33+
if (!empty($_REQUEST)) {
34+
$this->convertArray($_REQUEST);
35+
} else {
36+
$this->param = [];
37+
}
1938
} else {
20-
include '../resources/views/error404.php';
21-
exit;
39+
$this->controller = new $this->controller;
2240
}
41+
call_user_func_array([$this->controller, $this->method], $this->param);
2342
}
24-
public function getUrl()
43+
private function getUrl()
44+
{
45+
$uri = $_SERVER['PATH_INFO'] ?? '/';
46+
$uri = explode('/', trim($uri, '/'));
47+
return $uri;
48+
}
49+
private function convertArray($array)
2550
{
26-
// var_dump($_REQUEST);
27-
2851

29-
if (empty($_SERVER['REQUEST_URI'])) {
30-
$uri = '';
31-
} else {
32-
$uri = $_SERVER['REQUEST_URI'];
52+
foreach ($array as $value) {
53+
array_push($this->param, $value);
3354
}
34-
35-
36-
$uri = explode('/', trim($uri, '/'));
37-
// var_dump( $uri);
38-
// }
39-
40-
return $uri;
55+
}
56+
private function getBasePaths()
57+
{
58+
$path = $_SERVER['HTTP_REFERER'];
59+
header("Location: $path ");
4160
}
4261
}
43-
44-
45-
46-
47-
48-
49-
50-
51-
52-
53-
54-
55-
56-
?>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace app\Controllers;
4+
class ErrorController {
5+
6+
7+
8+
public function index(){
9+
10+
echo 'this is the index action';
11+
}
12+
13+
}
14+
15+
16+
17+
18+
19+
20+
21+
22+
?>

0 commit comments

Comments
 (0)