zoukankan      html  css  js  c++  java
  • 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 AppHttpRequests;
    
    use AppHttpRequestsRequest;
    
    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 AppHttpControllers;
    
    use AppHttpRequestsUserRequest;
    use IlluminateSupportFacadesDB;
    use IlluminateSupportFacadesValidator;
    
    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 AppHttpRequestsUserRequest; – 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 AppHttpRequestsUserRequest
      

      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 AppHttpRequestsUserRequest
    

    It will solve the problem

  • 相关阅读:
    84. Largest Rectangle in Histogram
    881. Boats to Save People
    148. Sort List
    830. Positions of Large Groups
    279. Perfect Squares
    15. 3Sum
    430. Flatten a Multilevel Doubly Linked List
    JS的所有字符串操作都在这里啦
    如何使DIV居中
    最齐全的vue公共函数给你们放出来啦
  • 原文地址:https://www.cnblogs.com/guiyishanren/p/10731240.html
Copyright © 2011-2022 走看看