Skip to content

Commit 1652921

Browse files
author
stein
committed
v1
1 parent 41f482c commit 1652921

6 files changed

Lines changed: 110 additions & 30 deletions

File tree

README.md

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
[![Build Status](https://travis-ci.org/stein189/Simple-PHP-Router.svg?branch=master)](https://travis-ci.org/stein189/Simple-PHP-Router)
55

6-
Note: This package is still in development, to use it add "minimum-stability": "dev" to your composer.json.
7-
86
<h2>Getting started</h2>
97

108
<b>Step 1 - .htaccess file</b>
@@ -28,7 +26,7 @@ create an .htaccess file in the root of your project and fill it with the code b
2826
````
2927

3028
<b>Step 2 - require szenis/routing</b><br/>
31-
In your terminal execute: ``composer require szenis/routing 0.*``
29+
In your terminal execute: ``composer require szenis/routing``
3230

3331
<b>Step 3 - create index.php</b><br/>
3432
Create the file index.php in the root of your project
@@ -44,7 +42,7 @@ use Szenis\RouteResolver;
4442
````
4543
to your index.php
4644

47-
<b>Step 6 *optional</b><br/>
45+
<b>Optional</b><br/>
4846
For debuging purpose add the following to your index.php
4947
```php
5048
error_reporting(E_ALL);
@@ -60,42 +58,55 @@ For the sake of simplicity consider this code to be inside index.php
6058
$router = new Router();
6159

6260
/**
63-
* Add a route to the homepage
64-
* The first argument is the route that we want to look for
65-
* The second argument are the accepted methods, methods have to be seperated by a |
66-
* The third and last argument is the full path to the action that has to be executed,
67-
* this could also be an closure
61+
* Route matches the url '/' for the GET method
6862
*/
69-
$router->add('/', 'GET|PUT', 'App\Controllers\PageController::index');
63+
$router->add('/', 'GET', function() {
64+
// the closure will be executed when route is triggerd and will return 'hello world'
65+
return 'hello world!';
66+
});
7067

7168
/**
7269
* It is posible to add one or multiple wildcards in one route
7370
*/
74-
$router->add('/user/{id}', 'GET', 'App\Controllers\UserController::show');
71+
$router->add('/user/{id}', 'GET', function($id) {
72+
return $id;
73+
});
7574

7675
/**
77-
* Closure route example
76+
* It is also posible to allow mulitple methods for one route (methods should be separated with a '|')
7877
*/
7978
$router->add('/user/{id}/edit', 'GET|POST', function($id) {
80-
echo $id;
81-
82-
return;
79+
return 'user id: '.$id;
8380
});
8481

82+
/**
83+
* Or when u are using controllers in a namespace you could give the full path to a controller (controller::action)
84+
*/
85+
$router->add('/user/{id}/delete', 'DELETE', 'App\Controllers\UserController::delete');
86+
87+
/**
88+
* When all the controller are in the same namespace you could set the default namespace like so
89+
*/
90+
$router->setNamespace('App\\Controllers\\');
91+
92+
/**
93+
* The route now uses the default namespace + the given namespace
94+
*/
95+
$router->add('/user/{id}/update', 'PUT', 'UserController::update');
96+
97+
/**
98+
* After all the routes are created the resolver must be initialized
99+
*/
85100
$resolver = new RouteResolver($router);
86101

87102
/**
88-
* resolve the route
89-
* the resolve function will search for an matching route
90-
* when a matching route is found the given function will be triggerd.
91-
* lets asume we have triggerd the route: /user/10
92-
* the function `show` from the class `UserController` will be called
93-
* the wildcard which is the number `10` will be passed on to the `show` function
103+
* resolve the route and receive the response
94104
*/
95-
$resolver->resolve([
105+
$response = $resolver->resolve([
96106
'uri' => $_SERVER['REQUEST_URI'],
97107
'method' => $_SERVER['REQUEST_METHOD'],
98108
]);
109+
99110
````
100111

101112
<b>When a route is not found an RouteNotFoundException will be thrown</b>
@@ -133,10 +144,10 @@ The following options exist
133144
</p>
134145

135146
```php
136-
// In this case the id may be a number
147+
// The id must be a number
137148
$router->add('/user/{n:id}', 'GET', 'App\Controllers\UserController::show');
138149

139-
// In this case the id may only contain alfabetic chars or numbers (or both)
150+
// The id must contain either alfabetic chars or numbers
140151
$router->add('/user/{an:id}', 'GET', 'App\Controllers\UserController::show');
141152

142153
// Now we want everything behind docs/ in the page variable
@@ -154,6 +165,10 @@ $router->add('/hello/{a:name}/{?:lastname}', 'GET', function($name, $lastname =
154165

155166
<h2>Changelog</h2>
156167

168+
<b>v1.0.0</b>
169+
- Updated readme
170+
- Posible to add default namespace
171+
157172
<b>v0.9.0</b>
158173
- 100% test coverage
159174
- minimum php version reduced from 5.4 to 5.3

src/Route.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ class Route
3939
*/
4040
private $action;
4141

42+
/**
43+
* Namespace for the route
44+
*
45+
* @var string
46+
*/
47+
private $namespace;
48+
4249
/**
4350
* Route constructor
4451
*
@@ -130,4 +137,24 @@ public function setAction($action)
130137

131138
$this->action = $action;
132139
}
140+
141+
/**
142+
* Get namespace
143+
*
144+
* @return string
145+
*/
146+
public function getNamespace()
147+
{
148+
return $this->namespace;
149+
}
150+
151+
/**
152+
* Set namespace
153+
*
154+
* @param string $namespace
155+
*/
156+
public function setNamespace($namespace)
157+
{
158+
$this->namespace = $namespace;
159+
}
133160
}

src/RouteResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function resolve($request)
6464
return call_user_func_array($route->getAction(), $arguments);
6565
}
6666

67-
$className = substr($route->getAction(), 0, strpos($route->getAction(), '::'));
67+
$className = $route->getNamespace() . substr($route->getAction(), 0, strpos($route->getAction(), '::'));
6868
$functionName = substr($route->getAction(), strpos($route->getAction(), '::') + 2);
6969

7070
return call_user_func_array(array((new $className), $functionName), $arguments);

src/Router.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ class Router implements RouterInterface
2828
*/
2929
private $factory;
3030

31+
/**
32+
* Default namespace
33+
*
34+
* @var string
35+
*/
36+
private $namespace;
37+
3138
/**
3239
* An array with all the registerd routes
3340
*
@@ -45,9 +52,10 @@ class Router implements RouterInterface
4552
/**
4653
* Router constructor
4754
*/
48-
public function __construct()
55+
public function __construct($namespace = '')
4956
{
5057
$this->factory = new RouteFactory();
58+
$this->namespace = $namespace;
5159
}
5260

5361
/**
@@ -60,7 +68,8 @@ public function __construct()
6068
public function add($url, $method, $action)
6169
{
6270
$route = $this->factory->create($url, $method, $action);
63-
71+
$route->setNamespace($this->namespace);
72+
6473
$this->routes[] = $route;
6574

6675
foreach ($route->getMethod() as $method) {
@@ -93,4 +102,14 @@ public function getByMethod($method)
93102

94103
return array();
95104
}
105+
106+
/**
107+
* Set namespace
108+
*
109+
* @param string $namespace
110+
*/
111+
public function setNamespace($namespace)
112+
{
113+
$this->namespace = $namespace;
114+
}
96115
}

tests/RouteResolverTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class RouteResolverTest extends BaseTest
2929
public function setUp()
3030
{
3131
// initialize router
32-
$router = new \Szenis\Router(new \Szenis\RouteFactory());
32+
$router = new \Szenis\Router('RouterTests\\');
3333

3434
// add some routes
3535
$router->add('/test', 'GET', function(){
@@ -44,7 +44,11 @@ public function setUp()
4444
return $number;
4545
});
4646

47-
$router->add('/call/controller', 'GET', 'RouterTests\TestController::index');
47+
$router->add('/call/controller', 'GET', 'TestController::index');
48+
49+
$router->setNamespace('');
50+
51+
$router->add('/call/controller', 'PUT', 'RouterTests\TestController::index');
4852

4953
// initialize resolver
5054
$this->resolver = new \Szenis\RouteResolver($router);
@@ -123,4 +127,19 @@ public function testResolveWithClassPath()
123127

124128
$this->assertEquals('index called', $response);
125129
}
130+
131+
/**
132+
* Test action after namespace has changed
133+
*/
134+
public function testNamespace()
135+
{
136+
$request = array(
137+
'uri' => '/call/controller',
138+
'method' => 'PUT',
139+
);
140+
141+
$response = $this->resolver->resolve($request);
142+
143+
$this->assertEquals('index called', $response);
144+
}
126145
}

tests/RouterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class RouterTest extends BaseTest
2828
*/
2929
public function setUp()
3030
{
31-
$this->router = new \Szenis\Router(new \Szenis\RouteFactory());
31+
$this->router = new \Szenis\Router();
3232
}
3333

3434
/**

0 commit comments

Comments
 (0)