码迷,mamicode.com
首页 > 其他好文 > 详细

laravel5学习之请求控制

时间:2015-03-03 18:21:42      阅读:1734      评论:0      收藏:0      [点我收藏+]

标签:

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

 

laravel5学习之请求控制

标签:

原文地址:http://www.cnblogs.com/webclz/p/4311510.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!