Skip to content
This repository was archived by the owner on Dec 6, 2022. It is now read-only.

Commit e9a4798

Browse files
fix psr-0 compliance
2 parents 421bbf1 + ab305bb commit e9a4798

File tree

5 files changed

+198
-173
lines changed

5 files changed

+198
-173
lines changed

README.md

100644100755
+18-18
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,31 @@ A simple Rails inspired PHP router class.
88
* Dynamic URL's: use URL segments as parameters.
99

1010
## Usage
11+
```php
12+
<?php
13+
require 'Router.php';
14+
require 'Route.php';
1115

12-
<?php
13-
require 'Router.php';
14-
require 'Route.php';
16+
$router = new Router();
1517

16-
$router = new Router();
18+
$router->setBasePath('/PHP-Router');
1719

18-
$router->setBasePath('/PHP-Router');
20+
// defining routes can be as simple as this
21+
$router->map('/', 'users#index');
1922

20-
// defining routes can be as simple as this
21-
$router->map('/', 'users#index');
23+
// or somewhat more complicated
24+
$router->map('/users/:id/edit/', array('controller' => 'SomeController', 'action' => 'someAction'), array('methods' => 'GET,PUT', 'name' => 'users_edit', 'filters' => array('id' => '(\d+)')));
2225

23-
// or somewhat more complicated
24-
$router->map('/users/:id/edit/', array('controller' => 'SomeController', 'action' => 'someAction'), array('methods' => 'GET,PUT', 'name' => 'users_edit', 'filters' => array('id' => '(\d+)')));
26+
// You can even specify closures as the Route's target
27+
$router->map('/hello/:name', function($name) { echo "Hello $name."; });
2528

26-
// You can even specify closures as the Route's target
27-
$router->map('/hello/:name', function($name) { echo "Hello $name."; });
28-
29-
// match current request URL & http method
30-
$target = $router->matchCurrentRequest();
31-
var_dump($target);
32-
33-
// generate an URL
34-
$router->generate('users_edit', array('id' => 5));
29+
// match current request URL & http method
30+
$target = $router->matchCurrentRequest();
31+
var_dump($target);
3532

33+
// generate an URL
34+
$router->generate('users_edit', array('id' => 5));
35+
```
3636

3737
## More information
3838
Have a look at the example.php file or read trough the class' documentation for a better understanding on how to use this class.

Route.php

-105
This file was deleted.

composer.json

+25-19
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1-
{
2-
"name": "dannyvankooten/php-router",
3-
"description": "Simple PHP Router, supports REST and reverse routing.",
4-
"keywords": ["router", "routing", "php", "rest"],
5-
"homepage": "https://github.com/dannyvankooten/PHP-Router",
6-
"license": "MIT",
7-
"authors": [
8-
{
9-
"name": "Danny van Kooten",
10-
"email": "[email protected]",
11-
"homepage": "http://dannyvankooten.com/"
12-
}
13-
],
14-
"require": {
15-
"php": ">=5.3.0"
16-
},
17-
"autoload": {
18-
"classmap": ["Router.php", "Route.php"]
19-
}
1+
{
2+
"name": "dannyvankooten/php-router",
3+
"description": "Simple PHP Router, supports REST and reverse routing.",
4+
"keywords": ["router", "routing", "php", "rest"],
5+
"homepage": "https://github.com/dannyvankooten/PHP-Router",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Danny van Kooten",
10+
"email": "[email protected]",
11+
"homepage": "http://dannyvankooten.com/"
12+
},
13+
{
14+
"name": "Jefersson Nathan",
15+
"email": "[email protected]"
16+
}
17+
],
18+
"require": {
19+
"php": ">=5.3.0"
20+
},
21+
"autoload": {
22+
"psr-0": {
23+
"PHProuter": "src/"
24+
}
25+
}
2026
}

src/PHPRouter/Route.php

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
namespace PHPRouter;
3+
4+
class Route
5+
{
6+
/**
7+
* URL of this Route
8+
* @var string
9+
*/
10+
private $_url;
11+
12+
/**
13+
* Accepted HTTP methods for this route
14+
* @var array
15+
*/
16+
private $_methods = array('GET','POST','PUT','DELETE');
17+
18+
/**
19+
* Target for this route, can be anything.
20+
* @var mixed
21+
*/
22+
private $_target;
23+
24+
/**
25+
* The name of this route, used for reversed routing
26+
* @var string
27+
*/
28+
private $_name;
29+
30+
/**
31+
* Custom parameter filters for this route
32+
* @var array
33+
*/
34+
private $_filters = array();
35+
36+
/**
37+
* Array containing parameters passed through request URL
38+
* @var array
39+
*/
40+
private $_parameters = array();
41+
42+
public function getUrl()
43+
{
44+
return $this->_url;
45+
}
46+
47+
public function setUrl($url)
48+
{
49+
$url = (string) $url;
50+
51+
// make sure that the URL is suffixed with a forward slash
52+
if(substr($url,-1) !== '/') $url .= '/';
53+
54+
$this->_url = $url;
55+
}
56+
57+
public function getTarget()
58+
{
59+
return $this->_target;
60+
}
61+
62+
public function setTarget($target)
63+
{
64+
$this->_target = $target;
65+
}
66+
67+
public function getMethods()
68+
{
69+
return $this->_methods;
70+
}
71+
72+
public function setMethods(array $methods)
73+
{
74+
$this->_methods = $methods;
75+
}
76+
77+
public function getName()
78+
{
79+
return $this->_name;
80+
}
81+
82+
public function setName($name)
83+
{
84+
$this->_name = (string) $name;
85+
}
86+
87+
public function setFilters(array $filters)
88+
{
89+
$this->_filters = $filters;
90+
}
91+
92+
public function getRegex()
93+
{
94+
return preg_replace_callback("/:(\w+)/", array(&$this, 'substituteFilter'), $this->_url);
95+
}
96+
97+
private function substituteFilter($matches)
98+
{
99+
if (isset($matches[1]) && isset($this->_filters[$matches[1]])) {
100+
return $this->_filters[$matches[1]];
101+
}
102+
103+
return "([\w-]+)";
104+
}
105+
106+
public function getParameters()
107+
{
108+
return $this->_parameters;
109+
}
110+
111+
public function setParameters(array $parameters)
112+
{
113+
$this->_parameters = $parameters;
114+
}
115+
}

0 commit comments

Comments
 (0)