5.1 Laravel中的文件上传
文件系统
Laravel 的文件系统是基于 Frank de Jonge 的 Flysystem 扩展包
提供了简单的接口,可以操作本地端空间、Amazong S3、Rackspace Clound Storage
可以非常简单的切换不同的保存方式,但仍使用相同的api操作
配置文件
config/filesystem.php
添加上传目录配置
‘uploads’=>[
‘driver’=>’local’,
‘root’=>storage_path(‘app/uploads’)
],
配置好的存储目录在
/laravel53/storage/app/public/uploads
在控制器中添加文件上传的代码
在/laravel53/app/Http/Controller/StudentController.php
public function upload(Request $request){
//localhost/laravel53/public/upload
//return‘upload’; 测试控制器权限
if($request->isMethod(‘PSOT’)){
//var_dump($_FILES);
$file=$request->file(‘source’); //source上传文件框的名字
// dd($file);
//文件是否长传成功
if($file->isValid()){
$originalName=$file->getClientOrigialName();
$ext=$file->getClientOriginalExtension();
$type=$file->getClientMimeType();
$realPath=$file->getRealPath();
$file=date(‘Y-m-d-H-i-s’).’-’.uniqid().’.’.$ext;
$bool=Storage::disk(‘uploads’)->put($filename,file_get_contents($realPath));
//var_dump($bool);
}
exit();
}
returnview(‘student.upload’);
}
新增视图文件 upload.blade.php
/laravel53/resources/views/students/upload.blade.php
在这个文件中写一个文件上传的表单,具体内容略
在控制器中新增方法之后必须修改路由文件,新增路由配置
在/laravel53/routers/web.php中新增下面的代码
Router::any(‘upload’,’StudentController@upload’);
如果要把图片传到public/uploads中去,修改filesystem.php
‘uploads’=>[
‘driver’=>’local’,
‘root’=>public_path(‘uploads’)
],
5.2 Laravel中的邮件发送
邮件(SwiftMailer)
Laravel的邮件功能基于热门的SwiftMailer函数库之上,提供了一个简介的api
Laravel为SMTP、Mailgun、Mandrill、Amazon SES、PHP的mail函数、已经sendmail提供了驱动从而允许你快速通过本地或云服务发送邮件
配置
config/mail.php
发送
Mail::raw()
Mail::send()
实战:
修改配置文件
/laravel53/config/mail.php
<?php
‘from’=>[
‘address’=>’json_vip@163.com’,
‘name’=>’墨客网’
]
?>
修改.env文件中配置的发件邮箱信息
MAIL_DRIVER=smtp
MAIL_HOST=smtp.163.com
MAIL_PORT=465
MAIL_USERNAME=json_vip@163.com
MAIL_PASSWROD=test123
MAIL_ENCRYPTION=ssl
在控制器SdudentController.php中添加邮件发送函数mail()
use Mail;
public function mail(){
//发送文本格式的文件
Mail::raw(‘邮件内容 测试’,function($message){
$message->from(‘json_vip@163.com’,’摩客网’);
$message->subject(‘邮件主题 测试’);
$message->to('2563234@qq.com’);
});
//发送html格式的文件
Mail::send(‘students.mail’,[‘name’=>’sean’ ],function($message){
$message->to('2563234@qq.com’);
})
}
在/laravel53/routes/web.php中添加一条路由
<?php
Router::any(‘mail’,’StudentController@mail’);
?>
localhost/laravel53/public/mail
创建html邮件模板
路径/laravle53/resources/views/student/mail.blade.php
<h1>Hello {{$name}}</h1>
5.3 Laravel中的缓存使用
Laravel为各种不同的缓存系统提供一致的api
Laravel 支持各种后端缓存系统,如File、Memcached、Redis
put() add() forever() has get() pull() forget()
配置文件
config/cache.php
实战使用缓存
在控制器中添加方法
StudentController.php
public function cache1(){
Cache::put(‘key1’,’value1’,10);
}
public function cache2(){
Cache::get(‘key1’);
}
public function cache3(){
//$bool=Cache::add(‘key1’,’vlaue1’,10);
//var_dump($bool);
$bool=Cache::add(‘key2’,’vlaue2’,10);
var_dump($bool);
}
public function cache4(){
Cache::forever(‘key3’,’value3’); //永久保存对象到缓存中
}
public function cache5(){
if(Cache::has(‘key1’)){ //判断缓存是否存在
$var=Cache::get(‘key1’);
var_dump($var);
}else{
echo “No”;
}
}
public function cache6(){
$var=Cache::pull(‘key3’); //取出缓存,然后删了该缓存
var_dump($var);
}
public function cache7(){
$var=Cache::forget(‘key1’); //删除缓存,成功返回true,失败返回false
var_dump($var);
}
添加路由:
/laravle53/routes/web.php
Route::get(‘cache1’,’StudentController@mail’);
Route::get(‘cache2’,’StudentController@mail’);
Route::get(‘cache3’,’StudentController@mail’);
Route::get(‘cache4’,’StudentController@mail’);
5.4 Laravel中的错误与日志
Debug模式
HTTP异常
日志
配置:
config.php
进行本地开发时, 应当配置 APP_DEBUG环境变量为true ,,在上线环境,
,这个值应该永远为不false
在控制器StudentController.php中添加方法
public function err(){
//$name=’seran’;
var_dump($name);
returnview(‘student.err’);
}
在routes/web.php中添加一条路由
Route::any(‘error’,’StudentController@error’);
HTTP异常
有些异常描述来自服务器的http错误码
例如, 这可能是一个”页面未找到” ,错误(404),”认证失败错误”(401) 亦或是程序出错造成的500错误
public function error(){
$student=null;
if($student==null){
abort(‘503’); //错误模板路径 /resources/views/errors/503.blade.php
}
return view(‘Student.error’);
}
自定义错误模板:在/resources/views/errors/这个目录下面添加报错模板
日志
Laravel 日志工具基于强大的Monolog库,提供了single、daily、syslog和errorlog日志模式
debug、info、notice、warning、error、critical和alert七个错误级别
日志的配置文件
/laravel53/config/app.php
查找’log’=>
‘log’=>env(‘APP_LOG’,’single’),
‘log_level’=>evn(‘APP_LOG_LEVEL’,’debug’)
在/laravel/.env中添加配置项
APP_LOG=single
最终生成的日志文件位置
/laravel53/storage/logs/laravel.log
日志使用
在控制器StudentController.php中添加方法
info级别的日志
public function log(){
Log::info(‘这是一个info级别的日志’);
echo‘成功写入日志’;
}
warning级别的日志
public function log(){
Log::warning(‘这是一个warning级别的日志’);
echo‘成功写入日志’;
}
error级别的日志
public function log(){
Log::error(‘这是一个数组’,[‘name’=>’seer’,’age’=>’18’]);
echo‘成功写入日志’;
}
模式设置为daily的时候日志是每天生成一个
修改 .env 文件 APP_LOG=daily
5.5 Laravel中的队列应用
介绍
Laravel队列服务,为各种不同的后台队列提供了统一的api
允许推迟耗时任务(例如发送邮件) 的执行,从而大幅提高web请求速度
主要步骤
迁移队列需要的数据表
编写任务类
推送任务到队列
运行队列监听器
处理失败任务
使用
config/queue.php
在/laravel53/.env中配置驱动
QUEUE_DRIVER=database
在命令行中执行创建迁移文件
$>php artisan queue:table
创建队列任务表
生成的迁移文件
/laravel53/database/migrations/2016-09-02_045435_create_jobs_table.php
执行迁移
$>php artisan migrate
生成队列任务表jobs
用命令行去创建一个邮件任务
$>php artisan make:jog SendEmail
生成了一个邮件任务队列
在/laravel53/jobs/SendEmail.php
class SendEmail implements ShouldQueue{
useInteractsWithQueue,Queueable,SerializesModels;
protected$emial;
publicfunction __construct($email){
$this->email=$email;
}
public functionhandle(){
Mail::raw(‘队列测试’,function($message){
$message->to($this->email);
});
}
}
任务写完,把任务推送到队列中 s
在控制器中添加方法
public function queue(){
dispatch(newSendEmail(‘165156@qq.com’));
}
在 /laravle53/routes/web.php添加一个路由:
Route::any(‘queue’,’StudentController@queue’);
就会在数据库中产生一条记录:
下面来执行任务
$>php artisan queue:listen
$>Failed:App\Jobs\SendEmail
排错方法去看错误日志
/laravle53/storage/logs/laravel.log
发现是laravel.failed_jobs表不存在
还有一个原因是554错误码,就是发送邮件太多被服务器拦截了
解决方法,修改邮件发送类
public function handle(){
Log::info(‘已发送邮件’,$this->email);
}
表明队列执行成功
建立队列执行错误日志表
创建迁移文件
执行迁移
$>php artisan queue:failed-table
$>php artisan migrate
这个表示队列执行失败的表 failed_jobs
$>php artisan queue:failed //查询执行失败的任务
$>php artisan queue:retry 1 //重新执行失败的队列任务数字1是失败任务的id
$>php artisan queue:forget 1 //删除执行失败的任务
$>php artisan quequ:flush //删除所有执行失败的任务