【发布时间】: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 文件夹中的默认欢迎页面文件。
编辑:这是我的文档,指的是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代码更新你的问题吗