【问题标题】:Views showing welcome.blade.php or 404 not found未找到显示 welcome.blade.php 或 404 的视图
【发布时间】:2021-04-12 19:52:09
【问题描述】:

web.php

use App\Http\Controllers\PagesController;
use Illuminate\Support\Facades\Route;

Route::get('/', PagesController::class, 'index');
Route::get('/test', PagesController::class, 'test');

PagesController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PagesController extends Controller
{
    public function index()
    {
        return view('invoices.index');
    }

    public function test()
    {
        return view('invoices.test');
    }
}

文件夹结构

views
  invoices
    index.blade.php
    test.blade.php
  layouts
    app.blade.php
  welcome.blade.php

问题

当我运行 php artisan serve 并转到 localhost:8000 时,我看到了 welcome.blade.php 模板,当我转到 localhost:8000/test 时,我得到 404 not found。

我尝试了以下命令,但没有成功。

php artisan cache:clear
php artisan route:cache

【问题讨论】:

  • 因为您发出了/test 路由发布请求,并且您似乎想通过get 请求访问它
  • test必须使用get方法
  • 那是我的错,我尝试使用 post 测试它,但不起作用,就像使用 get 请求一样。我现在编辑了原始代码。
  • 这也没有解释为什么/返回welcome.blade.php,因为它应该返回invoices.index.blade.php 我试图做一个新项目,看看是否会有区别,还是一样的。
  • 看起来在这个端口中打开了另一个项目尝试在另一个端口中运行您当前的项目,例如php artisan --port=8080

标签: laravel laravel-8


【解决方案1】:

Laravel 8 路由结构变化

Route::get('/', [PagesController::class, 'index']);

如果你想写以前的结构,只需在Providers -> RouteServiceProvider.php中添加路由命名空间

Route::middleware('web')
        ->namespace($this->namespace)        // make sure this is present in your code
        ->group(base_path('routes/web.php'));

我希望它会工作!

【讨论】:

    【解决方案2】:

    检查您的路线定义。 它应该是这样的:

    Route::get('/', 'PagesController@index');
    

    Route::get('/', [PagesController::class, 'index']);
    

    你也可以试试:

    Route::view('/', 'invoices.index');
    

    【讨论】:

    • 刚刚尝试了两种解决方案,结果相同,/ 显示welcome.blade.php 视图和/test 显示404 |未找到
    • 也可以试试Route::view('/', 'invoices.index')
    • 试过了,仍然显示welcome.blade.php模板。
    【解决方案3】:

    你可以试试这个

    use Illuminate\Support\Facades\Route;
    use App\Http\Controllers\PagesController;
    
    Route::get('/', [PagesController::class, 'index']);
    Route::get('/test', [PagesController::class, 'test']);
    

    Reference

    【讨论】:

    • 是的,我试过了,因为其他人也建议使用这种语法,但仍然是同样的问题。我的安装可能有问题,因为我以前从未使用过 linux,但我一步一步安装了 ubuntu 和 laravel,它对那个人有用。
    猜你喜欢
    • 2020-12-07
    • 2012-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-04
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多