routes icon indicating copy to clipboard operation
routes copied to clipboard

Incompatible with AltoRouter v2.0.3

Open BunyaminUsMedia opened this issue 11 months ago • 7 comments

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.

BunyaminUsMedia avatar Jan 08 '25 12:01 BunyaminUsMedia

+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); 

martijnelzinga avatar Jan 13 '25 08:01 martijnelzinga

+1 This is one of the reasons to include the composer.lock in the repo to always have "the same code" / state.

jmslbam avatar Jan 14 '25 15:01 jmslbam

+1 is there a work around?

benedict-w avatar Jan 23 '25 17:01 benedict-w

i'm using a composer patch for now.

if you're unfamiliar with how:

  1. 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);
     	}
    
     	/**
    
  2. run composer require cweagans/composer-patches
  3. in composer.json, add the following:
    "patches": {
      "upstatement/routes": {
        "Fix change due to altorouter/altorouter": "PATH_TO_PATCH_FILE"
      }
    },
    
  4. run composer update upstatement/routes
  5. 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 avatar Jan 23 '25 18:01 jameelmoses

@jameelmoses always wondered (to lazy to look up) how to do a composer patch, thank you 😃

jmslbam avatar Jan 23 '25 20:01 jmslbam

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.

Celian-Nematis avatar Feb 12 '25 14:02 Celian-Nematis

Good catch, @Celian-Nematis. PR updated

jameelmoses avatar Feb 12 '25 16:02 jameelmoses

New tagged version 0.9.2 is now out.

Levdbas avatar Feb 25 '25 18:02 Levdbas