Yazmış olduğum bu sınıf belirlenmiş bir url şablonuna göre $_SERVER[“REQUEST_URI”] sonucu alıp set edilen şablona göre bölüp controller , controller’ın methodu ve parametre olarak geri dönüyor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* Router
*
* @author can acar
* @copyright 2011
* @version $Id$
* @access public
*/
class Router
{
protected static $instance = array();
protected $Route;
/**
* Router::__construct()
*
* @return
*/
private function __construct()
{
}
/**
* Router::instance()
*
* @return
*/
public static function instance()
{
if( isset( self::$instance ) and ( self::$instance instanceof self ) )
{
return self::$instance;
} else
{
self::$instance = new self();
return self::$instance;
}
}
/**
* Router::Connect()
*
* @param mixed $path
* @param mixed $params
* @param mixed $filter
* @return
*/
public function Connect( $path, $params = array(), $filter = array() )
{
$this->Route[$path] = new Route( $path, $params, $filter );
}
/**
* Router::Match()
*
* @param mixed $url
* @return
*/
public function Match( $url )
{
$Matches = array();
$url = '/'.trim($url,'/');
foreach ( $this->Route as $Route )
{
$regex = preg_replace( '/:(\w+)/', '(?P<$1>\w+)', $Route->path );
$regex = str_replace( '/', '\/', $regex );
if( preg_match( "/^" . $regex . "$/", $url, $Match ) )
{
foreach ( $Route->params as $key => $value )
{
$Matches[$key] = $value;
}
foreach ( $Route->filter as $property => $Regex )
{
if( array_key_exists( $property, $Match ) )
{
$o = preg_match( '/^' . $Regex . '$/', $Match[$property], $t );
$Matches[$property] = $t[0];
}
}
unset($property,$Regex,$Match);
}
}unset($Route);
return $Matches;
}
}
******************************
/**
* Route
*
* @author can acar
* @copyright 2011
* @version $Id$
* @access public
*/
class Route
{
public $path;
public $params = array();
public $filter;
/**
* Route::__construct()
*
* @param mixed $path
* @param mixed $params
* @param mixed $filter
* @return
*/
public function __construct( $path, $params, $filter )
{
$this->path = '/'.trim( $path,'/');
$this->params = $params;
$this->filter = $filter;
return $this;
}
}

Bu sınıfları index.php e kayıt yapıp
index.php nin alt satırlarınada

1
2
3
4
5
6
7
8
9
10
$r = Router::instance();
//$r->Connect('',array('controller'=>'default','action'=>'index'));
$r->Connect('index.php',array('controller'=>'default','action'=>'index'));
$r->Connect('index.php/kid-:id',array('controller'=>'default','action'=>'kategori'),array('id'=>'([^-][0-9]{0,})'));
$r->Connect('index.php/:type',array('controller'=>'default','action'=>'index'),array('type'=>'[\w+]{0,}'));
$r->Connect('index.php/:type/:id',array('controller'=>'default','action'=>'index'),array('type'=>'[\w+]{0,}','id'=>'[0-9]{0,}'));
$k =$r->Match($_SERVER["REQUEST_URI"]);
echo "<pre>";
echo var_dump($k);
echo "</pre>";

Tarayıcıdan da index.php/kid-12
yada index.php/hede/20
gibi denemeler yapıp sonucu:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
(array) Array
(
[controller] => default
[action] => kategori
[id] => 12
)
//////
(array) Array
(
[controller] => default
[action] => index
[type] => hede
[id] => 20
)

olarak yazılacaktır.