What is the difference between $routes-> and $builder-> in Routing

Could someone explain the difference in usage of $routes-> and $builder-> in Routing as they seem to be used interchangeably, as seen in config/routes.php for App Skeleton. I have read through the Routing docs and could find only one example of $builder usage, but $routes would have achieved the same result.

Excerpt from App/config/routes.php :

/** @var \Cake\Routing\RouteBuilder $routes */
$routes->setRouteClass(DashedRoute::class);

$routes->scope('/', function (RouteBuilder $builder) {
    // Register scoped middleware for in scopes.
    $builder->registerMiddleware('csrf', new CsrfProtectionMiddleware([
        'httpOnly' => true,
    ]));

    $builder->applyMiddleware('csrf');
    $builder->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
    $builder->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
    $builder->fallbacks();
});

A few usage examples of each would be great too!! :slight_smile:

Many thanks.

The scope() method creates a routing scope where certain configurations like middleware are applied only to routes in that scope.

Here is the best example I can link in the docs:
https://book.cakephp.org/4/en/development/routing.html#connecting-scoped-middleware

This is not the same as Prefix Routing which is described here:
https://book.cakephp.org/4/en/development/routing.html#prefix-routing

Hi @coreytaylor many thanks for your reply. But you see what I mean. In the Connecting Scoped Middleware link you sent me $routes are used. I found another example in the CMS Tutorial where $builder is used:

Finding Articles By Tags

Still getting the impression they are interchangable, unless the routing code in some sections of the docs hasn’t been updated yet.

I don’t understand your question. Why does the variable name matter?

@coreytaylor Variable naming does matter, as the names we use make our code more understandable. If there are multiple variables, named differently, but doing the same job then this will likely lead to confusion.

Sure, but the code is clearly different here.

If you are asking if PHP cares if the name of the function() parameter is different, the answer is no.

Apparently, PHP doesn’t care about variable names - but we have “Variable naming guidelines” and the reason for having these guidelines is to have variable names that make sense.
E.g.
$truck = new Database()
seems like a bad variable name.
Similarly:
$router = new Builder()
and
$builder = new Router()

Someone who knows what builder and router are, this might not seem like a problem at all, but for someone who is trying to understand and learn in an ecosystem (like CakePHP), such ambiguities tend to push people back :slight_smile:
We fear what we don't understand

People want to “not-be-feared” from CakePHP, but stuff like this… really doesn’t help… and when the response is something like “… if PHP cares if the name of the function() parameter is different, then answer is no.”, then the answer becomes “I better try Laravel”.

:frowning_face:

1 Like