bildirgec.org

icli kofte

11 yıl önce üye olmuş, 5 yazı yazmış. 1 yorum yazmış.

url mapper,url routing

icli kofte | 18 May 2011 18:01

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.

Explode in Java

icli kofte | 05 November 2009 09:57

Merhaba ,php web programcılığında kullandığım fonksiyonlardan biri olan Explode yöntemini java’ya uyarladım java’da yaptığım sorgulamalarda işimi kolaylaştırdı.Yazdığım sınıf :

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
/*
* @author: Can
* @since:2009
* @access:public
*/
class explode
{
public static String[] explode(String delimiter, String string )
{
StringBuffer buffer = new StringBuffer(string);
int limit =1;
for (int i = 0; i < buffer.length(); i++ )
{
if(delimiter.indexOf(buffer.charAt(i)) != -1)
limit++;
}
String[] elements = new String[limit];
int y,z = 0;
if(buffer.toString().indexOf(delimiter) !=-1)
{
while(buffer.length()>0)
{
if(buffer.toString().indexOf(delimiter) !=-1)
{
y = buffer.toString().indexOf(delimiter);
if(y != buffer.toString().lastIndexOf(delimiter))
{
elements[z] =buffer.toString().substring(0,y);
z++;
buffer.delete(0, y+1);
}
else if (buffer.toString().lastIndexOf(delimiter) == y)
{
elements[z] = buffer.toString().substring(0,buffer.toString().indexOf(delimiter));
z++;
buffer.delete(0, buffer.toString().indexOf(delimiter)+1);
elements[z] = buffer.toString();z++;
buffer.delete(0,buffer.length());
}
}
}
}
else
{
elements[0] = buffer.toString();
}
buffer = null;
return elements;
}
}

Implode in Java

icli kofte | 03 November 2009 18:16

Bir önceki yazımda yazdığım Explode in Java’da bu sefer parçaları birleştirmeyi yapalım. Method , php de ki implode fonksiyonuyla aynı.
Sınıf:

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
/* @class implode
* @author Can Acar
* @since 2009
* @access public
*/
class implode{
public static String implode(String glue , String[] pieces)
{
if(glue == null || pieces == null )
{
return null;
}else
{
StringBuffer result = new StringBuffer();
if(pieces.length > 0)
{
result.append(pieces[0]);
for(int i = 1; i < pieces.length; i++)
{
result.append(glue);
result.append(pieces[i]);
}
}
return result.toString();
}
}
}

Php ile Hook yapımı

icli kofte | 17 June 2009 19:12

function test() {
echo ‘Head yazısı denemesi’;
}
modul_add(‘head’,’test’);

function modul_register($where,$function)
{
global $modul;
$modul[$yer][$function]=Array(‘function’=>$function);
return;
}

function modul_add($where,$function){return modul_register($where,$function);
}

function modul_action($tag){global $modul;if(!isset($modul[$tag])){return;}

foreach((array)$modul[$tag] as $the_){call_user_func( $the_[‘function’],$tag);}

}

modul_action(‘head’);

bu kod ile kendi yaptığınız sistemde modül ekleme ve eklenen modülü sayfanın tanımlanmış herhangi bir yerine modulü injekt yapılır , bu yazdığım basite indirgedim.