LaravelCollective / annotations

Route and Event Annotations for the Laravel Framework
MIT License
365 stars 73 forks source link

Routes repeated because extended classes #101

Closed somoza closed 3 years ago

somoza commented 4 years ago

If I extend a class on the controller, the routes get repeated... For example, I've 5 controllers that extend a common controller X. The X controller routes are being added 6 times into routes.scanned.php and Laravel 8 complains about repeated route names.

Thanks!

somoza commented 4 years ago

Problem located. ReflectionClass is not able to ignore parent classes... Looking forward to a solution.

somoza commented 3 years ago

Any update so far?

loonytoons commented 3 years ago

Oh wow, this totally slipped through my net. I'll take a look this weekend

isa95Ar commented 3 years ago

I found a little fix for solve this problem if you go to annotations\src\Routing\Annotations\AnnotationSet.php

protected function getMethodAnnotations(ReflectionClass $class, SimpleAnnotationReader $reader)
    {
        $annotations = [];

        foreach ($class->getMethods() as $method) {
            if($method->class == $class->name) {

                $results = $reader->getMethodAnnotations($method);

                if (count($results) > 0) {
                    $annotations[$method->name] = $results;
                }
            }
        }

        return $annotations;
    }

the validation
if($method->class == $class->name)

make the difference.

somoza commented 3 years ago

Thanks @isa95Ar , sounds like a good approach.

somoza commented 3 years ago

I've prepared this pull request based on @isa95Ar suggestion. https://github.com/LaravelCollective/annotations/pull/104

loonytoons commented 3 years ago

Merged into 8.0