【问题标题】:Resource routing资源路由
【发布时间】:2021-08-01 06:26:49
【问题描述】:

所以我对 laravel 比较陌生,我正在努力理解它。到目前为止,一切都很好,但我已经被错误困住了一段时间,因此我们将不胜感激。

我正在尝试跟随 youtube 教程(不确定是否允许链接),这就是我想要做的, 我有一个名为 CarsController 在我的控制器文件中的控制器和一个名为 Car 的模型

我已经使用 --resource 标志生成了 CarsController 页面,所以在我的索引函数中我有这段代码。

 public function index()
{
    return view('index'); //Error here, replace with return view (cars.index);
}

在我用于路由的 web.php 页面中,我有以下命令

 use App\Http\Controllers\CarsController; //added by me
 use Illuminate\Support\Facades\Route; //present by default

 Route::resource('/cars', CarsController::class);

据我从文档中了解到,使用资源路由更好,因为我不必为控制器中存在的每个函数编写路由。

另外,我正在尝试查看的页面具有类似的目录层次结构

resources > views > cars > index.blade.php

这是我要访问的文件。抱歉,如果这是一个菜鸟问题,我已经尝试查看文档和谷歌搜索,但不明白我到底做错了什么。

最后,我在访问http://127.0.0.1:8000/ 时收到的错误是基本的 404 错误。 如果我尝试访问http://127.0.0.1:8000/cars 我只是得到索引不是文件你是blade.php 视图是否存在。另外,如果重要的话,我已经删除了 laravel 包含在 views 文件夹中的默认欢迎页面文件。

这是我通过 PHP artisan 命令的路由列表,

编辑:这是我的文档,指的是https://laravel.com/docs/8.x/controllers#resource-controllers

Edit2:我的汽车代码/index.blade.php

@extends('layouts.app')

@section() //error here too, replace with @section('content')
    <div class="m-auto w-4/5 py-24">
        <div class="text-center">
            <h1 class = "text-5xl uppercase bold">
                Cars
            </h1>
        </div>
    </div>
@endsection

如果重要的话,我会使用顺风 CSS。

布局/app.blade.php

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="{{ assset('css/app.css') }}">
    <title>Document</title>
</head>
<body class="bg-gradient-to-r from gray-100 to-gray-200">
    @yield('content')
</body>
</html>

【问题讨论】:

  • 应该是return view('cars.index');
  • 这样做时我仍然收到 404 错误,如果我尝试访问 localhost/cars 则会收到此错误,函数 Illuminate\View\Factory::startSection() 的参数太少,传入的 0第 3 行的 C:\Users\myuser\workspace\cars\storage\framework\views\267dcc3e93ce52fcb9040a9a9185662cceb1eab3.php 和至少 1 个预期(查看:C:\Users\haris\workspace\cars\resources\views\cars\index.php)刀片.php)
  • 您在127.0.0.1:8000/cars 收到此错误?
  • 是的,错误出现在 127.0.0.1:8000/cars,我指的文档就是这个,laravel.com/docs/8.x/controllers#resource-controllers
  • 你能用cars/index.blade.php代码更新你的问题吗

标签: php laravel


【解决方案1】:

您的刀片位于resources &gt; views &gt; cars &gt; index.blade.php, 所以你的 view() 方法将是 view('cars.index')

public function index()
{
    return view('cars.index');
}

【讨论】:

  • 所以我这样做并更换了它,我也以这种方式更改了路线Route::resource('/', CarsController::class); 并且仍然收到附加到原始帖子评论的错误
  • 我接受了这个答案,因为它的格式很好,但我想补充一点,这并没有为我解决这个问题,我也做了php artisan route:cachephp artisan route:clear 以及我的 web.php路线是Route::resource('/', CarsController::class);
  • 我还想指出我的 >cars/index.blade.php 有另一个错误,我忘了说 @esection('content')
【解决方案2】:

在您的方法中,您可以将视图返回为return view('cars.index');,其中索引是汽车文件夹中的 index.blade.php 文件。

【讨论】:

  • 这给出了这个错误,函数 Illuminate\View\Factory::startSection() 的参数太少,在 C:\Users\haris\workspace\cars\storage\framework\views\267dcc3e93ce52fcb9040a9a9185662cceb1eab3 中传递了 0 .php 位于第 3 行,预计至少有 1 个(查看:C:\Users\haris\workspace\cars\resources\views\cars\index.blade.php)
猜你喜欢
  • 2013-01-31
  • 2015-06-09
  • 1970-01-01
  • 2015-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-12
  • 1970-01-01
相关资源
最近更新 更多