【发布时间】:2019-10-20 18:04:04
【问题描述】:
我对“404 | 未找到”有疑问。路线存在,但我还是有问题。
我已经尝试过:
php artisan route:list
而且路线确实存在。
web.php:
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/p/create', 'PostsController@create');
Route::get('/p/{post}', 'PostsController@show');
Route::post('/p', 'PostsController@store');
Route::get('/profile/{user}', 'ProfilesController@index')->name('profile.show');
Route::get('/profile/{user}/edit', 'ProfilesController@edit')->name('profile.edit'); //this shows the form of edit profile
Route::patch('/profile/{user}', 'ProfilesController@update')->name('profile.update'); //this will actually do the process of updating our profile
ProfilesController.php:
public function edit(User $user)
{
return view('profiles.edit', compact('user'));
}
public function update(User $user){ //some validation
$data =request()->validate([
'title' => 'required',
'description' => 'required',
'url' => 'url', //require the http://
'image' => '',
]);
auth()->user()->profile->update($data); //auth() is a protection. Without this, an external user, for example in incognite, can edit the profile
}
edit.blade.php:
@extends('layouts.app')
@section('content')
<div class="container">
<form action="/profile->{{ $user->id }}" enctype="multipart/form-data" method="POST">
@csrf
@method('PATCH') <!--it's not permitted write method = 'patch', by default it will be get-->
<div class="row">
<div class="col-8 offset-2">
<div class="row">
<h2>Edit Profile</h2>
</div>
<div class="form-group row">
<label for="title" class="col-md-4 col-form-label">Title</label>
<input id="title"
type="text"
class="form-control @error('title') is-invalid @enderror"
name="title"
value="{{ old('title') ?? $user->profile->title }}"
autocomplete="title" autofocus>
@error('title')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
index.blade.php:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-3 p-5">
<img src="https://scontent-fco1-1.cdninstagram.com/vp/1ba68a1705bb9cdd1f7f2ea9ea062810/5D79BFC8/t51.2885-19/s320x320/22709172_932712323559405_7810049005848625152_n.jpg?_nc_ht=scontent-fco1-1.cdninstagram.com" style = "height:150px" class = "rounded-circle">
</div>
<div class="col-9 pt-5">
<div class="d-flex justify-content-between align-items-baseline">
<h1>{{ $user -> username}}</h1>
<a href="/p/create">Add New Post</a>
</div>
<a href="/profile/{{ $user->id }}/edit">Edit Profile</a>
<div class = "d-flex">
<div class = "pr-5"><strong>{{ $user->posts->count() }}</strong> posts</div>
<div class = "pr-5"><strong>23k</strong> followers</div>
<div class = "pr-5"><strong>212</strong> following</div>
</div>
<div class = "pt-4 font-weight-bold">{{ $user->profile->title }}</div>
<div>{{ $user->profile->description }}</div>
<div><a href="#">{{ $user->profile->url }}</a></div>
</div>
</div>
<div class="row pt-5">
@foreach($user->posts as $post)
<div class="col-4 pb-4">
<a href="/p/{{ $post->id }}">
<img src="/storage/{{ $post->image }}" class = "w-100">
</a>
</div>
@endforeach
</div>
</div>
@endsection
错误只是“404 | Not Found”。当我单击编辑按钮时,地址变为:“http://localhost:8000/profile->1”,错误可能就在那里。
【问题讨论】:
-
问题可能出在您在视图中生成链接的方式上
-
这是一个视图问题 - 刀片文件中有什么?
-
@apokryfos 是的,我同意。
-
@Trent 我有两个刀片文件:edit.blade.php 和 index.blade.php 现在我正在用edit.blade.php 的代码编辑帖子,好吗?
-
@eamirgh 错误出现在“localhost:8000/profile->1”
标签: php laravel laravel-5.2