我对“ 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 |未找到”。当我单击编辑按钮时,地址变为:“ http://localhost:8000/profile-> 1”,并且错误可能存在。
答案 0 :(得分:2)
首先,问题在于您生成的网址无效。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void strTOvec(string x, vector<string> y) {
for (int i = 0; i < strlen(x.c_str()); i++) {
y.push_back(x[i]);
}
}
void main() {}
将生成/profile->{{ $user->id }}
,但/profile->1
不能是URL的一部分。通常,您可以通过执行->
来解决此问题。
但是我建议您使用Laravel的URL生成器助手来生成URL。
例如:/profile/{{ $user->id }}
应该是:
<form action="/profile->{{ $user->id }}"
提醒一下,这里有3个主要的路线助手:
<form action="{{ route('profile.update', [ 'user' => $user->id ])}}"
url('/relative/path'); // Will generate an absolute URL to your app based on a relative URL
action('Controller@action'); // Will generate a URL which calls the specified controller
route('route.name'); // Will generate a URL based on the named route
中的第二个参数将填写所需的路线参数。
答案 1 :(得分:0)
问题是您尝试访问的网址! 根据您的路线web.php,您应该访问“ http://localhost:8000/profile/1”。