Artisan
artisan是laravel中自带的命令行工具的名称
由强大的Symfony Console组件驱动的
提供了一些对应用开发有帮助的命令
查看所有可用的Artisan的命令(list)
php artisan
php artisan list
查看命令的帮助信息(help)
php artisan help migrate
创建控制器
php artisan make:controller StudentController
创建模型
php artisan make:model Student
创建中心件
php artisan make:middleware Activity
文件上传
文件系统
基于Frank de Jonge的Flysystem扩展包
提供了简单的接口,可以操作本地段空间、AmanzonS3、RackspaceCloud Storage
可以非常简单的切换不同的保存方式,但仍使用API操作
配置文件:
config/filesystems.php
'uploads' => [
'driver' => 'local',
'root' => storage_path('app/uploads'),
],
创建控制器
public function upload(Request $request){
$path=$request->img->store('uploads');
if ($request->isMethod('POST')){
// var_dump($_FILES);
$file=$request->file('source');
//文件是否上传成功
if ($file->isValid()){
//取文件名
$originalName=$file->getClientOriginalName();
//获取文件拓展名
$ext=$file->getClientOriginalExtension();
//获取文件类型
$type=$file->getClientMimeType();
//临时绝对路劲
$realPath=$file->getRealPath();
//起名字
$filename=date('Y-m-d-H-i-s'). '-' . uniqid() . '.' .$ext;
//磁盘,返回bool值
$bool=Storage::disk('uploads')->put($filename,file_get_contents($realPath));
var_dump($bool);
}
// dd($file);
exit;
}
return view('student.upload');
}
添加路由
Route::any('upload','Student2Controller@upload');
缓存使用
为各种不同的缓存系统提供一致的API
支持各种常见的后端缓存系统,如File、Memcached和Redis
主要方法
put()
add()
forever()
has()
get()
pull()
foget()
配置
config/cache.php
public function cache1(){
// put()
// Cache::put('key1','val1',10);
// add()
//如果值存在,返回false,不存在,添加成功返回ture
// $bool=Cache::add('key4','val4',10);
// var_dump($bool);
// forever() 永久保存
// Cache::forever('key4','val4');
// has()
if (Cache::has('key1')){
$val=Cache::get('key1');
dd($val);
}else{
echo 'No';
}
}
public function cache2(){
// get()是取
// $val=Cache::get('key3');
// dd($val);
// pull取出来之后删除
// $val=Cache::pull('key3');
// dd($val);
// forget()从缓存中删除对象,删除成功返回ture、
$bool=Cache::forget('key1');
}
错误和日志
Debug模式
配置
config/app.php
进行本地开发时,应该配置APP_DEBUG环境变量为ture,在上线环境,这个值应该永远为false
HTTP异常
有些异常描述来自服务器的HTTP错误码
页面未找到错误(404)
认证失败错误(401)
程序出错造成的错误(500)
日志
基于Monolog库,提供了single、daily、syslog、errorlog日志模式
debug、info、notice、warning、error、critical和alert七个错误级别