22
33namespace Core ;
44
5+ use Core \Middleware \Auth ;
6+ use Core \Middleware \Guest ;
7+ use Core \Middleware \Middleware ;
8+
59class Router
610{
711 protected $ routes = [];
@@ -11,39 +15,96 @@ public function add($method, $uri, $controller)
1115 $ this ->routes [] = [
1216 'uri ' => $ uri ,
1317 'controller ' => $ controller ,
14- 'method ' => $ method
18+ 'method ' => $ method ,
19+ 'middleware ' => null
1520 ];
21+
22+ return $ this ;
1623 }
1724
1825 public function get ($ uri , $ controller )
1926 {
20- $ this ->add ('GET ' , $ uri , $ controller );
27+ return $ this ->add ('GET ' , $ uri , $ controller );
2128 }
2229
2330 public function post ($ uri , $ controller )
2431 {
25- $ this ->add ('POST ' , $ uri , $ controller );
32+ return $ this ->add ('POST ' , $ uri , $ controller );
2633 }
2734
2835 public function delete ($ uri , $ controller )
2936 {
30- $ this ->add ('DELETE ' , $ uri , $ controller );
37+ return $ this ->add ('DELETE ' , $ uri , $ controller );
3138 }
3239
3340 public function patch ($ uri , $ controller )
3441 {
35- $ this ->add ('PATCH ' , $ uri , $ controller );
42+ return $ this ->add ('PATCH ' , $ uri , $ controller );
3643 }
3744
3845 public function put ($ uri , $ controller )
3946 {
40- $ this ->add ('PUT ' , $ uri , $ controller );
47+ return $ this ->add ('PUT ' , $ uri , $ controller );
48+ }
49+
50+ public function only ($ key )
51+ {
52+ // what we need to do is grab the last route that was added
53+ // and set its middleware to the key that was passed in
54+ // this is a very simple way to do it, but it works
55+ // there are several ways to do this, but this is the simplest
56+ $ this ->routes [array_key_last ($ this ->routes )]['middleware ' ] = $ key ;
57+
58+ return $ this ;
4159 }
4260
4361 public function route ($ uri , $ method )
4462 {
4563 foreach ($ this ->routes as $ route ) {
4664 if ($ route ['uri ' ] === $ uri && $ route ['method ' ] === strtoupper ($ method )) {
65+ // apply middleware if it exists
66+ // if ($route['middleware'] === 'guest') {
67+ // if($_SESSION['user'] ?? false) {
68+ // header('Location: /');
69+ // exit(); // or die
70+ // }
71+ // }
72+ // if ($route['middleware'] === 'auth') {
73+ // if(! $_SESSION['user'] ?? false) {
74+ // header('Location: /');
75+ // exit(); // or die
76+ // }
77+ // }
78+ // this is pretty sloppy, instead what if the handlers of each of these
79+ // stored in a separate file, and we just require that file
80+
81+ // this is more dynamic way of handling middleware that is we are handling below in comment
82+ // null handling
83+ Middleware::resolve ($ route ['middleware ' ]);
84+ // added below handling in Router.php
85+ // if ($route['middleware']) {
86+ // $middleware = Middleware::MAP[$route['middleware']];
87+ // (new $middleware)->handle();
88+ // }
89+
90+ // if ($route['middleware'] === 'guest') {
91+ // (new Guest)->handle();
92+ // }
93+ // if ($route['middleware'] === 'auth') {
94+ // (new Auth)->handle();
95+ // }
96+
97+ // if later we need to add more middleware, we need to do this
98+ // and its not look good we could simplify this
99+ // its sound like we are associating a key with a corresponding middleware class
100+ // so the guest key points to the Guest class
101+ // the auth key points to the Auth class
102+ // something like that
103+ // so why don't we setup some kind of lookup table
104+ // lets add some kind of parent class called Middleware
105+ // if ($route['middleware'] === 'email-confirmed') {
106+ // (new ConfirmedEmail)->handle();
107+ // }
47108 return require base_path ($ route ['controller ' ]);
48109 }
49110 }
0 commit comments