Php 缺少路由的必需参数
我想问一下如何修复这个错误。我发现此错误缺少Php 缺少路由的必需参数,php,laravel,laravel-5,Php,Laravel,Laravel 5,我想问一下如何修复这个错误。我发现此错误缺少[Route:userprofile.edit][URI:userprofile/{userprofile}/edit]所需的参数。(视图:C:\xampp\htdocs\code\task1\resources\views\userprofile\create.blade.php) 我在视图userprofile\create.blade.php中有用户信息,提交按钮后,我想在userprofile\edit.blade.php <input
[Route:userprofile.edit][URI:userprofile/{userprofile}/edit]所需的参数。(视图:C:\xampp\htdocs\code\task1\resources\views\userprofile\create.blade.php)
我在视图userprofile\create.blade.php
中有用户信息,提交按钮后,我想在userprofile\edit.blade.php
<input type="submit" class="btn btn-primary" action="{{ route('userprofile.edit') }}" value="Edit profile">
这是我的userProfileController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
class userProfileController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view("userprofile.create");
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$user = User::find($id);
return view('userprofile.edit')->withUser($user);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
在create.blade.php
<input type="submit" class="btn btn-primary" action="{{ route('userprofile.edit') }}" value="Edit profile">
如果你知道为什么会有这样的错误,请写信给我!
谢谢。代替
<input type="submit" class="btn btn-primary" action="{{ route('userprofile.edit') }}" value="Edit profile">
使用
配置文件ID必须是ID您不能向输入字段添加操作。将操作添加到
表单
标记。您没有在表单操作中传递任何id。将id添加到您的操作中,就像我已通过id=1一样
<form method="post" action="{{ route('userprofile.edit', ['id' => 1]) }}">
更新
首先,您必须使用给定的数据创建一个用户,然后使用生成的id重定向到编辑页面。您需要为传递到
编辑
和其他方法的id设置默认值,如下所示:
public function edit($id = null)
{
$user = User::find($id);
return view('userprofile.edit')->withUser($user);
}
这里,我们将id
的默认值设置为null
。接下来,检查方法是否为null,如果为null,则返回一个错误或在这种情况下要执行的任何操作:
public function edit($id = null)
{
if(!$id)
return back()->with(['error'=>'No document selected for editing.']);
$user = User::find($id);
return view('userprofile.edit')->withUser($user);
}
如果(!$id)
return back()->带有(['error'=>“未选择要编辑的文档”。]);
这是您的检查,如果id为空,我们只需返回并通知用户(您必须在刀片视图中检查错误,如果存在则显示错误).为什么使用url而不是路由?在我更改url并提交按钮后,我有相同的视图,并且在提交按钮后,路由将更改为此视图。在更改路由后,我有相同的视图localhost:8080/code/task1/public/userprofile/create?user\u name=admin%40example.com设置id值时没有问题,该方法为我提供了正确的id,但我无法将页面重新定位到edit.blade.php
public function edit($id = null)
{
if(!$id)
return back()->with(['error'=>'No document selected for editing.']);
$user = User::find($id);
return view('userprofile.edit')->withUser($user);
}