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条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改