php - 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');
}
}
解决方案
检查您是否在 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")
*/
推荐阅读
- vba - VBA 表达式求值顺序
- ibm-watson - 如何通过Watson 助手中的计数变量获取矩阵元素(对话)
- c# - 如何从 C# 将引用参数传递给 PowerShell 脚本
- windows - Visual Studio 2019 Community 可以安装在 Windows Server 2016 Core 上吗?
- java - oop java方法按顺序递增数字
- python - 使用 xpath 从单个 DIV 类中获取多个选择性值
- excel - Excel 宏 - 根据条件复制特定单元格
- c++ - 设置 OpenGL 红皮书项目时遇到问题
- html - 无论如何,是否可以通过任何应用程序在静态页面的 head 标记中添加自定义 HTML/脚本?
- c++ - 检查/列出 C++ 中的所有 bash 命令?