laravel UserRequest $request error
laravel5.2,I create a UserRequest.php under Requests directory,but in controller,public function add(UserRequest $request) show error,but use public function add(Request $request) is normal.
UserRequest
namespace App\Http\Requests;
use App\Http\Requests\Request;
class UserRequest extends Request
{
/**
* 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 [
'user_sn' => 'required|unique',
'user_name' => 'required',
'email' => 'required|unique',
'password' => 'required',
];
}
}
UserController
namespace App\Http\Controllers;
use App\Http\Requests\UserRequest;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
class UserController extends Controller
{
public function add(UserRequest $request)
{
if ($request->get('dosubmit')) {
$validator = Validator::make($request->all(), $request
->rules(), $request->messages());
if ($validator->fails()) {
return redirect('user/add')->withErrors($validator)
->withInput();
}
}
$corporation_list = DB::table('corporation')->get();
$department_list = DB::table('department')->get();
return view('user.add', ['corporation_list' => $corporation_list, 'department_list' => $department_list]);
}
}
Route
Route::group(['middleware'],function (){
Route::any('user/add',['as'=>'user.add','uses'=>'UserController@add']);
});
- 1You need to add
usestatement at top of file forUserRequest! Like:use App\Http\Requests\UserRequest;– Hiren Gohel Aug 30 '17 at 6:54 - Is you
add()method use to show the form or process the form submission or both? – Ross Wilson Aug 30 '17 at 7:58 - yes ,both ,is the problem here? – zahdxj Aug 30 '17 at 8:02
- Yes. If you can add the
Routes and theadd()method to your question as well I'll be able to help you out. – Ross Wilson Aug 30 '17 at 8:03 - Route::any('user/add',['as'=>'user.add','uses'=>'UserController@add']) – zahdxj Aug 30 '17 at 8:05
There are usually 2 reasons you could be having this issue.
You've not added the
usestatement for theUserRequest.At the top of your controller (above the
class) add:use App\Http\Requests\UserRequest
assuming that is the correct namespace.
- You may need to run
composer dump-autoloadto make sure the class has been added to the autoloader.
Edit
Firstly, replace the add() method with the following methods:
public function create()
{
$corporation_list = DB::table('corporation')->get();
$department_list = DB::table('department')->get();
return view('user.add', compact('corporation_list', 'department_list'));
}
public function store(UserRequest $request)
{
// If you get to this point the validation will have passed
// Process the request
}
Then change your routes from:
Route::any('user/add',['as'=>'user.add','uses'=>'UserController@add'])
to:
Route::get('user/add', ['as' => 'user.add', 'uses' => 'UserController@create']);
Route::post('user/add', ['as' => 'user.store', 'uses' => 'UserController@store']);
Obviously, feel free to change the as in the Routes to whatever, they should unique though.
Lastly, I would suggest looking at Resource Controllers which is a RESTful approach.
Hope this helps!
- your issue1 I have already use this statement,so I tried issue 2,but problem is still,this problem appears when I use iframe in my project ,when I click the left menu,the right show the "welcome page" not the correct page.but when I use Request $request,the right show the right page – zahdxj Aug 30 '17 at 7:15
- @zahdxj Would you be able to add the code for the
UserRequestto your question? – Ross Wilson Aug 30 '17 at 7:26 - I have posted my UserRequest – zahdxj Aug 30 '17 at 7:51
- thanks a lot,I just study laravel for a few days,you are a patient person... – zahdxj Aug 30 '17 at 8:34
- 1I'm a chinese,my english is poor,It's a little exhausting.... – zahdxj Aug 30 '17 at 9:03
The problem is that you have not identified UserController that you are using UserRequest file
use App\Http\Requests\UserRequest
It will solve the problem
laravel UserRequest $request error的更多相关文章
- Dedecms自定义sql 出现错误Safe Alert: Request Error step 2!
Dedecms自定义执行sql: SELECT body FROM dede_addonarticle WHERE aid = (select max(aid) fromdede_addonartic ...
- 织梦手机站下一篇变上一篇而且还出错Request Error!
最新的织梦dedecms程序手机版下一篇变上一篇而且还出错Request Error!,这是因为官方写错了一个地方 打开 /include/arc.archives.class.php 找到 $mli ...
- docker maven 出错:Failed to execute goal com.spotify:docker-maven-plugin:...: Request error: POST https://192.168.99.100:2376/build?t=
Spring Boot项目构建docker镜像,出错Failed to execute goal com.spotify:docker-maven-plugin:0.4.13:build (defau ...
- DedeCms中出现Safe Alert: Request Error step 1/2 的解决方法
dedecms安全警告:Safe Alert: Request Error step 2!不知道大家有没有发现这个现象.只从Dedecms官方公布了之前的版本有严重的漏洞以来,现在在仿站的时候都是采用 ...
- DHCP request error:Timed out waiting for dhcpcd to start【转】
本文转载自:http://blog.csdn.net/zvivi521/article/details/9166899 [init.svc.dhcpcd_eth0]: [stopped] I/Serv ...
- laravel 请求request 接收参数
获取请求输入 获取所有输入值 你可以使用 all 方法以数组格式获取所有输入值: $input = $request->all(); 获取单个输入值 使用一些简单的方法,就可以从 Illumin ...
- 解决laravel出现Syntax error or access violation: 1055 '***' isn't in GROUP BY
laravel 5.3 以后默认开启 mysql严格模式(strict)在mysql在严格模式下, 并且开启了ONLY_FULL_GROUP_BY的情况下,group by 的字段没有出现在 sele ...
- request error: Connection aborted.', error(113, 'No route to host')
from: https://superuser.com/questions/720851/connection-refused-vs-no-route-to-host/720860 "Con ...
- laravel中请求用例$request可用的一些方法小结
laravel中$request可用的一些方法小结 1,请求方法的获取 $method = $request->method(); 2,检测请求方法 $res = $request->is ...
随机推荐
- 【转】WdatePicker.js的使用方法 帮助文档 使用说明 如何使用
[转]WdatePicker.js的使用方法 帮助文档 使用说明 如何使用 日期控件支持平面显示功能,只要设置一下eCont属性就可以把它当作日历来使用了,无需触发条件,直接显示在页面上 示例2-1 ...
- linux下离线安装svn服务器并配置
一.下载相应的包 subversion-1.8.18.tar.gz 下载地址:http://subversion.apache.orgsqlite-autoconf-3190300.tar.gz ...
- Django之django-redis对数据进行简单缓存
最近公司老大抱怨,产品某部分内容访问速度奇慢无比,由于是之前接手的别人的代码,不太清楚业务的具体逻辑,不过,经过查看,内容为无需实时更新的内容,so 直接上缓存. 什么是缓存? 对于后端来说,要做的 ...
- Python3中 对local和nonlocal 关键字的改善认识(新手向)
nonlocal关键字用来在函数或其他作用域中使用外层(非全局)变量. nonlocal用于声明,修改嵌套作用域(enclosing 作用域,外层非全局作用域)中的变量,如下实例: #!/usr/bi ...
- 用UNIX的cat命令用于给文本加行号 (转载)
转载:http://blog.csdn.net/vicant/article/details/2317721 有时候需要给文本加上行号,用UNIX的cat命令可以进行这项工作. cat命令的用法如下: ...
- bzoj 1742: [Usaco2005 nov]Grazing on the Run 边跑边吃草【区间dp】
挺好的区间dp,状态设计很好玩 一开始按套路设f[i][j],g[i][j]为吃完(i,j)区间站在i/j的最小腐败值,后来发现这样并不能保证最优 实际上是设f[i][j],g[i][j]为从i开始吃 ...
- bzoj 3144 [Hnoi2013]切糕【最小割+dinic】
都说了是'切'糕所以是最小割咯 建图: 每个点向下一层连容量为这个点的val的边,S向第一层连容量为inf的边,最后一层向T连容量为自身val的边,即割断这条边相当于\( f(i,j) \)选择了当前 ...
- mysql 状态查询
select COUNT(case when info.State = '0' then State end ) as daichuliCount, COUNT(case when info.St ...
- P2885 [USACO07NOV]电话线Telephone Wire——Chemist
题目: https://www.luogu.org/problemnew/show/P2885 由于把每一根电线杆增加多少高度不确定,所以很难直接通过某种方法算出答案,考虑动态规划. 状态:f [ i ...
- python之logging模块简单用法
前言: python引入logging模块,用来记录自己想要的信息.print也可以输入日志,但是logging相对print来说更好控制输出在哪个地方.怎么输出以及控制消息级别来过滤掉那些不需要的信 ...

