diff --git a/README.md b/README.md index d0b7777..792e973 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,8 @@ Supported Laravel Versions: - v1.x: Laravel 6 and later ([see the source](https://github.com/izniburak/laravel-auto-routes/tree/1.x)) Run the following command directly in your Project path: -``` -$ composer require izniburak/laravel-auto-routes +```sh +composer require izniburak/laravel-auto-routes ``` **OR** open your `composer.json` file and add the package like this: ```json @@ -39,16 +39,16 @@ $ composer require izniburak/laravel-auto-routes } ``` after run the install command. -``` -$ composer install +```sh +composer install ``` The service provider of the Package will be **automatically discovered** by Laravel. After that, you should publish the config file via following command: -``` -$ php artisan vendor:publish --provider="Buki\AutoRoute\AutoRouteServiceProvider" +```sh +php artisan vendor:publish --provider="Buki\AutoRoute\AutoRouteServiceProvider" ``` Greate! You can start to use **Auto Route** Package. diff --git a/composer.json b/composer.json index 72ebbfb..db4f9b3 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "php": "^8.1" }, "require-dev": { - "illuminate/routing": "^10.0", + "illuminate/routing": "^10.0|^11.0", "phpunit/phpunit": "^10.1" }, "autoload": { diff --git a/src/AutoRoute.php b/src/AutoRoute.php index 68b25e1..88ff07e 100644 --- a/src/AutoRoute.php +++ b/src/AutoRoute.php @@ -77,23 +77,20 @@ public function auto(string $prefix, string $controller, array $options = []): v $except = $options['except'] ?? []; $patterns = $options['patterns'] ?? []; + $routeName = trim($options['as'] ?? ($options['name'] ?? trim($prefix, '/')), '.') . '.'; + if ($routeName === '.') { + $routeName = ''; + } + $this->router->group( - array_merge($options, [ - 'prefix' => $prefix, - 'as' => isset($options['as']) - ? "{$options['as']}." - : (isset($options['name']) - ? "{$options['name']}." - : trim($prefix, '/') . '.' - ), - ]), + array_merge($options, ['prefix' => $prefix, 'as' => $routeName]), function () use ($controller, $only, $except, $patterns) { [$class, $className] = $this->resolveControllerName($controller); $classRef = new ReflectionClass($class); foreach ($classRef->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { // Check the method should be added into Routes or not. if (in_array($method->class, [BaseController::class, "{$this->namespace}\\Controller"]) - || $method->getDeclaringClass()->getParentClass()->getName() === BaseController::class + || ($method->getDeclaringClass()->getParentClass() && $method->getDeclaringClass()->getParentClass()->getName() === BaseController::class) || !$method->isPublic() || str_starts_with($method->name, '__')) { continue;