Laravel 注释扫描路由不起作用

Laravel Annotation scanning route doesn't work

最近我正在使用 LaravelCollective/annotations 并且我正在尝试在我们的 project.After 中使用它来实现包扫描路由不扫描控制器中定义的路由

安装包:

"laravelcollective/annotations": "^8.0.1",

在我们的应用程序中实施包并尝试使用此命令:

php artisan route:scan

此文件在 storage/framework/models.scannedstorage/framework/routes.scanned

中是空的

这是我在 app.conf 文件中注册的 AnnotationsServiceProvider class 内容

App\Providers\AnnotationsServiceProvider::class,

AnnotationsServiceProvider.php内容:

<?php

namespace App\Providers;

use App\Http\Controllers\Backend\AdminController;
use App\Models\User;
use Collective\Annotations\AnnotationsServiceProvider as ServiceProvider;

class AnnotationsServiceProvider extends ServiceProvider
{
    /**
     * The classes to scan for event annotations.
     *
     * @var array
     */
    protected $scanEvents = [];

    /**
     * The classes to scan for route annotations.
     *
     * @var array
     */
    protected $scanRoutes = [
        AdminController::class
    ];

    /**
     * The classes to scan for model annotations.
     *
     * @var array
     */
    protected $scanModels = [
        User::class,
    ];

    /**
     * Determines if we will auto-scan in the local environment.
     *
     * @var bool
     */
    protected $scanWhenLocal = true; //imported from define ('true', (bool)1, true);

    /**
     * Determines whether or not to automatically scan the controllers
     * directory (App\Http\Controllers) for routes
     *
     * @var bool
     */
    protected $scanControllers = true; //imported from define ('true', (bool)1, true);

    /**
     * Determines whether or not to automatically scan all namespaced
     * classes for event, route, and model annotations.
     *
     * @var bool
     */
    protected $scanEverything = false; //imported from define ('false', (bool)1, true);

    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        parent::register(); //imported from AnnotationsServiceProvider::class
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
        parent::boot(); //imported from AnnotationsServiceProvider::class
    }
}

最后这是我的简单控制器,我想在其中使用注释:

/*
 * @Controller()
 * @Resource("/posts",names="post")
 */

class AdminController extends Controller
{
    public function index()
    {
        return view('layouts.pages.posts.test');
    }

    /*
     * @Get("/posts/search")
     */
    public function search()
    {
        return view('layouts.pages.posts.test');
    }
}

检查您是否在 AnnotationsServiceProvider class 中扩展了“Collective\Annotations\AnnotationsServiceProvider”。如果您生成了 class,则默认情况下您正在扩展“Illuminate\Support\ServiceProvider”。

如果您在 class 中获得自动生成的方法“boot”和“register”,请删除它们或确保在 em 中调用父方法“boot”和“register”。

使用“app.conf”,希望您参考 laravel 项目中的 config/app.php 文件?

namespace App\Providers;

use Collective\Annotations\AnnotationsServiceProvider as ServiceProvider;

class AnnotationsServiceProvider extends ServiceProvider
{

    /**
     * The classes to scan for event annotations.
     *
     * @var array
     */
    protected $scanEvents = [];

    /**
     * The classes to scan for route annotations.
     *
     * @var array
     */
    protected $scanRoutes = [
        \App\Http\Controllers\AdminController::class
    ];

    /**
     * The classes to scan for model annotations.
     *
     * @var array
     */
    protected $scanModels = [
        \App\User::class,
    ];

    /**
     * Determines if we will auto-scan in the local environment.
     *
     * @var bool
     */
    protected $scanWhenLocal = true;

    /**
     * Determines whether or not to automatically scan the controllers
     * directory (App\Http\Controllers) for routes
     *
     * @var bool
     */
    protected $scanControllers = true;

    /**
     * Determines whether or not to automatically scan all namespaced
     * classes for event, route, and model annotations.
     *
     * @var bool
     */
    protected $scanEverything = false;



    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //
        parent::register();
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
        parent::boot();
    }


}

这应该使“php artisan route:scan”命令起作用。

$ php artisan route:scan
Routes scanned!

要使您的路线正常工作,您需要在注释评论中添加一个额外的 *

/**
 * Show the Index Page
 * @Get("/posts/search")
 */