Skip to content

Commit 2f29d95

Browse files
author
stein
committed
Added shortcut functions for get, post, put, patch, delete and any
1 parent db30f54 commit 2f29d95

File tree

5 files changed

+186
-23
lines changed

5 files changed

+186
-23
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ $router->setNamespace('App\\Controllers\\');
9494
*/
9595
$router->add('/user/{id}/update', 'PUT', 'UserController::update');
9696

97+
98+
/**
99+
* Since version 1.1 there are shortcut methods for get, post, put, patch, delete and any.
100+
* You can use them as follow
101+
*/
102+
$router->get('/example/get', function() {}); // Will match GET requests
103+
$router->post('/example/post', function() {}); // Will match POST requests
104+
$router->put('/example/put', function() {}); // Will match PUT requests
105+
$router->patch('/example/patch', function() {}); // Will match PATCH requests
106+
$router->delete('/example/delete', function() {}); // Will match DELETE requests
107+
$router->any('/example/any', function() {}); // Will match GET, POST, PUT, PATCH, DELETE requests
108+
97109
/**
98110
* After all the routes are created the resolver must be initialized
99111
*/
@@ -165,6 +177,9 @@ $router->add('/hello/{a:name}/{?:lastname}', 'GET', function($name, $lastname =
165177

166178
<h2>Changelog</h2>
167179

180+
<b>v1.1.0</b>
181+
- Shortcut functions for get, post, put, patch, delete and any
182+
168183
<b>v1.0.0</b>
169184
- Updated readme
170185
- Posible to add default namespace

src/Interfaces/RouterInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function add($url, $method, $action);
3030
*
3131
* @return array
3232
*/
33-
public function getAll();
33+
public function getAllRoutes();
3434

3535
/**
3636
* Get routes by method
@@ -39,5 +39,5 @@ public function getAll();
3939
*
4040
* @return array
4141
*/
42-
public function getByMethod($method);
42+
public function getRoutesByMethod($method);
4343
}

src/RouteResolver.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,9 @@ public function __construct(RouterInterface $router)
4646
public function resolve($request)
4747
{
4848
// get all register routes with the same request method
49-
$routes = $this->router->getByMethod($request['method']);
49+
$routes = $this->router->getRoutesByMethod($request['method']);
5050
// remove trailing and leading slash
5151
$requestedUri = trim(preg_replace('/\?.*/', '', $request['uri']), '/');
52-
// get all segments of the requested uri in an array
53-
$requestedUriSegments = explode('/', $requestedUri, PHP_URL_PATH);
5452

5553
// loop trough the posible routes
5654
foreach ($routes as $route) {

src/Router.php

Lines changed: 91 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,92 @@ public function __construct($namespace = '')
5858
$this->namespace = $namespace;
5959
}
6060

61+
/**
62+
* Add a route with get method
63+
*
64+
* @param string $url
65+
* @param mixed $action
66+
*
67+
* @return void
68+
*/
69+
public function get($url, $action)
70+
{
71+
$this->add($url, 'GET', $action);
72+
}
73+
74+
/**
75+
* Add a route with post method
76+
*
77+
* @param string $url
78+
* @param mixed $action
79+
*
80+
* @return void
81+
*/
82+
public function post($url, $action)
83+
{
84+
$this->add($url, 'POST', $action);
85+
}
86+
87+
/**
88+
* Add a route with put method
89+
*
90+
* @param string $url
91+
* @param mixed $action
92+
*
93+
* @return void
94+
*/
95+
public function put($url, $action)
96+
{
97+
$this->add($url, 'PUT', $action);
98+
}
99+
100+
/**
101+
* Add a route with patch method
102+
*
103+
* @param string $url
104+
* @param mixed $action
105+
*
106+
* @return void
107+
*/
108+
public function patch($url, $action)
109+
{
110+
$this->add($url, 'PATCH', $action);
111+
}
112+
113+
/**
114+
* Add a route with delete method
115+
*
116+
* @param string $url
117+
* @param mixed $action
118+
*
119+
* @return void
120+
*/
121+
public function delete($url, $action)
122+
{
123+
$this->add($url, 'DELETE', $action);
124+
}
125+
126+
/**
127+
* Add a route for all posible methods
128+
*
129+
* @param string $url
130+
* @param mixed $action
131+
*
132+
* @return void
133+
*/
134+
public function any($url, $action)
135+
{
136+
$this->add($url, 'GET|POST|PUT|PATCH|DELETE', $action);
137+
}
138+
61139
/**
62140
* Add new route to routes array
63141
*
64142
* @param string $url
65143
* @param string $method
66-
* @param string $action
144+
* @param mixed $action
145+
*
146+
* @return void
67147
*/
68148
public function add($url, $method, $action)
69149
{
@@ -78,13 +158,13 @@ public function add($url, $method, $action)
78158
}
79159

80160
/**
81-
* Get RouteCollection
161+
* Set namespace
82162
*
83-
* @return RouteCollection
163+
* @param string $namespace
84164
*/
85-
public function getAll()
165+
public function setNamespace($namespace)
86166
{
87-
return $this->routes;
167+
$this->namespace = $namespace;
88168
}
89169

90170
/**
@@ -94,22 +174,18 @@ public function getAll()
94174
*
95175
* @return array
96176
*/
97-
public function getByMethod($method)
177+
public function getRoutesByMethod($method)
98178
{
99-
if ($this->routesByMethod && isset($this->routesByMethod[$method])) {
100-
return $this->routesByMethod[$method];
101-
}
102-
103-
return array();
179+
return ($this->routesByMethod && isset($this->routesByMethod[$method])) ? $this->routesByMethod[$method] : array();
104180
}
105181

106182
/**
107-
* Set namespace
183+
* Get all routes
108184
*
109-
* @param string $namespace
185+
* @return array
110186
*/
111-
public function setNamespace($namespace)
187+
public function getAllRoutes()
112188
{
113-
$this->namespace = $namespace;
189+
return $this->routes;
114190
}
115191
}

tests/RouterTest.php

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,80 @@ public function setUp()
3131
$this->router = new \Szenis\Router();
3232
}
3333

