1
1
<?php
2
+
2
3
/**
3
4
* @Package: Router - simple router class for php
4
5
* @Class : Router
@@ -396,7 +397,8 @@ public function controller(string $route, string $controller, array $options = [
396
397
$ methodVar = strtolower (preg_replace ('%([a-z]|[0-9])([A-Z])% ' , '\1-\2 ' , $ methodVar ));
397
398
398
399
if ((!empty ($ only ) && !in_array ($ methodVar , $ only ))
399
- || (!empty ($ except ) && in_array ($ methodVar , $ except ))) {
400
+ || (!empty ($ except ) && in_array ($ methodVar , $ except ))
401
+ ) {
400
402
continue ;
401
403
}
402
404
@@ -591,8 +593,11 @@ protected function exception(string $message = '', int $statusCode = Response::H
591
593
protected function routerCommand (): RouterCommand
592
594
{
593
595
return RouterCommand::getInstance (
594
- $ this ->baseFolder , $ this ->paths , $ this ->namespaces ,
595
- $ this ->request (), $ this ->response (),
596
+ $ this ->baseFolder ,
597
+ $ this ->paths ,
598
+ $ this ->namespaces ,
599
+ $ this ->request (),
600
+ $ this ->response (),
596
601
$ this ->getMiddlewares ()
597
602
);
598
603
}
@@ -646,6 +651,25 @@ protected function setPaths(array $params): void
646
651
$ this ->cacheFile = $ params ['cache ' ] ?? realpath (__DIR__ . '/../cache.php ' );
647
652
}
648
653
654
+ protected function controllerPaths ($ dir )
655
+ {
656
+ $ root = scandir ($ dir , 1 );
657
+ foreach ($ root as $ value ) {
658
+
659
+ if ($ value === '. ' || $ value === '.. ' ) {
660
+ continue ;
661
+ }
662
+ if (is_file ("$ dir/ $ value " )) {
663
+ $ result [] = "$ dir/ $ value " ;
664
+ continue ;
665
+ }
666
+ foreach ($ this ->controllerPaths ("$ dir/ $ value " ) as $ value ) {
667
+ $ result [] = $ value ;
668
+ }
669
+ }
670
+ return $ result ;
671
+ }
672
+
649
673
/**
650
674
* @param string $controller
651
675
*
@@ -667,7 +691,17 @@ protected function resolveClassName(string $controller)
667
691
668
692
$ file = realpath ("{$ this ->paths ['controllers ' ]}/ {$ controller }.php " );
669
693
if (!file_exists ($ file )) {
670
- $ this ->exception ("{$ controller } class is not found! Please check the file. " );
694
+ // Search for Controller Path
695
+ $ controllerPaths = preg_grep (
696
+ "/ $ controller/ " ,
697
+ $ this ->controllerPaths ($ this ->paths ['controllers ' ])
698
+ );
699
+ $ controllerPath = $ controllerPaths ? array_values ($ controllerPaths )[0 ] : null ;
700
+ $ file = $ controllerPath ? realpath ($ controllerPath ) : $ file ;
701
+
702
+ if (!file_exists ($ file )) {
703
+ $ this ->exception ("{$ controller } class is not found! Please check the file. " );
704
+ }
671
705
}
672
706
673
707
$ controller = $ this ->namespaces ['controllers ' ] . str_replace ('/ ' , '\\' , $ controller );
@@ -725,7 +759,9 @@ protected function addRoute(string $uri, string $method, $callback, ?array $opti
725
759
$ callback = is_array ($ callback ) ? implode ('@ ' , $ callback ) : $ callback ;
726
760
$ routeName = is_string ($ callback )
727
761
? strtolower (preg_replace (
728
- '/[^\w]/i ' , '. ' , str_replace ($ this ->namespaces ['controllers ' ], '' , $ callback )
762
+ '/[^\w]/i ' ,
763
+ '. ' ,
764
+ str_replace ($ this ->namespaces ['controllers ' ], '' , $ callback )
729
765
))
730
766
: null ;
731
767
$ data = [
0 commit comments