[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 ...
随机推荐
- 使用HttpClient实现并发请求
在.Net 4.0之前,一直是依靠HttpWebRequest实现Http操作的.它默认有一个非常保守的同一站点下最大2并发数限制,导致默认情况下HttpWebRequest往往得不到理想的速度,必须 ...
- 游戏保护大放送之GPK
GPK也没有啥特别.龙之谷多开检测和别的不一样. #include "struct.h" #include "FGPK.h" ///////////////// ...
- Sql Server中sql语句自己主动换行
怎么让sql server中的sql语句自己主动换行呢? 例如以下图: 工具--选项--全部语言 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvamlhbm ...
- MySQL报1130错误解决办法
update user set password_expired='N' where host = '127.0.0.1'; update user set password=password('ro ...
- C#中使用log4net框架做日志输出
一.用法 1.引入包:https://www.nuget.org/packages/log4net/ 2.Main函数 using System; using System.IO; using log ...
- Spark机器学习(7):KMenas算法
KMenas算法比较简单,不详细介绍了,直接上代码. import org.apache.log4j.{Level, Logger} import org.apache.spark.{SparkCon ...
- pandas基础用法——索引
# -*- coding: utf-8 -*- # Time : 2016/11/28 15:14 # Author : XiaoDeng # version : python3.5 # Softwa ...
- 基于CentOS搭建基于 ZIPKIN 的数据追踪系统
系统要求:CentOS 7.2 64 位操作系统 配置 Java 环境 安装 JDK Zipkin 使用 Java8 -openjdk* -y 安装完成后,查看是否安装成功: java -versio ...
- 二值化函数cvThreshold()参数CV_THRESH_OTSU的疑惑【转】
查看OpenCV文档cvThreshold(),在二值化函数cvThreshold(const CvArr* src, CvArr* dst, double threshold, double max ...
- sql server 2008 express 安装的时提示“重启计算机失败"
sql server 2008 express 安装的时提示"重启计算机失败" 解决办法: 打开注册表编辑器(regedit.exe),在HKEY_LOCAL_MACHINE\SY ...