Laravel 框架学习笔记(二)

Laravel 框架学习笔记(二)

用了一阵,发现这个框架无敌

四、POST表单

brade中

<form role="form" method="post" action="{{ url('/release/upload') }}">
    {{ csrf_field() }}
    
    <div class="box-body">
        <div class="form-group">
            <label for="cate_title">{{$tname}}label>
            <input class="form-control" name="title">
            <input class="form-control" name="type" style="display:none" value="{{$type}}">
        div>
    div>
    
    
    @if (count($errors) > 0)
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
            <li>{{ $error }}li>
            @endforeach
        ul>
    div>
    @endif @if(!empty(session('info')))
    <p>
        <p>
            <div class="alert alert-danger">
                <ul>
                    <li>{{session('info')}}li>
                ul>
            div>
            @endif
            <div class="box-footer">
                <button type="submit" name="action" value="edit" class="btn btn-primary">提交button>
            div>
form>

控制器中

public function upload(Request $request){
        $table = $request->input('type');
        if($table=='model'){
            $messages = [
                'title.required' => '标题不能为空',
                'title.max' => '标题过长',
            ];
            $rules = [
                'title' => 'required|max:255',
                'file' => 'required',
            ];
            $validator = Validator::make($request->all(),$rules,$messages);
            if ($validator->fails()) {
                return Redirect::back()
                            ->withInput()
                            ->withErrors($validator);
            }else{
                $suc=DB::table($table)->insert(['opus'=>$request->input('title')]);
                return Redirect::back()
                        ->withInput()
                        ->with('info',$suc?"添加成功":"添加失败");
            }
        }
    }

五、返回json数据

$lesson = DB::table('lesson')->get();
return response()->json([
    'code'=> '1',
    'type' => '3DsMax',
    'data' => $lesson
]);

这样就可以返回json数据了


六、上传文件

brade中
此处利用的ajax的异步处理,其实Laravel中貌似不需要这么做,这么做的原因是一开始这块post请求的地址是七牛云的一个存储服务器,现在我们只需要修改一下post请求地址就可以直接拿来用了。

<script>
$.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
    $(document).ready(function(){
        function upload(){
            var formData = new FormData();
            formData.append('img', $('#selectfile')[0].files[0]);

            $.ajax({
                type:"POST",
                url:"{{url('/release/upload')}}",
                dataType:"json",
                mimeType: "multipart/form-data",
                contentType: false,
                processData: false,
                data:formData,
                success:function(data){
                    if(data.code){
                        $("#imgurl").val(data.url);
                        $("#filename").val(data.filename);
                    }else{
                        alert(data.msg);
                    }
                },
                error:function(jqXHR){
                    alert("发生错误:"+jqXHR.status);
                }
            });
        }
        $("#upload").click(function(){
            upload();
        });
    });
script>

控制器中

public function upload(Request $request){
        if(Auth::user()->authority==1||Auth::user()->authority==2||Auth::user()->authority==3){//上传权限
            $file=$request->file('img');//获取post img参数
            $str=$file->store('public');//存储到storage/app/public中
            return response()->json([   //返回给ajaxjson
                'code'=>1,
                'url' => $str,
                'filename' => $file->getClientOriginalName() //获取文件原名
            ]);
        }else{
            return Redirect::back()
                        ->withInput()
                        ->with('info',"添加失败");
        }

    }

你可能感兴趣的:(Laravel,Web开发)