我在路由器中有代码:
/**************Quản lý user*****************/
Route::get('admin/manage-user', 'UserController@getList')->middleware('admin');
Route::get('admin/manage-user/add', 'UserController@indexAdd')->middleware('admin');
Route::post('admin/manage-user/add', 'UserController@getAdd')->middleware('admin');
UserController中的代码:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use App\Http\Requests\AddUserRequest;
class UserController extends Controller
{
//
public function getList()
{
$data = User::paginate(10);
return view('admin.manage-user',['data' => $data]);
}
public function indexAdd()
{
return view('admin.add-user');
}
public function getAdd(AddUserRequest $request)
{
if($request->fails())
return view('admin.add-user')->withInput();
}
}
AddUserRequest中的代码
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class AddUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'username' => 'required|max:200',
'email' => 'required|email|unique:users',
'pass1' => 'required|min:6',
'pass2' => 'required|same:pass1',
];
}
}
代码视图错误:
@extends('layouts.admin')
@section('title','Add User')
@section('content')
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-6">
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Add User</h3>
</div>
<!-- /.box-header -->
<!-- form start -->
<form role="form" action="{{url('admin/manage-user/add')}}" method="post">
<div class="box-body">
<div class="form-group">
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
</div>
在运行路径:http://localhost/LBlog/public/admin/manage-user/add并提交(不输入表单信息)时,屏幕返回错误:由于不活动,页面已过期。请刷新,然后重试。 我希望有人可以帮助我解决这个问题
答案 0 :(得分:1)
由于CSRF令牌而出现该错误。
在表单中添加csrf令牌。
<form role="form" action="{{url('admin/manage-user/add')}}" method="post">
@csrf
<div class="box-body">
<div class="form-group">
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
</div>