0
 

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']);

});

asked Aug 30 '17 at 6:53
zahdxj

33
  • 1
    You need to add use statement at top of file for UserRequest! 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 the add() 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

2 Answers

1
 

There are usually 2 reasons you could be having this issue.

  1. You've not added the use statement for the UserRequest.

    At the top of your controller (above the class) add:

    use App\Http\Requests\UserRequest

    assuming that is the correct namespace.

  2. You may need to run composer dump-autoload to 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'=>'UserControl‌​ler@add'])

to:

Route::get('user/add', ['as' => 'user.add', 'uses' => 'UserControl‌​ler@create']);
Route::post('user/add', ['as' => 'user.store', 'uses' => 'UserControl‌​ler@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!

answered Aug 30 '17 at 7:03
Ross Wilson

17.6k23043
  •  
    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 UserRequest to 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
  • 1
    I'm a chinese,my english is poor,It's a little exhausting.... – zahdxj Aug 30 '17 at 9:03
 
0

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

answered Aug 30 '17 at 7:05

laravel UserRequest $request error的更多相关文章

  1. Dedecms自定义sql 出现错误Safe Alert: Request Error step 2!

    Dedecms自定义执行sql: SELECT body FROM dede_addonarticle WHERE aid = (select max(aid) fromdede_addonartic ...

  2. 织梦手机站下一篇变上一篇而且还出错Request Error!

    最新的织梦dedecms程序手机版下一篇变上一篇而且还出错Request Error!,这是因为官方写错了一个地方 打开 /include/arc.archives.class.php 找到 $mli ...

  3. 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 ...

  4. DedeCms中出现Safe Alert: Request Error step 1/2 的解决方法

    dedecms安全警告:Safe Alert: Request Error step 2!不知道大家有没有发现这个现象.只从Dedecms官方公布了之前的版本有严重的漏洞以来,现在在仿站的时候都是采用 ...

  5. 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 ...

  6. laravel 请求request 接收参数

    获取请求输入 获取所有输入值 你可以使用 all 方法以数组格式获取所有输入值: $input = $request->all(); 获取单个输入值 使用一些简单的方法,就可以从 Illumin ...

  7. 解决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 ...

  8. 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 ...

  9. laravel中请求用例$request可用的一些方法小结

    laravel中$request可用的一些方法小结 1,请求方法的获取 $method = $request->method(); 2,检测请求方法 $res = $request->is ...

随机推荐

  1. mysql07---主从复制

    mysql主从复制,replication,(可以一主多从,不可一从多主) 原理: 主从分离,最少2台服务器.主服务器里面的数据,要在从服务器里面都有一份. 把主服务器的所有insert,update ...

  2. HDU2121 Ice_cream’s world II —— 最小树形图 + 不定根 + 超级点

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2121 Ice_cream’s world II Time Limit: 3000/1000 MS (J ...

  3. 【bug】uc浏览器qq浏览器广告过滤

  4. STM32F4 DMA2D_M2M_PFC

    此例程为STM324x9I_EVAL:DCMI_CaptureMode,使用的stm32f4xx_hal_driver, At each camera line event, the line is ...

  5. jquery 3D云

    http://www.jq22.com/jquery-info1325 http://demo.jq22.com/jquery-cloud-141217202931 下载地址: http://www. ...

  6. PCB Genesis SET拼板(圆形板拼板) 实现效果(二)

    越来发现Genesis采用Surface多边形数据结构的重要性了,当撑握了多边形缩放,交集, 差集,并集等算法, 想实现PCB拼板简直轻而易举了;当然借助多边形算法可以开发出更多的PCB实用的工具出来 ...

  7. bzoj 1195: [HNOI2006]最短母串【状压dp】

    我有病吧--明明直接枚举是否匹配就可以非要写hash,然后果然冲突了(--我个非酋居然还敢用hash 设f[s][i]为已选串状态为s并且最后一个串是i,还有预处理出g[i][j]表示最长有长为g[i ...

  8. bzoj 1007: [HNOI2008]水平可见直线【半平面交】

    其实并不算标准半平面交?但是思路差不多 先按照斜率排序,然后用栈维护凸壳,每遇到重斜率或a[i],s[top-1]交点的x轴在s[top],s[top-1]交点左侧,则说明s[top]被a[i],s[ ...

  9. 组合数学练习题(二)——Chemist

    题意: 在一个 n 维无限空间中,一开始原点处有一个细胞.细胞每秒都会增殖,每个原有细胞都会消亡,在与它曼哈顿距离恰为 1的所有位置都会新增一个细胞.求 T 秒后,原点处会有多少细胞,答案 mod10 ...

  10. 正睿多校联盟训练Week6

    并没有参加 Problem A.阿瓦分蛋糕输入文件: cake.in输出文件: cake.out时间限制: 1 second空间限制: 512 megabytes阿瓦为了庆祝自己自己成长为了一只可爱的 ...