【问题标题】:Laravel 404 Not FoundLaravel 404 未找到
【发布时间】: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


【解决方案1】:

问题是您要访问的网址! 根据您的路线 web.php,您应该访问“http://localhost:8000/profile/1”。

【讨论】:

  • 是的,我也这么认为。但是我如何访问该地址并进行更新呢?
  • @CasellaJr 关注 apokryfos 的答案,这是一个更完整的答案。
  • 是的,我明白了。谢谢你。我评论了他的回答,你能帮帮我吗?
  • @CasellaJr 随时使用@eamirgh id 在电报上提问。
【解决方案2】:

首先问题是您生成的 URL 无效。

/profile-&gt;{{ $user-&gt;id }} 将生成 /profile-&gt;1,但 -&gt; 不能是 URL 的一部分。通常你可以通过 /profile/{{ $user-&gt;id }} 来解决这个问题。

不过我建议你应该使用 Laravel 的 URL 生成器助手来生成 URL。

例如:&lt;form action="/profile-&gt;{{ $user-&gt;id }}" 应该是:

<form action="{{ route('profile.update', [ 'user' => $user->id ])}}"

提醒一下,有 3 个主要的路线助手:

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

route中的第二个参数会填写需要的路由参数。

【讨论】:

  • 谢谢,问题是->
  • 此时,当我单击更新配置文件的按钮时,我得到一个空白页面。我必须点击充值页面的按钮才能获取“localhost:8000/profile/1”你知道为什么吗?
  • @CasellaJr 我认为我使用了错误的路线名称(profile.edit 而不是profile.update)。尝试更新
  • 不,我只将 profile-> 更改为 profile/ 因为我从未使用过第二种方法。将来我会尝试的。但在这种情况下,我不知道为什么我必须刷新页面
  • 您的update 方法似乎已更新,但未向用户提供任何反馈。通常,您会在该方法的末尾添加 return redirect()-&gt;to('/profile/'.$user-&gt;id); 以重定向回编辑页面。
猜你喜欢
  • 2021-03-21
  • 2021-02-27
  • 1970-01-01
  • 1970-01-01
  • 2021-08-06
  • 1970-01-01
  • 2020-10-15
  • 2020-03-01
  • 2019-02-01
相关资源
最近更新 更多