laravel5学习之请求控制

php artisan make:request CreateArticleRequest

 

文件内容

<?php namespace App\Http\Requests;



use App\Http\Requests\Request;



class CreateArticleRequest extends Request {



    /**

     * Determine if the user is authorized to make this request.

     *

     * @return bool

     */

    public function authorize()

    {

        return false;  //是否开启请求控制

    }



    /**

     * Get the validation rules that apply to the request.

     *

     * @return array

     */

    public function rules()

    {

        return [

            //具体的权限
      
]; } }

改为如下

<?php namespace App\Http\Requests;



use App\Http\Requests\Request;



class CreateArticleRequest 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 [

            'title'=>'required|min:3',

            'body'=>'required',

            'publish_at'=>'required|date'

        ];

    }



}

在添加文章的方法中

 /*

     * 添加文章

     */



    public function store(Requests\CreateArticleRequest $request){



       /* $title = Request::get('title');

        return $title;*/

      //  $input = Request::all();

      /*$input['publish_at'] = Carbon::now();*/

       /* $article  = new Articles;

        $article->title  = $input['title'];

        $article -> body = $input['body'];*/



//        $article = new Articles(['title'=>]);



       // Articles::create(Request::all());

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



        return redirect('article');



      //  return $input;

    }

在对应的view文件中打出错误的信息

@extends('app')



@section('content')

<h1>write a article</h1>

    {!! Form::open(['url'=>'article']) !!}

        <div class="form-group">

        {!! Form::label('title','Title')  !!}

        {!! Form::text('title',null,['class'=>'form-control','foo'=>'bar'])  !!}

        </div>



        <div class="form-group">

            {!! Form::label('body','Body')  !!}

            {!! Form::textarea('body',null,['class'=>'form-control','foo'=>'bar'])  !!}

        </div>



        <div class="form-group">

            {!! Form::label('publish_at','publish on:') !!}

            {!! Form::input('date','publish_at',date('Y-m-d'),['class'=>'form-control']) !!}

        </div>

        <div class="form-group">

            {!! Form::submit('Add Article',['class'=>'btn btn-primary form-control']) !!}

        </div>





    {!! Form::close() !!}





    @if($errors->any())



       <ul class="alert alert-danger">



           @foreach($errors->all() as $error)

               <li>{{$error}}</li>

            @endforeach

       </ul>



    @endif

@endsection

 

你可能感兴趣的:(laravel)