【发布时间】:2019-10-28 12:02:48
【问题描述】:
我试图弄清楚为什么我的创建路由返回 404 错误,因为它只给了我一个 Failed to load resource: 服务器响应状态为 404(未找到)页。
显示、编辑和更新路由功能没有问题,并且视图与 create.blade.php 文件位于同一文件夹中。我的 web.php 似乎可以识别控制器中的 create 函数,并且控制器似乎至少可以从 vs code 的 Follow Link 系统中识别 create.blade.php 文件。我也试过清空刀片文件,只留下一个 div、扩展和部分,但仍然收到 404。
web.php
Route::get('/location/{location}', 'LocationController@show')->name('location.show');
Route::get('/location/create', 'LocationController@create');
Route::post('/location', 'LocationController@store');
Route::get('/location/{location}/edit', 'LocationController@edit')->name('location.edit');
Route::patch('/location/{location}', 'LocationController@update')->name('location.update');
位置.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Location extends Model
{
public function casino() {
return $this->belongsTo(Casino::class);
}
public function duties() {
return $this->hasMany(Duty::class)->orderBy('title', 'ASC');
}
}
LocationController.php
<?php
namespace App\Http\Controllers;
use App\Location;
use Illuminate\Http\Request;
class LocationController extends Controller
{
public function create() {
return view('locations.create');
}
public function store() {
$data = request()->validate([
'title' => 'required',
]);
return redirect("/casino/" . location()->casino()->id);
}
public function edit(Location $location) {
return view('locations.edit', compact('location'));
}
public function update( Request $request, Location $location) {
$data = request()->validate([
'title' => 'required'
]);
$location->title = $request->get('title');
$location->save();
return redirect("/location/{$location->id}");
}
public function show(Location $location) {
return view('locations.show', compact('location'));
}
}
create.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<form action="/location" enctype="multipart/form-data" method="POST">
@csrf
<div class="row">
<div class="col-8 offset-2">
<div class="row pt-4">
<h1>Add New Location</h1>
</div>
<div class="form-group row">
<label for="title" class="col-md-4 col-form-label">Location Title</label>
<input id="title"
type="text"
class="form-control @error('title') is-invalid @enderror"
name="title"
value="{{ old('title') }}"
required autocomplete="title"
autofocus>
@error('title')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
<div class="row pt-4">
<button class="btn btn-primary">Add New Location</button>
</div>
</div>
</div>
</form>
</div>
@endsection
【问题讨论】:
-
您是否尝试清除缓存?
php artisan optimize