FastRoute icon indicating copy to clipboard operation
FastRoute copied to clipboard

simpleDispatcher Deprecated

Open Sam7367238 opened this issue 6 months ago • 5 comments

When I tried to use it, it was deprecated, what alternatives can I use? The README also uses it, so I suggest changing it up.

Sam7367238 avatar Jul 03 '25 14:07 Sam7367238

@Sam7367238 the functions are being replaced by this.

So, for simpleDispatcher you would do something like:

$routeDefinition = static function(FastRoute\ConfigureRoutes $r): void {
};

$dispatcher = \FastRoute\FastRoute::recommendedSettings($routeDefinition, __DIR__ . '/var/routes.cache')
    ->disableCache()
    ->dispatcher();

Documentation hasn't yet been updated to reflect that and we'll act on it.

Thanks for flagging it.

lcobucci avatar Jul 03 '25 14:07 lcobucci

Well, I have this code

$dispatcher = simpleDispatcher(function(RouteCollector $routeCollector) {
            $routes = include(BASE_PATH . "/routes/web.php");

            foreach ($routes as $route) {
                $routeCollector -> addRoute(...$route);
            }
        });

So how do I just sort of transition it into the code you provided?

Sam7367238 avatar Jul 03 '25 15:07 Sam7367238

The closure you're passing to simpleDispatcher() goes to the first variable I defined, and that's it.

I'd only advise not to disable the caching when running things in production 👍

lcobucci avatar Jul 04 '25 05:07 lcobucci

Alright, how is this?

$routeDefinition = static function(RouteCollector $routeCollector): void {
            $routes = include(BASE_PATH . "/routes/web.php");

            foreach ($routes as $route) {
                $routeCollector -> addRoute(...$route);
            }
        };

        $dispatcher = FastRoute::recommendedSettings($routeDefinition, BASE_PATH . "/routes.cache") -> disableCache() -> dispatcher();

        $routeInfo = $dispatcher -> dispatch(
            $request -> getMethod(),
            $request -> getPathInfo()
        );

       // Handle the request...

One more thing, can you also explain to me the $routeDefinition? It's a variable that equals a static function (which for me is inside a method), that's new to me. And also how does the caching work?

Sam7367238 avatar Jul 04 '25 07:07 Sam7367238

If it's too obvious then close this issue already 😴

Sam7367238 avatar Aug 03 '25 05:08 Sam7367238