Skip to content

Commit 99cfb52

Browse files
authored
Add files via upload
1 parent 30ce9a0 commit 99cfb52

File tree

4 files changed

+285
-0
lines changed

4 files changed

+285
-0
lines changed

apps/controller/testController.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/**
4+
* Simple Controller
5+
*/
6+
class testController
7+
{
8+
public function __construct()
9+
{
10+
return "This is testController";
11+
}
12+
13+
public function simple()
14+
{
15+
echo "This is testController simple Method";
16+
}
17+
18+
19+
20+
}
21+
22+
?>

apps/route/app.php

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<?php
2+
namespace App;
3+
/*
4+
App Route
5+
Jafran Hasan
6+
http://jafran.returnxero.com
7+
*/
8+
class Route {
9+
10+
// if no route found it returns false
11+
// its used for 404 page routing
12+
public static $routeFound = false;
13+
14+
// main method
15+
private static function map($method, $routes, $callback, $option=NULL)
16+
{
17+
18+
// formating routes
19+
$routes = self::formatRoute($routes);
20+
$request = self::formatRoute($_SERVER['REQUEST_URI']);
21+
$pattern = "@^" . preg_replace('/\\\:[a-zA-Z0-9\_\-]+/', '([a-zA-Z0-9\-\_]+)', preg_quote($routes)) . "$@D";
22+
23+
// check if requested uri matches to routes
24+
if(preg_match($pattern, $request, $matches)) {
25+
array_shift($matches);
26+
27+
// request method checking
28+
if($_SERVER['REQUEST_METHOD']==$method || $method == "ANY"){
29+
30+
self::$routeFound = true;
31+
32+
// parameter searches
33+
if($matches!=NULL){
34+
// parameter found
35+
if(is_callable($callback)){
36+
return call_user_func_array($callback, $matches);
37+
}
38+
39+
} else {
40+
// no paramater found
41+
42+
// is function callable
43+
if(is_callable($callback)){
44+
45+
return call_user_func($callback);
46+
47+
} else {
48+
// function is not callable .
49+
if($option != NULL){
50+
switch ($option) {
51+
// redirection
52+
case "redirect":
53+
$callback = self::formatRoute($callback);
54+
header("Location: {$callback}");
55+
// return false;
56+
break;
57+
58+
default:
59+
// controller load
60+
return false;
61+
break;
62+
}
63+
} else {
64+
// return controllers
65+
static::LoadController($callback);
66+
}
67+
}
68+
}
69+
} else {
70+
// server request doesn't match
71+
http_response_code(404);
72+
}
73+
74+
75+
}
76+
}
77+
78+
79+
// get request
80+
public static function get($routes, $callback)
81+
{
82+
return self::map('GET', $routes, $callback);
83+
}
84+
85+
// post request
86+
public static function post($routes, $callback)
87+
{
88+
return self::map('POST', $routes, $callback);
89+
}
90+
91+
// put request
92+
public static function put($routes, $callback)
93+
{
94+
return self::map('PUT', $routes, $callback);
95+
}
96+
97+
// delete request
98+
public static function delete($routes, $callback)
99+
{
100+
return self::map('DELETE', $routes, $callback);
101+
}
102+
103+
// any cross request
104+
public static function any($routes, $callback)
105+
{
106+
return self::map('ANY', $routes, $callback);
107+
}
108+
109+
// redirecttion
110+
public static function redirect($routes, $callback)
111+
{
112+
return self::map('ANY', $routes, $callback, "redirect");
113+
}
114+
115+
116+
117+
// format route
118+
private static function formatRoute($route)
119+
{
120+
$route = strtok($route, '?');
121+
$result = trim($route, '/');
122+
if ($result === '')
123+
{
124+
return '/';
125+
}
126+
return strtok($result, "?");
127+
}
128+
129+
protected static function LoadController($controller){
130+
if(strpos($controller, "@") > 0){
131+
$class_name = explode("@", $controller)[0];
132+
$method_name = explode("@", $controller)[1];
133+
} else {
134+
$class_name = $controller;
135+
$method_name = "__construct";
136+
}
137+
138+
// load class from controller folders
139+
if(file_exists(BASE_PATH . "/apps/controller/" . $class_name . ".php")){
140+
require_once BASE_PATH . "/apps/controller/" . $class_name . ".php";
141+
if(class_exists($class_name)){
142+
// print_r($method_name);
143+
$call = new $class_name;
144+
// returning callback function
145+
return $call->$method_name();
146+
147+
}
148+
}
149+
}
150+
151+
152+
153+
154+
}
155+
156+
157+
158+
159+
/*
160+
Jafran Hasan
161+
*/
162+
163+
164+
165+
166+
167+
168+
169+
?>

apps/route/route.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
use App\Route;
3+
4+
/***
5+
***
6+
** Route::get(ROUTE, CALLBACK Function);
7+
***
8+
***/
9+
10+
11+
12+
13+
Route::get("/", function(){
14+
echo "Home page";
15+
});
16+
17+
18+
// no matter you use /test or test or test/ , these are same .
19+
Route::get("test", function(){
20+
echo "Test Page";
21+
});
22+
23+
Route::get("/another", function(){
24+
echo "Another Page";
25+
});
26+
27+
Route::get("another-page/", function(){
28+
echo "Another Page 2";
29+
});
30+
31+
32+
33+
34+
35+
36+
37+
/*
38+
Redirection is easier now
39+
*/
40+
41+
// ineternal
42+
Route::redirect("/here", "/there");
43+
44+
// external
45+
Route::redirect("jafran", "http://facebook.com/iamjafran");
46+
47+
48+
49+
50+
51+
/*
52+
Parameter should be :PARAMETER .
53+
you can use any variable like
54+
*/
55+
Route::get("/user/:id/", function($myid){
56+
echo "id is " . $myid;
57+
});
58+
59+
Route::get("/name/:name/", function($name){
60+
echo "My name is " . $name;
61+
});
62+
63+
64+
65+
/*
66+
Using Controller Routing
67+
Pattern CONTROLLER NAME @ METHOD
68+
'testCrontroller@simple' means 'testController' is a class and 'simple' is a method inside this class
69+
*/
70+
71+
Route::get("controller", "testController");
72+
73+
Route::get("another/controller", "testController@simple");
74+

index.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
define("BASE_PATH", __DIR__ . DIRECTORY_SEPARATOR);
3+
date_default_timezone_set("Asia/Dhaka");
4+
use App\Route;
5+
6+
/*
7+
Develeper: Jafran Hasan
8+
Developer URI: http:://facebook.com/iamjafran
9+
*/
10+
11+
12+
// user routes
13+
require_once __DIR__ . '/apps/route/app.php';
14+
require_once __DIR__ . '/apps/route/route.php';
15+
16+
17+
if(Route::$routeFound==NULL){
18+
echo "<center>Error 404 | Not Found</center>";
19+
}
20+

0 commit comments

Comments
 (0)