routes
routes copied to clipboard
Incompatible with AltoRouter v2.0.3
Hi,
When updating PHP composer plugins, it automatically updates AltoRouter to v2.0.3. This version of AltoRouter is incompatible with Upstatement Routes v0.9.1.
I get the following error:
Uncaught TypeError: AltoRouter::map(): Argument #4 ($name) must be of type ?string, array given, called in /var/www/site/vendor/upstatement/routes/Routes.php on line 71 and defined in /var/www/site/vendor/altorouter/altorouter/AltoRouter.php:120
with the following example code:
Routes::map('users/[i:id]', function () { ... });
The origin of the problem is this:
// Routes.php line 71 & 72
$upstatement_routes->router->map('GET|POST|PUT|DELETE', $route, $callback, $args);
$upstatement_routes->router->map('GET|POST|PUT|DELETE', $route, $callback, $args);
It expects a string $name at the place of the $args but the array $args is given.
+1; $args argument seems to be unused?
// Routes.php line 71 & 72
$upstatement_routes->router->map('GET|POST|PUT|DELETE', trailingslashit($route), $callback );
$upstatement_routes->router->map('GET|POST|PUT|DELETE', untrailingslashit($route), $callback);
+1 This is one of the reasons to include the composer.lock in the repo to always have "the same code" / state.
+1 is there a work around?
i'm using a composer patch for now.
if you're unfamiliar with how:
- create a patchfile with the following contents:
diff --git a/Routes.php b/Routes.php index bb508b5..5af84fc 100644 --- a/Routes.php +++ b/Routes.php @@ -68,8 +68,8 @@ $upstatement_routes->router->setBasePath($base_path); } $route = self::convert_route($route); - $upstatement_routes->router->map('GET|POST|PUT|DELETE', trailingslashit($route), $callback, $args); - $upstatement_routes->router->map('GET|POST|PUT|DELETE', untrailingslashit($route), $callback, $args); + $upstatement_routes->router->map('GET|POST|PUT|DELETE', trailingslashit($route), $callback); + $upstatement_routes->router->map('GET|POST|PUT|DELETE', untrailingslashit($route), $callback); } /** - run
composer require cweagans/composer-patches - in composer.json, add the following:
"patches": { "upstatement/routes": { "Fix change due to altorouter/altorouter": "PATH_TO_PATCH_FILE" } }, - run
composer update upstatement/routes - you should see something like the following in your terminal:
Gathering patches for dependencies. This might take a minute. - Installing upstatement/routes (0.9.1): Extracting archive - Applying patches for upstatement/routes patches/upstatement-routes.patch (Fix change due to altorouter/altorouter)
@jameelmoses always wondered (to lazy to look up) how to do a composer patch, thank you 😃
Hi @jameelmoses , @benedict-w ,
I get the same error on updating my wordpress theme but I don't agree with this fix. With this fix, you can't use a named route from altorouter map function.
The problem comes from the argument type of $name in altorouter, so Route::map must use the right type.
So
// Routes.php line 53
public static function map($route, $callback, $args = array())
should be
// Routes.php line 53
public static function map($route, $callback, $args = '')
The $args variable should also be renamed as $name, to make sense.
Final fix :
// Routes.php line 53
public static function map($route, $callback, $name = '')
In this way, you'll retain all the functionality of the altorouter package.
Good catch, @Celian-Nematis. PR updated
New tagged version 0.9.2 is now out.