@@ -21,6 +21,8 @@ class RouteRule
2121
2222 private $ name ;
2323
24+ private $ namespace = '' ;
25+
2426 private $ method ;
2527
2628 private $ controller ;
@@ -31,17 +33,27 @@ class RouteRule
3133
3234 private $ as ;
3335
34- private $ uses ;
35-
3636 private $ where ;
3737
38- public function __construct ($ method , $ url , $ parameter )
38+ private $ prefix = '' ;
39+
40+ public function __construct ($ method , $ url , $ parameter , $ attributes = [])
3941 {
42+ $ this ->setAttribute ($ attributes );
4043 $ this ->method = $ method ;
4144 $ this ->url = $ url ;
4245 $ this ->parse ($ parameter );
4346 }
4447
48+ private function setAttribute ($ attributes )
49+ {
50+ foreach ($ attributes as $ k => $ v ) {
51+ if (isset ($ this ->{$ k })) {
52+ $ this ->$ k = $ v ;
53+ }
54+ }
55+ }
56+
4557 private function parse ($ parameter )
4658 {
4759 if (is_array ($ parameter )) {
@@ -56,9 +68,9 @@ private function parse($parameter)
5668 }
5769 throw new \RuntimeException ('parameter error ' );
5870 }
59- }
60- if ( $ parameter instanceof \Closure) {
61- $ this ->controller = $ parameter ;
71+ } else {
72+ // 控制器
73+ $ this ->controller ( $ parameter) ;
6274 }
6375 }
6476
@@ -72,9 +84,9 @@ public function middleware(...$args)
7284 $ this ->middleware = array_merge ($ this ->middleware , $ args );
7385 }
7486
75- public function uses (... $ controller )
87+ public function uses ($ controller )
7688 {
77- $ this ->uses = $ controller ;
89+ $ this ->controller ( $ controller) ;
7890 }
7991
8092 public function where ($ name , $ preg )
@@ -96,14 +108,38 @@ public function domain($domain)
96108 {
97109 }
98110
111+ public function controller ($ controller )
112+ {
113+ if ($ controller instanceof \Closure) {
114+ $ this ->controller = $ controller ;
115+ } else {
116+ $ this ->controller = $ this ->makeController ($ controller );
117+ }
118+ }
119+
120+ private function makeController ($ controllerArr )
121+ {
122+ if (is_string ($ controllerArr )) {
123+ $ controllerArr = explode ('@ ' , $ controllerArr );
124+ }
125+ if (!empty ($ this ->namespace )) {
126+ $ controllerArr [0 ] = ltrim ($ this ->namespace , '\\' ).'\\' .$ controllerArr [0 ];
127+ }
128+
129+ return $ controllerArr ;
130+ }
131+
99132 private function parent ()
100133 {
101134 }
102135
103- public function getOther ()
136+ public function getUrlParameter (): array
104137 {
105- // 获取所有 attribute 放入数组
138+ return [];
139+ }
106140
141+ public function getOther ()
142+ {
107143 return [
108144 'method ' => $ this ->method ,
109145 'controller ' => $ this ->controller ,
@@ -113,7 +149,11 @@ public function getOther()
113149
114150 public function getUrl ()
115151 {
116- return $ this ->url ;
152+ if (empty ($ this ->prefix )) {
153+ return '/ ' .trim ($ this ->url , '/ ' );
154+ }
155+
156+ return '/ ' .trim ($ this ->prefix , '/ ' ).rtrim ($ this ->url , '/ ' );
117157 }
118158
119159 public function getController ()
0 commit comments