laravel UserRequest $request error

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

});

  • 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!

  •  
    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

posted @ 2019-04-18 17:57  归一山人  阅读(315)  评论(0编辑  收藏  举报