我已使用$fieldvalues
创建了表单请求。它在php artisan make:request ValidateRegistration
目录下创建了一个ValidateRegistration.php
文件。在此之后,我对注册控制器的App\Http\Requests\
功能进行了更改,即store()
,表示我已更改
发件人
UserController.php
以
public function store(Request $request)
{
// Save the data
User::create(request(['fname','lname','phone','email','password']));
// redirect to home page
return redirect('/registration-success');
}
并在public function store(ValidateRagistration $request)
{
// Save the data
User::create(request(['fname','lname','phone','email','password']));
// redirect to home page
return redirect('/registration-success');
}
文件的顶部添加了use App\Http\Requests\ValidateRagistration;
。但是当我提交表单而没有填写任何内容时,它会显示一个错误UserController.php
修改
添加了Class App\Http\Controllers\ValidateRegistraion does not exist
和UserController.php
个文件。
UserController.php
ValidateRegistration.php
ValidateRegistration.php
<?php
use App\Http\Requests\ValidateRegistration;
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class UserController 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()
{
$title = "Registration";
return view('/registration', compact('title'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(ValidateRegistration $request)
{
//// validate requested data
//$this->validate(request(), [
// 'fname' => 'required',
// 'lname' => 'required',
// 'phone' => 'required|size:10',
// 'email' => 'required',
// 'password' => 'required'
//]);
// Save the data
User::create(request(['fname','lname','phone','email','password']));
// redirect to home page
return redirect('/registration-success');
}
/**
* 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)
{
//
}
/**
* 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)
{
//
}
}
答案 0 :(得分:2)
发现班级名称的差异:
ValidateRagistration
ValidateRegistraion
我猜它应该读 ValidateRegistration ,清理错别字,以后只会混淆事情
答案 1 :(得分:0)
在UserController.php的顶部交换namespace
和use
行的位置,namespace
应该始终是第一个
<?php
namespace App\Http\Controllers;
use App\Http\Requests\ValidateRegistration;
use Illuminate\Http\Request;
use App\User;
和ValidateRegistration.php
位于App \ Http \ Requests目录
答案 2 :(得分:0)
我修改了authorize()函数。它正在返回false
。将其更改为true
。它现在正在运作。
这
public function authorize()
{
return false;
}
要
public function authorize()
{
return true;
}