Skip to content

Commit 2f1c137

Browse files
committed
add new method rest() for quick registration RESTful resources routes
1 parent a238cbc commit 2f1c137

File tree

6 files changed

+123
-21
lines changed

6 files changed

+123
-21
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-12-04
6+
* Time: 14:58
7+
*/
8+
9+
namespace Inhere\Route\Examples\Controllers;
10+
11+
/**
12+
* Class RestController
13+
* @package Inhere\Route\Examples\Controllers
14+
*/
15+
class RestController
16+
{
17+
public function indexAction()
18+
{
19+
echo __METHOD__ . PHP_EOL;
20+
}
21+
22+
public function viewAction()
23+
{
24+
echo __METHOD__ . PHP_EOL;
25+
}
26+
27+
public function createAction()
28+
{
29+
echo __METHOD__ . PHP_EOL;
30+
}
31+
32+
public function updateAction()
33+
{
34+
echo __METHOD__ . PHP_EOL;
35+
}
36+
37+
public function patchAction()
38+
{
39+
echo __METHOD__ . PHP_EOL;
40+
}
41+
42+
public function deleteAction()
43+
{
44+
echo __METHOD__ . PHP_EOL;
45+
}
46+
}

examples/cached.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Inhere\Route\Dispatcher;
1515
use Inhere\Route\CachedRouter;
16+
use Inhere\Route\Examples\Controllers\RestController;
1617

1718
require __DIR__ . '/simple-loader.php';
1819

@@ -48,6 +49,7 @@ function dump_routes() {
4849
}
4950

5051
$router->get('/routes', 'dump_routes');
52+
$router->rest('/rest', RestController::class);
5153

5254
/** @var array $routes */
5355
$routes = require __DIR__ . '/some-routes.php';

examples/object.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313

1414
use Inhere\Route\Dispatcher;
15+
use Inhere\Route\Examples\Controllers\RestController;
1516
use Inhere\Route\ORouter;
1617

1718
require __DIR__ . '/simple-loader.php';
@@ -35,7 +36,9 @@
3536
'controllerSuffix' => 'Controller',
3637
]);
3738

