Skip to content

Commit d7af5ad

Browse files
committed
Defend against only using #[Route] on a class method. Will discuss changing this with Matt.
1 parent 66971da commit d7af5ad

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

src/Framework/Routing/Routing.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,22 @@ public static function build(BuildContext $context): void
8282
$routes = new RouteCollection();
8383

8484
foreach (BuildTools::getReflectionClasses() as $class) {
85-
$classAttr = ReflectionHelpers::getAttribute($class, RouteAttribute::class);
86-
if (!$classAttr) {
85+
if (!$class->isInstantiable()) {
8786
continue;
8887
}
8988

90-
if (!$class->isInstantiable()) {
91-
throw new BuildStepFailureException(
92-
message: 'Controller #[Route]s cannot be placed on non-instantiable classes',
93-
target: $class,
94-
);
89+
$classAttr = ReflectionHelpers::getAttribute($class, RouteAttribute::class);
90+
if (!$classAttr) {
91+
foreach ($class->getMethods() as $method) {
92+
if (ReflectionHelpers::getAttribute($method, RouteAttribute::class)) {
93+
throw new BuildStepFailureException(
94+
message: 'Controller [#Routes] on methods must be paired with a #[Route] on the class',
95+
target: $method,
96+
);
97+
}
98+
}
99+
100+
continue;
95101
}
96102

97103
foreach ($class->getMethods() as $method) {

0 commit comments

Comments
 (0)