[Laravel] 09 - Functional models
Laravel框架下的若干常用功能实现。
- 文件上传
- 邮件发送
- 缓存使用
- 错误日志
- 队列应用
文件上传
一、配置文件
- 功能

- 配置
[config/filesystems.php]
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => 'your-key',
'secret' => 'your-secret',
'region' => 'your-region',
'bucket' => 'your-bucket',
],
],
新添加插入其中:
'uploads' => [
'driver' => 'local',
'root' => storage_path('app/uploads'),
],
二、画个视图
- 添加布局

- 修改布局

- 路由 --> 控制器 --> 视图
[1] 路由
Route::any('upload', 'StudentController@upload');
[2] 控制器:获取 字段 为 "source” 的表单。
if ($request->isMethod('POST') ) {
$file = $request->file('source');
if ($file->isValid() ) {
// 原文件名
$originalName = $file->getClientOrignalNam();
// 扩展名
$ext = $file->getClientOriginalExtension();
// MimeType
$type = $file->getClientMineType();
// 临时绝对路径
$realPath = $file->getRealPath();
$filename = date('Y-m-d-H-i-s) . '-' . uniqid() . '.' . $ext;
$bool = Storage::disk('uploads')->put($filename, file_get_content($realPath));
var_dump(bool);
}
exit;
}
[3] 文件上传位置

表单内容打印出来瞧瞧:【图片信息】

邮件发送
一、配置文件
- 功能

- 配置
[config/mail.php]
smtp默认
'from' => ['address' => null, 'name' => null],
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
[.env]

二、控制器 - 发送邮件
use Mail;
class StudentController extends Controller
{
public function mail()
{
Mail::raw('邮件内容’, function($message) {
}
--------------------------------------------------------------------
Mail::send('student.mail', ['name' => 'sean', 'age' => 18], function($message) {
$message->to('.......@qq.com');
});
}
}
[student/mail.blade.php]
新建并设计一个Html模板。
缓存使用
一、主要方法以及配置文件
put(), add(), forever(), has(), get(), pull(), forget()
配置文件:[config/cache.php]
二、控制器
- Cache::put - 添加后读取缓存
public function cache1()
{
// put()
Cache::put('key1', 'val1', 10); #10min
} public function cache2()
{
// get()
$val = Cache::get('key1');
}
- Cache::add - 添加后读取缓存
public function cache1()
{
// add()
$bool = Cache::add('key1', 'val1', 10); #key1存在则不能添加
} public function cache2()
{
// get()
$val = Cache::get('key1');
}
- Cache::forever - 添加后读取缓存
public function cache1()
{
// add()
$bool = Cache::forever('key3', 'val3');
} public function cache2()
{
// get()
$val = Cache::get('key1');
}
- Cache::has - 键值存在否
public function cache1()
{
if (Cache::has('key1')) {
$val = Cache::get('key');
var_dump($val);
} else {
echo 'No';
}
} public function cache2()
{
// get()
$val = Cache::get('key1');
}
- Cache::pull - 取走数据
public function cache2()
{
// pull()
$val = Cache::pull('key1'); # 取走后值就没了
}
- Cache::forget - 缓存中删除对象
public function cache2()
{
// forget()
$bool = Cache::forget('key1'); # 取走后值就没了
}
- 缓存文件的具体位置

错误与日志
一、知识点
Debug模式,HTTP异常,日志。
二、Debug模式
- 简介

- 配置 [.env]
APP_DEBUG=true
- 设置 [config/app.php]

- 路由 --> 控制器
Route::any('error', 'StudentController@error');
APP_DEBUG=true后,控制器内代码有问题,会出现相对友好不易被攻击的提示信息。

三、HTTP异常
- 简介

其实就是,控制器调用abort,直接返回error.blade的视图。
- 视图
<!DOCTYPE html>
<html>
<head>
<title>Be right back.</title> <style>
html, body {
height: 100%;
} body {
margin: 0;
padding: 0;
width: 100%;
color: #B0BEC5;
display: table;
font-weight: 100;
font-family: 'Lato';
} .container {
text-align: center;
display: table-cell;
vertical-align: middle;
} .content {
text-align: center;
display: inline-block;
} .title {
font-size: 72px;
margin-bottom: 40px;
}
</style>
</head>
<body>
<div class="container">
<div class="content">
<div class="title">Be right back.</div>
</div>
</div>
</body>
</html>
http error 503
- 调用视图:abort()

四、日志
- 简介

- 设置与配置
/*
|--------------------------------------------------------------------------
| Logging Configuration
|--------------------------------------------------------------------------
|
| Here you may configure the log settings for your application. Out of
| the box, Laravel uses the Monolog PHP logging library. This gives
| you a variety of powerful log handlers / formatters to utilize.
|
| Available Settings:"single", "daily", "syslog", "errorlog"
|
*/ 'log' => env('APP_LOG', 'single'),
- 生成日志
public function error()
{
Log::info('这是一个info级别的日志');
}
日志文件

日志内容

数组形式
Log::error('这是一个数组’,['name' => 'sean', 'age' => 18]);
- daily日志
生成带日期标示的日志。

队列
一、简介

配置文件:[config/queue.php]
二、迁移队列需要的数据表
- 设置 QUEUE_DRIVER

- 创建迁移文件
$ php artisan queue:table
有了 <time>_create_jobs_table.php 文件
- 执行迁移
$ php artisan migrate

多了一个jobs表。

三、创建任务类
- 创建 SendEmail.php
$ php artisan make:job SendEmail
文件自动有了类的框架,如下:

- 任务加入队列
通过路由执行:route --> queue(),推送到队列中。
use Mail
public function queue()
{
dispatch(new SendEmail('xxxx@qq.com'));
}
- 运行队列 listener
运行:$ php artisan queue:listen
public function handle()
{
Mail::raw('队列测试‘, function($message) {
$message->to($this->email);
}); Log::info('Email sent.');
}
四、处理失败任务
- 建立失败表的迁移文件
$ php artisan queue:failed-table

- 执行迁移
$ php artisan migrate
迁移成功,数据库中可见到新表。

- 失败了会有记录在数据库中

- 重新执行失败队列
列出失败队列:$ php artisan queue:failed

- 彻底删掉失败队列
列出失败队列:$ php artisan queue:forget 4
列出失败所有队列:$ php artisan queue:flush
[Laravel] 09 - Functional models的更多相关文章
- [Laravel] 11 - WEB API : cache & timer
前言 一.资源 Ref: https://www.imooc.com/video/2870 二.缓存 缓存:静态缓存.Memcache.redis缓存 Ref: [Laravel] 09 - Func ...
- [Laravel] 14 - REST API: Laravel from scratch
前言 一.基础 Ref: Build a REST API with Laravel API resources Goto: [Node.js] 08 - Web Server and REST AP ...
- [Code::Blocks] Install wxWidgets & openCV
The open source, cross platform, free C++ IDE. Code::Blocks is a free C++ IDE built to meet the most ...
- 本人SW知识体系导航 - Programming menu
将感悟心得记于此,重启程序员模式. js, py, c++, java, php 融汇之全栈系列 [Full-stack] 快速上手开发 - React [Full-stack] 状态管理技巧 - R ...
- 优雅的使用 PhpStorm 来开发 Laravel 项目
[目录] Prerequisites plugin installation and configuration 1 Ensure Composer is initialized 2 Install ...
- Laravel 从入门到精通系列教程
转载;https://laravelacademy.org/laravel-tutorial-5_7 适用于 Laravel 5.5.5.6.5.7 版本,本系列教程将围绕一个 LTS 版本,然后采取 ...
- 一步一步学ZedBoard & Zynq(四):基于AXI Lite 总线的从设备IP设计
本帖最后由 xinxincaijq 于 2013-1-9 10:27 编辑 一步一步学ZedBoard & Zynq(四):基于AXI Lite 总线的从设备IP设计 转自博客:http:// ...
- django之ModelBase类及mezzanine的page link类
class ModelBase(type): """ Metaclass for all models. """ def __new__(c ...
- actor concurrency
The hardware we rely on is changing rapidly as ever-faster chips are replaced by ever-increasing num ...
随机推荐
- Structured Streaming教程(3) —— 与Kafka的集成
Structured Streaming最主要的生产环境应用场景就是配合kafka做实时处理,不过在Strucured Streaming中kafka的版本要求相对搞一些,只支持0.10及以上的版本. ...
- JSP_tomcat_mysql_注冊验证用户;
本文出自:http://blog.csdn.net/svitter 资源下载: github: git clone https://github.com/Svtter/JSP-tomcat-mysql ...
- block 相关清单
对Objective-C中Block的追探 李博士
- Markdown 语法手册 - 完整版(下)
6. 引用 语法说明: 引用需要在被引用的文本前加上>符号. 代码: > 这是一个有两段文字的引用, > 无意义的占行文字1. > 无意义的占行文字2. > > 无 ...
- Asp.net 子域共享cookie
最近项目遇到要共享cookie的问题,本来后台保存session用的是Redis来保存数据的.所以只需要2个站点发的ASP.NET_SessionId是相同的就可以,并且它的Domain 是父级域名. ...
- 详细解读Android中的搜索框(三)—— SearchView
本篇讲的是如何用searchView实现搜索框,其实原理和之前的没啥差别,也算是个复习吧. 一.Manifest.xml 这里我用一个activity进行信息的输入和展示,配置方式还是老样子,写一个输 ...
- Global Mapper如何加载在线地图
Global Mapper是一个比较好用的GIS数据处理软件,官网:http://www.bluemarblegeo.com/products/global-mapper.php ,除使用ArcGIS ...
- 为Docker容器设置http代理
以下内容复制自:传送门 ,可以直接去该地址查看. HTTP/HTTPS proxy The Docker daemon uses the HTTP_PROXY, HTTPS_PROXY, and NO ...
- C++的子类与父类强制转换产生的问题
近日,在项目的一个类中如果碰上想要将子类强制转换成父类,然后再调用其父类版本的virtual虚函数. 就会出现gcc编译错误提示:error: ld returned 1 exit status gc ...
- 大数高精度计算库gmp简介
1.编译安装,我用的ubuntu18.04 $sudo apt-get install m4 //默认没安装,gmp用这个 $tar -jvxf gmp-.tar.bz2 //解压 $cd gmp- ...
