php学习笔记
1, 自动创建控制器:
php artisan make:controller Photo/PhotoController
资源控制器处理的行为
| 动词 | 路径 | 动作 | 路由名称 |
|---|---|---|---|
| GET | /resource | index | resource.index |
| GET | /resource/create | create | resource.create |
| POST | /resource | store | resource.store |
| GET | /resource/{resource} | show | resource.show |
| GET | /resource/{resource}/edit | edit | resource.edit |
| PUT/PATCH | /resource/{resource} | update | resource.update |
| DELETE | /resource/{resource} | destroy | resource.destroy |
2、php验证的密码自动会用hash之后密码去和数据库做对比。
所以生成密码的时候记得用 Hash::make 生成的密码 。
// use Hash
// Hash::make($pwd);
3、去除csrf验证,接口不能访问的方法:
VerifyCsrfToken.php->
/**
* Determine if the session and input CSRF tokens match.
*
* @param \Illuminate\Http\Request $request
* @return bool
*/
protected function tokensMatch($request)
{
return true;
// $token = $request->input('_token') ?: $request->header('X-CSRF-TOKEN');
//
// if ( ! $token && $header = $request->header('X-XSRF-TOKEN'))
// {
// $token = $this->encrypter->decrypt($header);
// }
//
// return StringUtils::equals($request->session()->token(), $token);
}
3、上传图片
前端:
<RaisedButton label="上传头像" onClick={this.handleUpImg} primary={true}>
<input className="upInput" type="file" ref="upimg">
</input>
</RaisedButton>
handleUpImg = () => {
let finput = this.refs.upimg;
finput.onchange = (() => {
let fl = finput.files[0];
if (fl.size > 2097152) {
finput.value = "";
this._notificationSystem.addNotification({
title: '提示',
message: "请选择不大于2M的图片!",
autoDismiss: 3,
level: 'info',
position: 'tr'
})
return;
}
if (finput.value.match(/\.(jpg|jpeg|JPG|JPEG)(\?.*)?$/)) {
this.setState({imgFile: finput.files[0]});
} else {
finput.value = "";
this._notificationSystem.addNotification({
title: '提示',
message: "请选择正确的图片格式!",
autoDismiss: 3,
level: 'info',
position: 'tr'
})
}
});
finput.click();
}
handleSet = () => {
let staff = this.state.currentStaff;
console.log('-------->save', staff);
let formData = new FormData();
formData.append('headImg', this.state.imgFile);
formData.append('id', staff.id + '');
formData.append('email', staff.email);
formData.append('phone', staff.phone);
managerService.saveStaff(formData).then(res => {
if (res.flag == '1') {
this.handleClose_set();
this._notificationSystem.addNotification({
title: 'Success',
message: '保存成功!',
autoDismiss: 3,
level: 'success',
position: 'tr'
})
}
})
}
php:
$staff = $request::input('staff');
$id = $request::input('id');
$phone = $request::input('phone');
$headimg = $request::file('headImg');
$email = $request::input('email');
$res = null;
$info = "保存成功!";
if (isset($id)) {
$path='photos/upload/';
if ($request::hasFile('headImg')&&$request::file('headImg')->isValid())
{
$clientName = $headimg->getClientOriginalExtension();
$clientName = time().md5('picture').'.'.$clientName;
$headimg->move($path,$clientName);
$imgurl = 'http://'.$_SERVER['HTTP_HOST'].'/public/'.$path.$clientName;
}
else
$imgurl = '';
$res = DB::update('update ac_user set headimg=?,email=?,phone=? where id=?',
[$imgurl, $email, $phone, $id]);
$info = $imgurl;
} else {
$list = DB::select('select *from ac_user where loginname=? or name=?', [$staff['loginName'], $staff['name']]);
if (count($list) > 0) {
$res = false;
$info = "用户名或姓名已存在!";
} else {
$res = DB::insert('insert into ac_user (loginname,name,headimg,email,password) values (?, ?,?,?,?)',
[$staff['loginName'], $staff['name'], "", "", Hash::make('123456')]);
if (!$res)
$info = "保存失败!";
}
}
return \Response::json(['info' => $info, flag => $res]);
4、部署须知:
将laravel项目的文件拷贝到服务器根目录即可。修改config->database.php中的数据库配置:
'host' => env('DB_HOST', 'localhost'),
注:host要配置为:服务器提供的数据库地址。
5、这个一定要收藏: 配置url访问时,一并走具有react路由的index.html
在项目根目录下创建文件 .htaccess
内容为:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [L]
</IfModule>

浙公网安备 33010602011771号