php - laravel route 返回 404,即使它显示在 route:list
问题描述
服务提供者
<?php
namespace MyPackage\Faq;
use http\Exception\RuntimeException;
use Illuminate\Support\ServiceProvider;
class FaqServiceProvider extends ServiceProvider
{
/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot(): void
{
// set paths using realpath
$src = realpath(__DIR__."/../src/");
$paths = [
'views' => realpath($src.'/Http/resources/views'),
'migrations' => realpath(__DIR__.'/../database/migrations'),
'routes' => realpath($src.'/Http/routes/web.php'),
'public' => realpath( __DIR__.'/../public')
];
// make sure paths exist
foreach($paths as $k => $v) {
if($v === false)
throw new RuntimeException(sprintf("%s -- path for '%s' not found", __METHOD__, $k));
}
// $this->loadTranslationsFrom($paths['translations'], 'faq');
$this->loadViewsFrom($paths['views'], 'packagefaq');
$this->loadMigrationsFrom($paths['migrations']);
$this->loadRoutesFrom($paths['routes']);
$this->publishes([
$paths['public'] => public_path('packagefaq')
], 'public');
// Publishing is only necessary when using the CLI.
if ($this->app->runningInConsole()) {
$this->bootForConsole();
}
}
/**
* Register any package services.
*
* @return void
*/
public function register(): void
{
// config currently empty
$this->mergeConfigFrom(__DIR__.'/../config/faq.php', 'faq');
// Register the service the package provides.
$this->app->singleton('faq', function ($app) {
return new Faq;
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['faq'];
}
/**
* Console-specific booting.
*
* @return void
*/
protected function bootForConsole(): void
{
// Publishing the configuration file.
$this->publishes([
__DIR__.'/../config/faq.php' => config_path('faq.php'),
], 'faq.config');
}
}
src/Http/routes/web.php
<?php
use Illuminate\Support\Facades\Route;
Route::get('/faq', function() { return view('packagefaq::faq'); });
我之前直接在包代码中测试了视图并生成了我想要的并且在正确的文件夹中:src/Http/resources/views/faq.blade.php - 暂时,我已经替换了在尝试解决路由问题时使用简单的“Hello World”查看。
问题是,当我尝试使用 composer 将其导入 laravel 站点时,我一直找不到 404。我已经运行了 route:clear 和 route:cache,它在 route:list 中显示得很好,并且似乎没有任何动态路径会与之冲突。但无论我尝试什么,我都会得到 404。我该如何调试呢?我不确定在哪里设置断点来弄清楚为什么每次我尝试去 /faq 时它都会生成 404
$ ./artisan route:list
+--------+-----------+----------------------------------+-------------------------------+------------------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+----------------------------------+-------------------------------+------------------------------------------------------------+------------+
| | GET|HEAD | / | generated::QMsNgqDyiKoZlb54 | Closure | web |
| | GET|HEAD | about | about | Closure | web |
| | GET|HEAD | api/user | generated::HZ72XKpVaiuesCMR | Closure | api |
| | | | | | auth:api |
| | GET|HEAD | contact | contact | Closure | web |
| | GET|HEAD | faq | generated::fVFSPm1Ner0Hn4rU | Closure | |
| | GET|HEAD | terms-of-use | terms | Closure | web |
+--------+-----------+----------------------------------+-------------------------------+------------------------------------------------------------+------------+
解决方案
推荐阅读
- c# - 定位项目控制项目在画布上
- angular - 安装角材
- python - 尽管安装了 keras-rl2,但未解决导入“rl.agents”?
- pycaret - pycaret 多类分类返回数字,如何获取标签?
- android - Smack 4.4.2 通过证书建立安全连接
- publish-subscribe - Conversejs 群聊订阅 (ejabberd)
- android - 安排包含开始日期和结束日期的函数的最佳方法是什么?
- tensorflow - 当 Tensorflor 模型应为布尔值时的十进制输出 (1/0)
- python - 在Python中向量化矩阵演算的有效方法?
- shopify - Shopify 订单 API 中的双倍折扣行