34+
/**
35+
* Test the get method
36+
*/
37+
public function testGet()
38+
{
39+
$this->router->get('/get/route', function(){});
40+
41+
$routes = $this->router->getRoutesByMethod('GET');
42+
43+
$this->assertEquals(1, count($routes));
44+
}
45+
46+
/**
47+
* Test the post method
48+
*/
49+
public function testPost()
50+
{
51+
$this->router->post('/post/route', function(){});
52+
53+
$routes = $this->router->getRoutesByMethod('POST');
54+
55+
$this->assertEquals(1, count($routes));
56+
}
57+
58+
/**
59+
* Test the put method
60+
*/
61+
public function testPut()
62+
{
63+
$this->router->put('/put/route', function(){});
64+
65+
$routes = $this->router->getRoutesByMethod('PUT');
66+
67+
$this->assertEquals(1, count($routes));
68+
}
69+
70+
/**
71+
* Test the patch method
72+
*/
73+
public function testPatch()
74+
{
75+
$this->router->patch('/patch/route', function(){});
76+
77+
$routes = $this->router->getRoutesByMethod('PATCH');
78+
79+
$this->assertEquals(1, count($routes));
80+
}
81+
82+
/**
83+
* Test the delete method
84+
*/
85+
public function testDelete()
86+
{
87+
$this->router->delete('/delete/route', function(){});
88+
89+
$routes = $this->router->getRoutesByMethod('DELETE');
90+
91+
$this->assertEquals(1, count($routes));
92+
}
93+
94+
/**
95+
* Test the any method
96+
*/
97+
public function testAny()
98+
{
99+
$this->router->any('/any/route', function(){});
100+
101+
$getRoutes = $this->router->getRoutesByMethod('GET');
102+
$putRoutes = $this->router->getRoutesByMethod('PUT');
103+
104+
$this->assertEquals(1, count($getRoutes));
105+
$this->assertEquals(1, count($putRoutes));
106+
}
107+
34108
/**
35109
* Test getAll method and expects 2 results
36110
*/
@@ -39,7 +113,7 @@ public function testGetAll()
39113
$this->router->add('/', 'GET', function(){});
40114
$this->router->add('/test', 'POST', function(){});
41115

42-
$routes = $this->router->getAll();
116+
$routes = $this->router->getAllRoutes();
43117

44118
$this->assertEquals(2, count($routes));
45119
}
@@ -52,7 +126,7 @@ public function testGetByMethod()
52126
$this->router->add('/', 'GET', function(){});
53127
$this->router->add('/test', 'POST', function(){});
54128

55-
$routes = $this->router->getByMethod('GET');
129+
$routes = $this->router->getRoutesByMethod('GET');
56130

57131
$this->assertEquals(1, count($routes));
58132
}
@@ -64,7 +138,7 @@ public function testGetByMethodWithoutResults()
64138
{
65139
$this->router->add('/', 'GET', function(){});
66140

67-
$routes = $this->router->getByMethod('POST');
141+
$routes = $this->router->getRoutesByMethod('POST');
68142

69143
$this->assertEquals(0, count($routes));
70144
}

0 commit comments

Comments
 (0)