首页 > 解决方案 > 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        |
+--------+-----------+----------------------------------+-------------------------------+------------------------------------------------------------+------------+

标签: phplaravellaravel-routinglaravel-package

解决方案


推荐阅读