接口( )

////////////////////控制器
<?php

namespace App\Http\Controllers;

use App\models\registerModel;

// 1 先引入Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Validator;

use Illuminate\Http\Request;

class registerController extends Controller
{

public function index()
{
//
}

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

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
// $params= $request->all();
//// var_dump($params);
// $name=$request->input(‘name‘);
// if (empty($name)){
// //接口三要素
// $arr[‘status‘]=500;
// $arr[‘info‘]=‘用户名不可以为空‘;
// echo json_encode($arr);
// }
//$password=$request->input(‘password‘);
//if (empty($password)){
// $arr[‘status‘]=500;
// $arr[‘info‘]=‘密码不可以为空‘;
// echo json_encode($arr);
//
//}
//$img=$request->img;
//if (empty($img)){
// $arr[‘status‘]=500;
// //提示信息
// $arr[‘info‘]=‘照片不可以为空‘;
// //返回 json
// echo json_encode($arr);
//}
// $res=registerModel::store($name,$password,$img);
$param=$request->all();
// var_dump($param);
// $this->validate()
$validator=Validator::make($param,[
"name"=>‘required‘,
"password"=>"required",
"img"=>"required"
],[
"name.required"=>"用户名不可以为空",
"password.required"=>"密码不可以为空",
"img.required"=>"照片不可以为空",
]);
//print_r($validator);
if ($validator->fails()){
$err=$validator->errors()->first();
$arr[‘status‘]=200;
$arr[‘info‘]=$err;
echo json_encode($arr);
}
$param[‘img‘] = ‘/‘.$request->img->store(‘img‘);
$res=registerModel::store($param);
if ($res){
$arr[‘status‘]=200;
$arr[‘info‘]=‘添加成功‘;
return json_encode($arr);
}else{
$arr[‘status‘]=500;
$arr[‘info‘]=‘添加失败‘;
return json_encode($arr);
}
}

/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}

/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}

//////////////////////////////////////模型
<?php

namespace App\models;

use Illuminate\Database\Eloquent\Model;
use phpDocumentor\Reflection\Types\Self_;

class registerModel extends Model
{
//定义数据表与模型的关系
protected $table=‘register‘;
public $timestamps=false;
public $PrimaryKey=‘id‘;

//添加数据库

public static function store($param){
$obj=new self();
$obj->name=$param[‘name‘];
$obj->password=md5($param[‘password‘]);
$obj->img=$param[‘img‘];
return $obj->save();

}





}







接口( )

上一篇:设计模式之静态工厂方法


下一篇:chrome无法使用独显解决