Skip to content

Releases: pmjones/AutoRoute

2.1.1

22 Oct 14:48
Compare
Choose a tag to compare
  • Improve capitalization logic on word-separated segments in Creator

  • Improve json-encodability of Route::$exception

2.1.0

01 Jan 22:41
Compare
Choose a tag to compare
  • Added Route::asArray()

  • The Route now carries the Router messages that led to the route

  • Added invokable helper class for route generation

  • Added support for root-level catchalls with params

  • Route is now JsonSerializable

2.0.0

25 Aug 20:33
Compare
Choose a tag to compare

Change Log

2.0.0

Initial release.

Upgrading from 1.x to 2.0.0

Configuration

In 1.x, the AutoRoute options were configured with setters ...

$autoRoute = new AutoRoute(
    'Project\Http',
    dirname(__DIR__) . '/src/Project/Http/'
);

$autoRoute->setBaseUrl('/api');
$autoRoute->setIgnoreParams(1);
$autoRoute->setMethod('exec');
$autoRoute->setSuffix('Action');
$autoRoute->setWordSeparator('_');

... but in 2.x they are configured with named constructor parameters:

$autoRoute = new AutoRoute(
    namespace: 'Project\Http',
    directory: dirname(__DIR__) . '/src/Project/Http/',
    baseUrl: '/api',
    ignoreParams: 1,
    method: 'exec',
    suffix: 'Action',
    wordSeparator: '_',
);

Retrieving Objects

The methods to retrieve AutoRoute objects have been renamed from new*() to
get*():

// 1.x                          // 2.x
$autoRoute->newRouter();        $autoRoute->getRouter();
$autoRoute->newGenerator();     $autoRoute->getGenerator();
$autoRoute->newDumper();        $autoRoute->getDumper();
$autoRoute->newCreator();       $autoRoute->getCreator();

Route Properties

The 1.x Route property $params has been renamed to $arguments.

// 1.x
$response = call_user_func([$action, $route->method], ...$route->params);

// 2.x
$response = call_user_func([$action, $route->method], ...$route->arguments);

Error Handling

In 1.x, the Router would throw exceptions on errors:

try {
    $route = $router->route($request->method, $request->url[PHP_URL_PATH]);

} catch (\AutoRoute\InvalidNamespace $e) {
    // 400 Bad Request

} catch (\AutoRoute\InvalidArgument $e) {
    // 400 Bad Request

} catch (\AutoRoute\NotFound $e) {
    // 404 Not Found

} catch (\AutoRoute\MethodNotAllowed $e) {
    // 405 Method Not Allowed

}

In 2.x, the Router always returns a Route, and captures exceptions into the
returned Route property $error. Examine that property instead of catching
exceptions:

use AutoRoute\Exception;

switch ($route->error) {
    case null:
        // no errors! create the action class instance
        // and call it with the method and arguments.
        $action = Factory::newInstance($route->class);
        $method = $route->method;
        $arguments = $route->arguments;
        $response = $action->$method(...$arguments);
        break;

    case Exception\InvalidArgument::CLASS:
        $response = /* 400 Bad Request */;
        break;

    case Exception\NotFound::CLASS:
        $response = /* 404 Not Found */;
        break;

    case Exception\MethodNotAllowed::CLASS:
        $response = /* 405 Not Allowed */;
        /* N.b.: Examine $route->headers to find the 'allowed' methods for the
        resource, if any. */
        break;

    default:
        $response = /* 500 Server Error */;
        break;
}

Note that InvalidNamespace has been combined with InvalidArgument and is no
longer a separate exception type. Likewise, the new $headers property contains
suggested headers to return with the response.

1.1.1

23 Jun 15:56
Compare
Choose a tag to compare
  • Fix #6 (in Dumper, do not depend on find command)

  • Work around ReflectionType::__toString() deprecation PHP 7.4

1.1.0

30 Dec 18:10
Compare
Choose a tag to compare
  • Add autoroute-create functionality to create class file from HTTP verb and
    URL path

  • Update docs.

1.0.0

06 Apr 15:53
Compare
Choose a tag to compare

CHANGELOG

1.0.0

First stable release.