1010
1111namespace yii \base ;
1212
13+ use ReflectionClass ;
1314use Yii ;
1415use yii \di \ServiceLocator ;
1516
@@ -106,8 +107,9 @@ class Module extends ServiceLocator
106107 * configurations. Mapped actions are dispatched by {@see runAction()} without requiring a hosting controller, in
107108 * parallel to {@see $controllerMap}.
108109 *
109- * Each entry maps an ID (the first route segment) to either a fully qualified class name or an array configuration
110- * accepted by {@see \Yii::createObject()}. The class must extend {@see Action}.
110+ * Each entry maps a route relative to this module, without leading or trailing slashes, to either a fully qualified
111+ * class name or an array configuration accepted by {@see \Yii::createObject()}. The class must extend
112+ * {@see Action}. The key becomes the action's {@see Action::$id}.
111113 *
112114 * Usage example:
113115 *
@@ -118,14 +120,17 @@ class Module extends ServiceLocator
118120 * ]
119121 * ```
120122 *
121- * Action IDs in this map take precedence over IDs in {@see $controllerMap} and over module/controller resolution.
123+ * A key takes precedence over {@see $controllerMap} and over module/controller resolution for the exact route only .
122124 *
123- * Routes whose first segment is not present here fall through to the legacy controller pipeline unchanged.
125+ * Routes that do not equal a key fall through to the legacy controller pipeline unchanged, including longer routes
126+ * that start with a key (for example `health/status` with a `health` key), so a sub-module or controller with the
127+ * same first segment still handles them.
124128 *
125129 * Resolution scope: this map is consulted only by the module whose {@see runAction()} handles the route (typically
126130 * the application). Unlike {@see $controllerMap}, it is NOT resolved recursively across sub-modules during
127- * nested-route dispatch. To expose a standalone action under a sub-module, rely on {@see $actionNamespace} (which
128- * IS resolved recursively), or register the action in the dispatching module's {@see $actionMap}.
131+ * nested-route dispatch. To expose a standalone action under a sub-module, set the sub-module's
132+ * {@see $actionNamespace} (which IS resolved recursively), or register the action in the dispatching module's
133+ * {@see $actionMap}.
129134 *
130135 * @since 22.0
131136 */
@@ -147,9 +152,11 @@ class Module extends ServiceLocator
147152 * The namespace where standalone {@see Action} subclasses are located for convention-based discovery, parallel to
148153 * {@see $controllerNamespace} for controllers.
149154 *
150- * If not set, defaults to the value of {@see $controllerNamespace} during {@see init()}, so standalone actions can
151- * live next to controllers under the same root. Set this property to a different namespace to organize standalone
152- * actions in their own folder (for example, vertical slices under `app\UseCase`):
155+ * Discovery is opt-in: when this property is `null` (the default) or an empty string, the module resolves no route
156+ * to a standalone action by convention, on applications and sub-modules alike, and a sub-module does not inherit
157+ * the setting of its parent. Set it to the value of {@see $controllerNamespace} to keep standalone actions next to
158+ * controllers, or to a different namespace to organize them in their own folder (for example, vertical slices
159+ * under `app\UseCase`):
153160 *
154161 * Usage example:
155162 *
@@ -159,7 +166,9 @@ class Module extends ServiceLocator
159166 *
160167 * With the example above, route `posts/index` resolves to `app\UseCase\posts\IndexAction`, route
161168 * `posts/view-summary` resolves to `app\UseCase\posts\ViewSummaryAction`, and so on. The same hyphen, slash, and
162- * CamelCase rules apply as for controllers (see {@see createControllerByID()}).
169+ * CamelCase rules apply as for controllers (see {@see createControllerByID()}). Only the exact spelling of the
170+ * class name resolves. The discovered action's {@see Action::$id} is the last route segment (`view-summary`), and
171+ * {@see Action::getUniqueId()} returns the full route.
163172 *
164173 * Resolution order in {@see runAction()}: explicit {@see $controllerMap}/{@see $actionMap} entries are checked
165174 * first, then sub-modules, then controllers by namespace, and finally standalone actions by namespace. When a
@@ -272,10 +281,6 @@ public function init()
272281 $ this ->controllerNamespace = substr ($ class , 0 , $ pos ) . '\\controllers ' ;
273282 }
274283 }
275-
276- if ($ this ->actionNamespace === null ) {
277- $ this ->actionNamespace = $ this ->controllerNamespace ;
278- }
279284 }
280285
281286 /**
@@ -608,12 +613,13 @@ public function setModules($modules)
608613 *
609614 * If the route is empty, the method will use [[defaultRoute]].
610615 *
611- * Since version 22.0, when the first segment of the route matches a key in [[actionMap]], the action is dispatched
612- * as a standalone action via [[runMappedAction()]], without resolving a controller. When no controller resolves the
613- * route either, the method falls back to convention-based standalone action lookup under [[actionNamespace]] via
614- * [[createStandaloneAction()]]. When a controller method AND a standalone Action class both match the same route,
615- * the method throws [[InvalidConfigException]] so the developer disambiguates instead of silently shadowing one or
616- * the other.
616+ * Since version 22.0, when the route, trimmed of leading and trailing slashes, equals a key in [[actionMap]], the
617+ * action is dispatched as a standalone action via [[runMappedAction()]], without resolving a controller. When no
618+ * controller resolves the route either, the method falls back to convention-based standalone action lookup under
619+ * [[actionNamespace]] via [[createStandaloneAction()]]; the resulting action runs through the lifecycle of the
620+ * module that owns it and of every ancestor of that module. When a controller method AND a standalone Action class
621+ * both match the same route, the method throws [[InvalidConfigException]] so the developer disambiguates instead
622+ * of silently shadowing one or the other.
617623 *
618624 * @param string $route The route that specifies the action.
619625 * @param array $params The parameters to be passed to the action.
@@ -627,13 +633,8 @@ public function runAction($route, $params = [])
627633 {
628634 $ normalizedRoute = trim ((string ) $ route , '/ ' );
629635
630- if ($ normalizedRoute !== '' && $ this ->actionMap !== []) {
631- $ separator = strpos ($ normalizedRoute , '/ ' );
632- $ actionId = $ separator === false ? $ normalizedRoute : substr ($ normalizedRoute , 0 , $ separator );
633-
634- if (isset ($ this ->actionMap [$ actionId ])) {
635- return $ this ->runMappedAction ($ actionId , $ params );
636- }
636+ if ($ normalizedRoute !== '' && isset ($ this ->actionMap [$ normalizedRoute ])) {
637+ return $ this ->runMappedAction ($ normalizedRoute , $ params );
637638 }
638639
639640 $ parts = $ this ->createController ($ route );
@@ -724,8 +725,13 @@ protected function runMappedAction(string $id, array $params): mixed
724725 * CamelCased class name with an `Action` suffix, the preceding segments form a sub-namespace prefix, and the result
725726 * is looked up under the owning module's [[actionNamespace]].
726727 *
727- * The resolved class must extend [[Action]] and must NOT extend [[Controller]]; otherwise this method returns
728- * `null` and the caller falls through to the controller pipeline (or to `InvalidRouteException`).
728+ * The resolved class must be declared with exactly the computed name, so case and hyphen variants of a route do not
729+ * resolve, must extend [[Action]], must NOT extend [[Controller]], and must be instantiable (not abstract, no
730+ * private constructor); otherwise this method returns `null` and the caller falls through to the controller
731+ * pipeline (or to `InvalidRouteException`).
732+ *
733+ * The returned action is owned by the module that resolved it. Its [[Action::$id]] is the last route segment, and
734+ * [[Action::getUniqueId()]] returns the full route including the owning module's unique ID.
729735 *
730736 * @param string $route The route relative to this module.
731737 *
@@ -788,15 +794,26 @@ protected function createStandaloneAction(string $route): Action|null
788794 return null ;
789795 }
790796
797+ $ reflection = new ReflectionClass ($ className );
798+
799+ if ($ reflection ->getName () !== $ className ) {
800+ return null ;
801+ }
802+
791803 if (!is_subclass_of ($ className , Action::class) || is_subclass_of ($ className , Controller::class)) {
792804 return null ;
793805 }
794806
807+ if (!$ reflection ->isInstantiable ()) {
808+ return null ;
809+ }
810+
795811 /** @var Action $action */
796812 $ action = Yii ::createObject ($ className );
797813
798- $ action ->id = $ route ;
814+ $ action ->id = $ idPart ;
799815
816+ $ action ->setRoutePrefix (rtrim ($ prefix , '/ ' ));
800817 $ action ->setModule ($ this );
801818
802819 return $ action ;
@@ -805,8 +822,10 @@ protected function createStandaloneAction(string $route): Action|null
805822 /**
806823 * Runs a standalone {@see Action} instance through the standard module/action event lifecycle.
807824 *
808- * Module ancestors fire [[EVENT_BEFORE_ACTION]] outer-to-inner before execution and [[EVENT_AFTER_ACTION]]
809- * inner-to-outer afterwards, mirroring [[Controller::runAction()]]. The action itself also fires
825+ * The module chain starts at the module that owns the action ({@see Action::getModule()}, or this module when the
826+ * action has none) and includes every ancestor up to the application. The chain fires [[EVENT_BEFORE_ACTION]]
827+ * outer-to-inner before execution and [[EVENT_AFTER_ACTION]] inner-to-outer afterwards, mirroring
828+ * [[Controller::runAction()]]. The action itself also fires
810829 * [[Controller::EVENT_BEFORE_ACTION]] and [[Controller::EVENT_AFTER_ACTION]] so behaviors attached via the action's
811830 * [[behaviors()]] (for example [[\yii\filters\AccessControl]] or [[\yii\filters\VerbFilter]]) participate in the
812831 * lifecycle.
@@ -826,8 +845,10 @@ protected function runStandaloneAction(Action $action, array $params): mixed
826845 Yii::$ app ->requestedAction = $ action ;
827846 }
828847
829- $ modulesOuterToInner = [$ this ];
830- $ ancestor = $ this ->module ;
848+ $ owner = $ action ->getModule () ?? $ this ;
849+
850+ $ modulesOuterToInner = [$ owner ];
851+ $ ancestor = $ owner ->module ;
831852
832853 while ($ ancestor !== null ) {
833854 array_unshift ($ modulesOuterToInner , $ ancestor );
0 commit comments