.
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 
2 | 
3 | [](http://tarhche.com)
4 | [](https://github.com/khanzadimahdi/router/blob/master/LICENSE)
5 |
6 | # router
7 | A simple php Router
8 |
9 | ## how to use
10 | We have 3 important files here
11 |
12 |
13 | - htaccess: Redirects all requests to home.php
14 | - home.php: You can manage all requests in this file
15 | - ROUTE.php: This is ROUTE lib you need to manage requests in home.php
16 |
17 |
18 | Add ROUTE.php in home.php and then use it to manage requests:
19 |
20 | ```php
21 |
25 | ```
26 |
27 |
we have 4 functions in order to manage requests:
28 |
29 |
30 | - get: manages GET (method) requests.
31 | - post: manages POST (method) requests for example forms and etc.
32 | - go: manages both GET and POST requests
33 | - addHookFunction:this method runs per each request.
34 |
35 |
36 | here we have some examples:
37 |
38 | ```php
39 | my login page';
44 | });
45 |
46 | //example 2 : include files
47 | ROUTE::go('get','/register',function(){
48 | include 'pages/register.php';
49 | });
50 |
51 | //example 3 : for both get and post methods
52 | ROUTE::go('get|post','/logout',function(){
53 | echo 'logout page
';
54 | });
55 |
56 | //example 4 : using regex
57 | ROUTE::go('get','/show/{id:^\d*$}',function($id){
58 | echo 'your numeric id is : '.$id.'
';
59 | });
60 | ROUTE::post('/show/{id:^\d*$}',function($id){
61 | echo 'your numeric id is : '.$id.'
';
62 | });
63 |
64 | //example 5 : change to asp page
65 | ROUTE::get('/login.aspx',function(){
66 | //your page
67 | echo 'my login page
';
68 | });
69 | ?>
70 | ```
71 |
72 | URLs can be matched by REGEX.
73 |
74 | addHookFunction can be used to trace or takeing some actions before routing.
75 | here is an example of addHookFuncion :
76 |
77 | ```php
78 |
84 | ```
85 |
86 | you can use $data variable in addHookFunction to access requests.
87 | note: addHookFuncion must be written before any routes an home.php to work correctly.
88 |
89 | Iranian people can [visit here](www.tarhche.ir/?p=2466) for persian tutorials about this router.
90 |
91 | ## License
92 | This project is licensed under the GPL-3.0 License - see the [LICENSE.md](LICENSE.md) file for details
93 |
94 | ## contribute
95 | in order to developement or debug, you can create pull requests.
--------------------------------------------------------------------------------
/ROUTE.php:
--------------------------------------------------------------------------------
1 | 0){
65 | //remove {} from string
66 | if($param[0] == "{"){
67 | $param = substr($param,1);
68 | }
69 | if(substr($param,-1) == "}"){
70 | $param = substr($param,0,-1);
71 | }
72 | $param=explode(':',$param,2);
73 | if(preg_match("/$param[1]/i",self::$uri[$i])){
74 | array_push(self::$attrs,$uri[$i]);
75 | }
76 | }else{
77 | array_push(self::$attrs,$uri[$i]);
78 | }
79 | }else{
80 | array_push(self::$params,$param);
81 | }
82 | $i++;
83 | }
84 | }
85 |
86 | public static function matchCheck($definedPath){
87 | $definedPath = self::normalURI($definedPath);
88 | $definedPathParams = explode("/",$definedPath);
89 | if(count($definedPathParams) == count(self::$uri)){
90 | for($i = 0; $i < count($definedPathParams); $i++){
91 | if(preg_match("/\{(\S+)\}/",$definedPathParams[$i])){
92 | if(strpos($definedPathParams[$i],':')>0){
93 | //remove {} from string
94 | if($definedPathParams[$i][0] == "{"){
95 | $definedPathParams[$i] = substr($definedPathParams[$i],1);
96 | }
97 | if(substr($definedPathParams[$i],-1) == "}"){
98 | $definedPathParams[$i] = substr($definedPathParams[$i],0,-1);
99 | }
100 | $definedPathParams[$i]=(explode(':',$definedPathParams[$i],2));
101 | $regex=($definedPathParams[$i][1]);
102 | if(preg_match("/$regex/i",self::$uri[$i])){
103 | $definedPathParams[$i] = self::$uri[$i];
104 | }
105 | }else{
106 | $definedPathParams[$i] = self::$uri[$i];
107 | }
108 | }
109 | }
110 | }
111 | return $definedPathParams;
112 | }
113 |
114 | public static function commandRun(callable $action,array $args){
115 | self::runHookFunctions(array('uri'=>self::$uri,'params'=>self::$params,'action'=>$action,'args'=>$args));
116 | if(self::$founded){
117 | $result=(call_user_func_array($action,$args));
118 | if(!empty($result)){
119 | echo $result;
120 | }
121 | }
122 | }
123 |
124 | public static function go($methodPattern,$uri,callable $action){
125 | if(self::findMethod($methodPattern)){
126 | foreach(self::$methods as $method){
127 | if(is_callable(array(__CLASS__,$method))){
128 | if(call_user_func(array(__CLASS__,$method),$uri,$action)){
129 | return true;
130 | }
131 | }
132 | }
133 | }
134 | return false;
135 | }
136 |
137 | public static function get($uri,callable $action){
138 | $request_method=strtolower($_SERVER['REQUEST_METHOD']);
139 | if($request_method=='get'){
140 | self::findURI();
141 | $definedPathParams=self::matchCheck($uri);
142 | if($definedPathParams == self::$uri){
143 | self::findParams($uri,self::$uri);
144 | self::$founded=true;
145 | self::commandRun($action,self::$attrs);
146 | return true;
147 | }
148 | }
149 | }
150 |
151 | public static function post($uri,callable $action){
152 | $request_method=strtolower($_SERVER['REQUEST_METHOD']);
153 | if($request_method=='post'){
154 | self::findURI();
155 | $definedPathParams=self::matchCheck($uri);
156 | if($definedPathParams == self::$uri){
157 | self::findParams($uri,self::$uri);
158 | self::$founded=true;
159 | self::commandRun($action,self::$attrs);
160 | return true;
161 | }
162 | }
163 | }
164 | }
165 | ?>
--------------------------------------------------------------------------------
/home.php:
--------------------------------------------------------------------------------
1 | my login page';
14 | });
15 |
16 | //example 2 : include files
17 | ROUTE::go('get','/register',function(){
18 | include 'pages/register.php';
19 | });
20 |
21 | //example 3 : for both get and post methods
22 | ROUTE::go('get|post','/logout',function(){
23 | echo 'logout page
';
24 | });
25 |
26 | //example 4 : using regex
27 | ROUTE::go('get','/show/{id:^\d*$}',function($id){
28 | echo 'your numeric id is : '.$id.'
';
29 | });
30 | ROUTE::post('/show/{id:^\d*$}',function($id){
31 | echo 'your numeric id is : '.$id.'
';
32 | });
33 |
34 | //example 5 : change to asp page
35 | ROUTE::get('/login.aspx',function(){
36 | //your page
37 | echo 'my login page
';
38 | });
39 |
40 | //show 404 page if router cant match the request:
41 | if(!ROUTE::$founded){
42 | //here you return 404 view page (404.php for example)
43 | echo '404 Page';
44 | }
45 |
--------------------------------------------------------------------------------