首页 > 解决方案 > Laravel Annotation 扫描路线不起作用

问题描述

最近我正在使用LaravelCollective/annotations,我正在尝试在我们的项目中使用它。在实现包扫描路由后,不要扫描控制器中定义的路由

安装包:

"laravelcollective/annotations": "^8.0.1",

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

php artisan route:scan

这个文件里面是空的storage/framework/models.scanned并且storage/framework/routes.scanned

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

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');
    }
}

标签: phplaravellaravelcollective

解决方案


检查您是否在 AnnotationsServiceProvider 类中扩展了“Collective\Annotations\AnnotationsServiceProvider”。如果您生成了默认扩展“Illuminate\Support\ServiceProvider”的类。

如果您在类中获得了自动生成的方法“boot”和“register”,请将它们删除或确保在 em.xml 中调用父级“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")
 */

推荐阅读