1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,78 @@ |
1 |
+<?php |
|
2 |
+ |
|
3 |
+namespace MVC\Classe; |
|
4 |
+ |
|
5 |
+class Url |
|
6 |
+{ |
|
7 |
+ public $page; |
|
8 |
+ |
|
9 |
+ |
|
10 |
+ public function __construct(){ |
|
11 |
+ |
|
12 |
+ $page = array(); |
|
13 |
+ $page['name'] = 'accueil'; |
|
14 |
+ $page['description'] = ""; |
|
15 |
+ $page['params'] = array(); |
|
16 |
+ $page['control'] = false; |
|
17 |
+ |
|
18 |
+ |
|
19 |
+ |
|
20 |
+ $url = parse_url($_SERVER['REQUEST_URI']); |
|
21 |
+ $urlTrim = trim( $url['path'] , '/' ); |
|
22 |
+ $urlParts = explode('/' , $urlTrim ); |
|
23 |
+ |
|
24 |
+ //print_r($urlParts); |
|
25 |
+ if(isset($urlParts[0])) { |
|
26 |
+ //Récupération du nom de la page |
|
27 |
+ ($urlParts[0] == 'index' || $urlParts[0] == '') ? $page['name'] = 'accueil' : $page['name'] = $urlParts[0]; |
|
28 |
+ //array_shift($urlParts); |
|
29 |
+ unset($urlParts[0]); |
|
30 |
+ }else{ |
|
31 |
+ $page['name'] = 'accueil'; |
|
32 |
+ } |
|
33 |
+ |
|
34 |
+ if($page['name'] == 'control'){ |
|
35 |
+ $page['control'] = true; |
|
36 |
+ ($urlParts[1] == 'index' || $urlParts[1] == '' ) ? $page['name']='accueil' : $page['name']=$urlParts[1]; |
|
37 |
+ //array_shift($urlParts); |
|
38 |
+ unset($urlParts[1]); |
|
39 |
+ |
|
40 |
+ } |
|
41 |
+ |
|
42 |
+ //vérification du nombre de parametres: s'il n'existe pas autant de clé que |
|
43 |
+ // de valeurs on sort de la fonction et on renvoie une page d'erreur. |
|
44 |
+ $numParts = count($urlParts); |
|
45 |
+ if ( $numParts%2 != 0 ) { |
|
46 |
+ $page['name'] = 'error'; |
|
47 |
+ $page['params'] = array(); |
|
48 |
+ return $page; |
|
49 |
+ }else if ( $numParts != 0 ){ |
|
50 |
+ $values = array(); |
|
51 |
+ $keys = array(); |
|
52 |
+ foreach( $urlParts as $key => $value ){ |
|
53 |
+ if($key%2 == 0) { |
|
54 |
+ $values[] = $value; |
|
55 |
+ } else { |
|
56 |
+ $keys[] = $value; |
|
57 |
+ } |
|
58 |
+ } |
|
59 |
+ if($page['control']){ |
|
60 |
+ $page['params'] = array_combine($values, $keys); |
|
61 |
+ }else { |
|
62 |
+ $page['params'] = array_combine($keys, $values); |
|
63 |
+ } |
|
64 |
+ } |
|
65 |
+ |
|
66 |
+ //verification de l'existence de la page dans les controlleurs |
|
67 |
+ if($page['control']){ |
|
68 |
+ $pageFile = TRAITEMENT_PATH . DIRECTORY_SEPARATOR . $page['name'] . '.php'; |
|
69 |
+ }else { |
|
70 |
+ $pageFile = CONTROLLERS_PATH . DIRECTORY_SEPARATOR . $page['name'] . '.php'; |
|
71 |
+ } |
|
72 |
+ |
|
73 |
+ if(!file_exists($pageFile)){ |
|
74 |
+ $page['name'] = 'error'; |
|
75 |
+ } |
|
76 |
+ $this->page = $page; |
|
77 |
+ } |
|
78 |
+} |