38-
$router->get('/routes', function() use($router) {
39+
$router->get('/routes', function() {
40+
global $router;
41+
3942
echo "<h1>All Routes.</h1><pre><h2>StaticRoutes:</h2>\n";
4043
print_r($router->getStaticRoutes());
4144
echo "<h2>RegularRoutes:</h2>\n";
@@ -64,6 +67,8 @@
6467
$router->map($route[0], $route[1], $route[2], isset($route[3]) ? $route[3] : []);
6568
}
6669

70+
$router->rest('/rest', RestController::class);
71+
6772
$router->any('*', function () {
6873
echo "This is fallback handler\n";
6974
});

src/AbstractRouter.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,14 +253,14 @@ public function parseParamRoute($route, array $params, array $conf)
253253
/**
254254
* handle auto route match, when config `'autoRoute' => true`
255255
* @param string $path The route path
256-
* @param string $controllerNamespace controller namespace. eg: 'app\\controllers'
257-
* @param string $controllerSuffix controller suffix. eg: 'Controller'
256+
* @internal string $cnp controller namespace. eg: 'app\\controllers'
257+
* @internal string $sfx controller suffix. eg: 'Controller'
258258
* @return bool|callable
259259
*/
260-
public function matchAutoRoute($path, $controllerNamespace, $controllerSuffix = '')
260+
public function matchAutoRoute($path)
261261
{
262-
$cnp = trim($controllerNamespace);
263-
$sfx = trim($controllerSuffix);
262+
$cnp = trim($this->config['controllerNamespace']);
263+
$sfx = trim($this->config['controllerSuffix']);
264264
$tmp = trim($path, '/- ');
265265

266266
// one node. eg: 'home'

src/ORouter.php

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,6 @@ public function map($methods, $route, $handler, array $opts = [])
300300
}
301301

302302
$params = $this->getAvailableParams(isset($opts['params']) ? $opts['params'] : []);
303-
304303
list($first, $conf) = $this->parseParamRoute($route, $params, $conf);
305304

306305
// route string have regular
@@ -316,6 +315,56 @@ public function map($methods, $route, $handler, array $opts = [])
316315
return $this;
317316
}
318317

318+
/**
319+
* register a group restful routes for the controller class.
320+
* ```php
321+
* $router->rest('/users', UserController::class);
322+
* ```
323+
* @param string $prefix eg '/users'
324+
* @param string $controllerClass
325+
* @param array $map You can append or change default map list.
326+
* [
327+
* 'index' => null, // set value is empty to delete.
328+
* 'list' => 'get', // add new route
329+
* ]
330+
* @param array $opts Common options
331+
* @return ORouter
332+
*/
333+
public function rest($prefix, $controllerClass, array $map = [], array $opts = [])
334+
{
335+
$map = array_merge([
336+
'index' => ['GET'],
337+
'create' => ['POST'],
338+
'view' => ['GET', '{id}', ['id' => '[1-9]\d*']],
339+
'update' => ['PUT', '{id}', ['id' => '[1-9]\d*']],
340+
'patch' => ['PATCH', '{id}', ['id' => '[1-9]\d*']],
341+
'delete' => ['DELETE', '{id}', ['id' => '[1-9]\d*']],
342+
], $map);
343+
//$opts = array_merge([], $opts);
344+
345+
foreach ($map as $action => $conf) {
346+
if (!$conf || !$action) {
347+
continue;
348+
}
349+
350+
$route = $prefix;
351+
352+
// '/users/{id}'
353+
if (isset($conf[1]) && ($subPath = trim($conf[1]))) {
354+
// allow define a abs route. '/user-other-info'. it's not prepend prefix.
355+
$route = $subPath[0] === '/' ? $subPath : $prefix . '/' . $subPath;
356+
}
357+
358+
if (isset($conf[2])) {
359+
$opts['params'] = $conf[2];
360+
}
361+
362+
$this->map($conf[0], $route, $controllerClass . '@' . $action, $opts);
363+
}
364+
365+
return $this;
366+
}
367+
319368
/*******************************************************************************
320369
* route match
321370
******************************************************************************/
@@ -379,10 +428,7 @@ public function match($path, $method = 'GET')
379428
}
380429

381430
// handle Auto Route
382-
if (
383-
$this->config['autoRoute'] &&
384-
($handler = $this->matchAutoRoute($path, $this->config['controllerNamespace'], $this->config['controllerSuffix']))
385-
) {
431+
if ($this->config['autoRoute'] && ($handler = $this->matchAutoRoute($path))) {
386432
return [self::FOUND, $path, [
387433
'handler' => $handler,
388434
'option' => [],

src/RouterInterface.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
*/
1515
interface RouterInterface
1616
{
17-
// match result status
17+
/** match result status list */
1818
const FOUND = 1;
1919
const NOT_FOUND = 2;
2020
const METHOD_NOT_ALLOWED = 3;
2121

2222
const FAV_ICON = '/favicon.ico';
2323
const DEFAULT_REGEX = '[^/]+';
2424

25-
// supported method list
25+
/** supported method list */
2626
const ANY = 'ANY';
2727

2828
const GET = 'GET';
@@ -32,22 +32,25 @@ interface RouterInterface
3232
const DELETE = 'DELETE';
3333
const OPTIONS = 'OPTIONS';
3434
const HEAD = 'HEAD';
35+
36+
const COPY = 'COPY';
37+
const PURGE = 'PURGE';
38+
const LINK = 'LINK';
39+
const UNLINK = 'UNLINK';
40+
const LOCK = 'LOCK';
41+
const UNLOCK = 'UNLOCK';
3542
const SEARCH = 'SEARCH';
3643
const CONNECT = 'CONNECT';
3744
const TRACE = 'TRACE';
3845

39-
/**
40-
* supported methods
41-
* @var array
42-
*/
46+
/** @var array supported methods */
4347
const SUPPORTED_METHODS = [
4448
'ANY',
45-
'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD', 'SEARCH', 'CONNECT', 'TRACE',
49+
'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD',
50+
'COPY', 'PURGE', 'LINK', 'UNLINK', 'LOCK', 'UNLOCK', 'VIEW', 'SEARCH', 'CONNECT', 'TRACE',
4651
];
4752

48-
/**
49-
* the matched result index key
50-
*/
53+
/** the matched result index key */
5154
const INDEX_STATUS = 0;
5255
const INDEX_PATH = 1;
5356
const INDEX_INFO = 2;

0 commit comments

Comments
 (0)