Php 是什么原因造成的;“未定义路线”;此Laravel 8应用程序中存在错误?

Php 是什么原因造成的;“未定义路线”;此Laravel 8应用程序中存在错误?,php,laravel,laravel-8,Php,Laravel,Laravel 8,我正在开发一个需要用户注册和登录的Laravel应用程序 在注册后,用户应该可以向其个人资料中添加更多信息 为此,我做了以下工作: 在routes/web.php中,我有必要的路由,包括更新: 在新创建的Controllers\Dashboard\UserProfileController.php中,我有: <form action="{{ route('dashboard/profile/update') }}" enctype='multipart/form-dat

我正在开发一个需要用户注册和登录的Laravel应用程序

在注册后,用户应该可以向其个人资料中添加更多信息

为此,我做了以下工作:

routes/web.php
中,我有必要的路由,包括更新:

在新创建的
Controllers\Dashboard\UserProfileController.php
中,我有:

<form action="{{ route('dashboard/profile/update') }}" enctype='multipart/form-data' method="post" novalidate>
    {{csrf_field()}}

    <div class="form-group">
        <input type="text" id="first_name" name="first_name" placeholder="First name" class="form-control" value="{{$current_user->first_name}}">
        @if ($errors->has('first_name'))
            <span class="errormsg text-danger">{{ $errors->first('first_name') }}</span>
        @endif
    </div>

    <div class="form-group">
        <input type="text" id="last_name" name="last_name" placeholder="Last name" class="form-control" value="{{$current_user->last_name}}">
        @if ($errors->has('first_name'))
            <span class="errormsg text-danger">{{ $errors->first('last_name') }}</span>
        @endif
    </div>

    <div class="form-group">
        <input type="text" id="email" name="email" placeholder="E-mail address" class="form-control" value="{{$current_user->email}}">
        @if ($errors->has('email'))
            <span class="errormsg text-danger">{{ $errors->first('email') }}</span>
        @endif
    </div>

    <div class="form-group">
        <textarea name="bio" id="bio" class="form-control" cols="30" rows="6">{{$current_user->bio}}</textarea>

        @if ($errors->has('bio'))
            <span class="errormsg text-danger">{{ $errors->first('bio') }}</span>
        @endif
    </div>

    <label for="avatar" class="text-muted">Upload avatar</label>
        
    <div class="form-group d-flex">
            <div class="w-75 pr-1">
                <input type='file' name='avatar' id="avatar" class="form-control border-0 py-0 pl-0 file-upload-btn">
                @if ($errors->has('file'))
                <span class="errormsg text-danger">{{ $errors->first('avatar') }}</span>
                @endif
            </div>

            <div class="w-25">
              <img class="rounded-circle img-thumbnail avatar-preview" src="{{asset('images/avatars/default.png')}}" alt="{{$current_user->first_name}} {{$current_user->first_name}}">
            </div>
    </div>

    <div class="form-group mb-0">
        <input type="submit" name="submit" value='Save' class='btn btn-block btn-primary'>
    </div>
</form>
名称空间应用程序\Http\Controllers\Dashboard; 使用App\Http\Controllers\Controller; 使用\Http\Request; 使用Auth; 使用App\Models\UserProfile

class UserProfileController extends Controller
{
    public function index(UserProfile $user)
    {
        return view('dashboard.userprofile',
            array('current_user' => Auth::user()) 
        );
    }


    public function update(Request $request, $id)
    {
        $id = Auth::user()->id;
        
        $request->validate([
                'first_name' => ['required', 'string', 'max:255'],
                'last_name' => ['required', 'string', 'max:255'],
                'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        ]);

        $current_user = User::find($id);
        $current_user->first_name = $request->get('first_name');
        $current_user->last_name = $request->get('last_name');
        $current_user->email = $request->get('email');
        $current_user->bio = $request->get('bio');
        $current_user->avatar = $request->get('avatar');

        $current_user->update();

        return redirect('/dashboard/profile')->with('success', 'User data updated successfully');
    }
}
在保存表单(
resources\views\dashboard\userprofile.blade.php
)的视图文件中,我有:

我缺少什么?

函数接受一个名称,而不是URL:

所以你应该使用
route('update')
。虽然看到了代码,但您可能没有意识到
->name()
方法应该接受唯一的路由名称。因此,您应该确保没有任何其他名为
'update'
的路由


有些人这样做:
->name('dashboard.profile.update')
。您可以查看是否喜欢此约定。

由于从社区收到的信息,我应用了一个简单的修复程序:

我将
替换为:

<form action="{{ route('update') }}" enctype='multipart/form-data' method="post" novalidate>

Route [dashboard/profile/update] not defined. (View: Path\to\views\dashboard\userprofile.blade.php)
<form action="{{ route('update') }}" enctype='multipart/form-data' method="post" novalidate>