Skip to content

Commit 427c9b3

Browse files
authored
Merge pull request #8129 from cakephp/5.next-route-builder
RouterBuilder updates.
2 parents b4bc60e + 649b44e commit 427c9b3

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

en/appendices/5-3-migration-guide.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ Pagination
184184
Routing
185185
-------
186186

187+
- Added ``RouteBuilder::setOptions()`` method to set default route options at
188+
the scope level. This allows you to apply options like ``_host``, ``_https``,
189+
and ``_port`` to all routes within a scope without repeating them on each
190+
route. Options set at the scope level are inherited by nested scopes and can
191+
be overridden on individual routes.
187192
- ``EntityRoute`` now handles enum value conversions. This enables you to use
188193
enum backed properties as route parameters. When an enum backed property is
189194
used in routing, the enum's ``value`` or ``name`` will be used.

en/development/routing.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,60 @@ of ``connect()``::
440440
// Set lang to be a persistent parameter
441441
->setPersist(['lang']);
442442

443+
Setting Default Options for Routes in a Scope
444+
----------------------------------------------
445+
446+
You can set default options that will be applied to all routes within a scope
447+
using the ``setOptions()`` method. This is useful when you want to apply the
448+
same options (like ``_host``, ``_https``, or ``_port``) to multiple routes
449+
without repeating them::
450+
451+
$routes->scope('/api', function (RouteBuilder $routes) {
452+
// Set default options for all routes in this scope
453+
$routes->setOptions([
454+
'_host' => 'api.example.com',
455+
'_https' => true
456+
]);
457+
458+
// These routes will automatically have _host and _https set
459+
$routes->get('/users', ['controller' => 'Users', 'action' => 'index']);
460+
$routes->get('/posts', ['controller' => 'Posts', 'action' => 'index']);
461+
});
462+
463+
Options set via ``setOptions()`` are:
464+
465+
- **Inherited by nested scopes** - Child scopes automatically receive the parent's default options
466+
- **Overridable on individual routes** - Options passed to ``connect()`` or HTTP verb methods take precedence over defaults
467+
- **Merged with route-specific options** - Default options are combined with any options you specify on individual routes
468+
469+
Example with nested scopes and overrides::
470+
471+
$routes->scope('/api', function (RouteBuilder $routes) {
472+
$routes->setOptions(['_host' => 'api.example.com']);
473+
474+
// This route uses the default host
475+
$routes->get('/public', ['controller' => 'Public', 'action' => 'index']);
476+
477+
// This route overrides the default host
478+
$routes->get('/internal', [
479+
'controller' => 'Internal',
480+
'action' => 'index',
481+
'_host' => 'internal.example.com'
482+
]);
483+
484+
// Nested scope inherits the default host
485+
$routes->scope('/v2', function (RouteBuilder $routes) {
486+
// This also uses api.example.com
487+
$routes->get('/users', ['controller' => 'Users', 'action' => 'index']);
488+
});
489+
});
490+
491+
The ``setOptions()`` method is particularly useful for:
492+
493+
- Setting the same ``_host`` for an entire API scope
494+
- Enforcing ``_https => true`` for secure sections of your application
495+
- Configuring ``_port`` for routes that should use non-standard ports
496+
443497
Passing Parameters to Action
444498
----------------------------
445499

0 commit comments

Comments
 (0)