@@ -2,12 +2,12 @@ discard block |
||
| 2 | 2 | |
| 3 | 3 | namespace HnrAzevedo\Router; |
| 4 | 4 | |
| 5 | -trait Helper{ |
|
| 5 | +trait Helper { |
|
| 6 | 6 | use CheckTrait, ControllerTrait; |
| 7 | 7 | |
| 8 | 8 | protected function getProtocol(): string |
| 9 | 9 | { |
| 10 | - if((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')){ |
|
| 10 | + if ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')) { |
|
| 11 | 11 | return 'ajax'; |
| 12 | 12 | } |
| 13 | 13 | |
@@ -26,13 +26,13 @@ discard block |
||
| 26 | 26 | protected function import(string $path) |
| 27 | 27 | { |
| 28 | 28 | foreach (scandir($path) as $routeFile) { |
| 29 | - if(pathinfo($path.DIRECTORY_SEPARATOR.$routeFile, PATHINFO_EXTENSION) === 'php'){ |
|
| 30 | - require_once($path. DIRECTORY_SEPARATOR .$routeFile); |
|
| 29 | + if (pathinfo($path.DIRECTORY_SEPARATOR.$routeFile, PATHINFO_EXTENSION) === 'php') { |
|
| 30 | + require_once($path.DIRECTORY_SEPARATOR.$routeFile); |
|
| 31 | 31 | } |
| 32 | 32 | } |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | - protected function ControllerForm($controller, string $method, array $values){ |
|
| 35 | + protected function ControllerForm($controller, string $method, array $values) { |
|
| 36 | 36 | $this->check_role(); |
| 37 | 37 | $role = ($method !== 'method') ? $method : $this->getData()['POST']['role']; |
| 38 | 38 | $data = (!is_null($values)) ? json_decode($values['data']) : null; |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | $data = $this->getData(); |
| 45 | 45 | |
| 46 | 46 | foreach ($data['GET'] as $name => $value) { |
| 47 | - $controll = str_replace('{'.$name.'}',$value,$controll); |
|
| 47 | + $controll = str_replace('{'.$name.'}', $value, $controll); |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | $this->check_controllsettable($controll); |
@@ -53,11 +53,11 @@ discard block |
||
| 53 | 53 | |
| 54 | 54 | $this->check_controllmethod($controll); |
| 55 | 55 | |
| 56 | - $controller = ROUTER_CONFIG['controller.namespace'].'\\'. ucfirst(explode(':',$controll)[0]); |
|
| 56 | + $controller = ROUTER_CONFIG['controller.namespace'].'\\'.ucfirst(explode(':', $controll)[0]); |
|
| 57 | 57 | $controller = new $controller(); |
| 58 | - $method = explode(':',$controll)[1]; |
|
| 58 | + $method = explode(':', $controll)[1]; |
|
| 59 | 59 | |
| 60 | - if( ( $this->getProtocol() == 'form') ){ |
|
| 60 | + if (($this->getProtocol() == 'form')) { |
|
| 61 | 61 | $this->ControllerForm($controller, $method, $data['POST']); |
| 62 | 62 | }else { |
| 63 | 63 | $controller->$method($data); |
@@ -66,12 +66,12 @@ discard block |
||
| 66 | 66 | |
| 67 | 67 | protected function explodeRoute(bool $bar, string $url): array |
| 68 | 68 | { |
| 69 | - return explode( '/', $bar ? substr($url, 0, -1) : $url ); |
|
| 69 | + return explode('/', $bar ? substr($url, 0, -1) : $url); |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | protected function toHiking($walking) |
| 73 | 73 | { |
| 74 | - if(is_string($walking)){ |
|
| 74 | + if (is_string($walking)) { |
|
| 75 | 75 | $this->Controller($walking); |
| 76 | 76 | return true; |
| 77 | 77 | } |
@@ -4,28 +4,28 @@ |
||
| 4 | 4 | |
| 5 | 5 | use Exception; |
| 6 | 6 | |
| 7 | -trait ControllerTrait{ |
|
| 7 | +trait ControllerTrait { |
|
| 8 | 8 | |
| 9 | 9 | protected function check_controllsettable(string $controll) |
| 10 | 10 | { |
| 11 | - if(count(explode(':',$controll)) != 2){ |
|
| 11 | + if (count(explode(':', $controll)) != 2) { |
|
| 12 | 12 | throw new Exception("Controller {$controll} badly set."); |
| 13 | 13 | } |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | protected function check_controllexist(string $controll) |
| 17 | 17 | { |
| 18 | - $controllname = ROUTER_CONFIG['controller.namespace'].'\\'.ucfirst(explode(':',$controll)[0]); |
|
| 19 | - if(!class_exists($controllname)){ |
|
| 18 | + $controllname = ROUTER_CONFIG['controller.namespace'].'\\'.ucfirst(explode(':', $controll)[0]); |
|
| 19 | + if (!class_exists($controllname)) { |
|
| 20 | 20 | throw new Exception("No controller {$controllname} found."); |
| 21 | 21 | } |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | protected function check_controllmethod(string $controll) |
| 25 | 25 | { |
| 26 | - $controllname = ROUTER_CONFIG['controller.namespace'].'\\'.ucfirst(explode(':',$controll)[0]); |
|
| 27 | - $method = explode(':',$controll)[1]; |
|
| 28 | - if(!method_exists($controllname, $method)){ |
|
| 26 | + $controllname = ROUTER_CONFIG['controller.namespace'].'\\'.ucfirst(explode(':', $controll)[0]); |
|
| 27 | + $method = explode(':', $controll)[1]; |
|
| 28 | + if (!method_exists($controllname, $method)) { |
|
| 29 | 29 | throw new Exception("No method {$method} found for {$controllname}."); |
| 30 | 30 | } |
| 31 | 31 | } |
@@ -2,7 +2,7 @@ |
||
| 2 | 2 | |
| 3 | 3 | namespace Example\Controllers; |
| 4 | 4 | |
| 5 | -class User{ |
|
| 5 | +class User { |
|
| 6 | 6 | |
| 7 | 7 | public function my_account(array $data): void |
| 8 | 8 | { |
@@ -3,30 +3,30 @@ |
||
| 3 | 3 | use HnrAzevedo\Router\Router; |
| 4 | 4 | |
| 5 | 5 | |
| 6 | -Router::get('/{teste}',function(){ |
|
| 6 | +Router::get('/{teste}', function() { |
|
| 7 | 7 | echo 'teste'; |
| 8 | 8 | }); |
| 9 | 9 | |
| 10 | -Router::get('/1',function(){ |
|
| 10 | +Router::get('/1', function() { |
|
| 11 | 11 | echo 1; |
| 12 | 12 | }); |
| 13 | 13 | |
| 14 | -Router::get('/3',function(){ |
|
| 14 | +Router::get('/3', function() { |
|
| 15 | 15 | echo 3; |
| 16 | 16 | }); |
| 17 | 17 | |
| 18 | 18 | /* Returning parameters passed via URL in anonymous functions */ |
| 19 | -Router::get('/{parameter}/{otherparameter}', function($data){ |
|
| 19 | +Router::get('/{parameter}/{otherparameter}', function($data) { |
|
| 20 | 20 | echo "Parameter 1:{$data['parameter']}, Parameter 2:{$data['otherparameter']}."; |
| 21 | 21 | }); |
| 22 | 22 | |
| 23 | 23 | /* Passing controller and/or method via parameter in URL */ |
| 24 | -Router::get('/{controller}/{method}','{controller}:{method}'); |
|
| 24 | +Router::get('/{controller}/{method}', '{controller}:{method}'); |
|
| 25 | 25 | |
| 26 | 26 | /* Passing value via parameter */ |
| 27 | -Router::get('/my-account/{teste}','User:my_account'); |
|
| 27 | +Router::get('/my-account/{teste}', 'User:my_account'); |
|
| 28 | 28 | |
| 29 | 29 | /* Filter example */ |
| 30 | -Router::get('/my-account','User:my_account')->filter('User:user_in'); |
|
| 30 | +Router::get('/my-account', 'User:my_account')->filter('User:user_in'); |
|
| 31 | 31 | |
| 32 | 32 | |