duankun9280 2017-04-13 05:37
浏览 131
已采纳

如何在Laravel 5.4中验证POST数据?

I am testing an API in Laravel 5.4. I am now in the part of storing a record via API. I am having some issues on how to use Request::input() or Input::get() to validate POST data.

LessonsController.php

    <?php

    namespace App\Http\Controllers;

    use App\Http\Requests;
    use App\Lesson;
    use App\Acme\Transformers\LessonTransformer;
    //use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Input;


use App\Http\Controllers\Controller;

class LessonsController extends ApiController
{

    /**
     * @var Acme\Transformers\LessonTransformer
     */
    protected $lessonTransformer;

    function __construct(LessonTransformer $lessonTransformer)
    {
        $this->lessonTransformer = $lessonTransformer;
       // $this->middleware('sentry.auth')->only('post'); // basic level of protection for creating a lession
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     * If we are using basic authentication, we should be using SSL
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store()
    {

        if( ! Input::get('title') || ! Input::get('body')){
            return $this->setStatusCode(422)->respondWithError('Parameters failed validation for a lesson.');
        }

        Lesson::create($request->all());

        return $this->respondCreated('Lesson successfully created.');

    }

    /**
     * Display the specified resource.
     * 
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $lesson = Lesson::find($id);

        if( ! $lesson) {

            return $this->respondNotFound('Lesson does not exist');


        }

        return $this->respond([

            'data' => $this->lessonTransformer->transform($lesson)

        ]);

    }

}

When I test my code above with POSTMAN using POST request. I am prompted with "{"error":"Parameters failed validation for a lesson.","status_code":422}" . I receive an error when I try to add data or I don't add data.

Do you have any idea how to correct my store() code? Any help is appreciated.

  • 写回答

3条回答 默认 最新

  • duanjiao7440 2017-04-15 07:51
    关注

    The problem with my code above is that it does not have the $request var in the store() method.

    In my code be below, the Request class is now injected into store method.

    public function store(Request $request)
    {
    
        if (! $request->input('title') or ! $request->input('body') )   
        {
             return $this->respondUnprocessableEntity('Parameters failed validation for a lesson.');
        }
    
        Lesson::create($request->all());
    
        return $this->respondCreated('Lesson successfully created.');
    
    }
    

    Note: In postman, select x-www-form-urlencoded in Body

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 电脑误删了手机的照片怎么恢复?
  • ¥15 (标签-python|关键词-char)
  • ¥15 python+selenium,在新增时弹出了一个输入框
  • ¥15 苹果验机结果的api接口哪里有??单次调用1毛钱及以下。
  • ¥20 学生成绩管理系统设计
  • ¥15 来一个cc穿盾脚本开发者
  • ¥15 CST2023安装报错
  • ¥15 使用diffusionbert生成文字 结果是PAD和UNK怎么办
  • ¥15 有人懂怎么做大模型的客服系统吗?卡住了卡住了
  • ¥20 firefly-rk3399上启动卡住了