laravel5.1 项目开发常用接口
2016-01-14 21:02
639 查看
一、路由定义
app/Http/routes.php
Route::get();
为多种请求注册路由
注册路由响应所有 HTTP 请求
参数
详见:http://www.golaravel.com/laravel/docs/5.1/routing/#route-parameters
二、项目
1、数据库操作
事务:
DB::transaction(function()
{
DB::table('users')->update(['votes' => 1]);
DB::table('posts')->delete();
});
详见:
http://www.golaravel.com/laravel/docs/5.1/queries/
http://www.golaravel.com/laravel/docs/5.1/database/
Model类
all()
fromJson($value, $asObject = false)
2、view()
详见:http://www.golaravel.com/laravel/docs/5.1/views/
三、日志写入
四、缓存写入
保存对象到缓存中
使用 Carbon 对象配置缓存过期时间
确认对象是否存在
从缓存中取得对象
从缓存中删除对象
推荐:你分享自主开发平台
app/Http/routes.php
Route::get();
e.g.Route::get('foo','Photos\AdminController@method');
Route::post();
Route::put();
Route::delete();
为多种请求注册路由
Route::match();
e.g.
Route::match(['get','post'],'/',function(){return'Hello World';});
注册路由响应所有 HTTP 请求
Route::any();
参数
Route::get('user/{id}',function($id){return'User '.$id;});
详见:http://www.golaravel.com/laravel/docs/5.1/routing/#route-parameters
二、项目
1、数据库操作
useDB;
DB::select('select * from users where id = ?', [1]);
DB::insert('insert into users (id, name) values (?, ?)', [1, 'Dayle']);
DB::update('update users set votes = 100 where name = ?', ['John']);
DB::delete('delete from users');
DB::table('users')->select('name','email')->get();
DB::table('users')->distinct()->get();
DB::table('users')->orderBy('name','desc')->groupBy('count')->having('count','>',100)->get();
DB::table('users')->join('contacts','users.id','=','contacts.user_id')->join('orders','users.id','=','orders.user_id')->select('users.id','contacts.phone','orders.price')->get();聚合方法
$users=DB::table('users')->count();
$price=DB::table('orders')->max('price');
$price=DB::table('orders')->min('price');
$price=DB::table('orders')->avg('price');
$total=DB::table('users')->sum('votes');
DB::table('users')->insert([['email'=>'taylor@example.com','votes'=>0],['email'=>'dayle@example.com','votes'=>0]]);
DB::table('users')->where('id',1)->update(['votes'=>1]);DB::table('users')->increment('votes');DB::table('users')->increment('votes',5);DB::table('users')->decrement('votes');DB::table('users')->decrement('votes',5);
DB::table('users')->increment('votes',1,['name'=>'John']);
DB::table('users')->where('votes', '<', 100)->delete();
DB::table('users')->delete();
DB::table('users')->truncate();
事务:
DB::transaction(function()
{
DB::table('users')->update(['votes' => 1]);
DB::table('posts')->delete();
});
DB::beginTransaction();
DB::rollback();
DB::commit();
详见:
http://www.golaravel.com/laravel/docs/5.1/queries/
http://www.golaravel.com/laravel/docs/5.1/database/
Model类
all()
create(array $attributes = [])
update(array $attributes = [])
destroy($ids)
delete()
update(array $attributes = [])
save(array $options = [])
toJson($options = 0)
jsonSerialize()
toArray()
asJson($value)
fromJson($value, $asObject = false)
2、view()
e.g.
returnview('admin.profile',$data);
returnview('greetings',['name'=>'Victoria']);
$view=view('greeting')->with('name','Victoria');
详见:http://www.golaravel.com/laravel/docs/5.1/views/
三、日志写入
Log::emergency($error);
Log::alert($error);
Log::critical($error);
Log::error($error);
Log::warning($error);
Log::notice($error);
Log::info($error);
Log::debug($error);
四、缓存写入
保存对象到缓存中
Cache::put('key','value',$minutes);
使用 Carbon 对象配置缓存过期时间
$expiresAt=Carbon::now()->addMinutes(10);Cache::put('key','value',$expiresAt);
确认对象是否存在
Cache::has('key')
从缓存中取得对象
$value=Cache::get('key');
从缓存中删除对象
Cache::forget('key');
推荐:你分享自主开发平台
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- workerman结合laravel开发在线聊天应用的示例代码
- 简单对比分析Ruby on Rails 和 Laravel
- PHP+Apache在Windows 9x下的安装和配置
- IIS 6 的 PHP 最佳配置方法
- 安装Apache和PHP的一些补充
- Linux Apache+MySQL+PHP
- 建立Apache+PHP+MySQL数据库驱动的动态网站
- PHP 5.3.0 安装分析心得
- apache 环境下 php 的配置注意事项
- ASP.NET、ASP、PHP、JSP之间有什么区别?
- PHP VBS JS 函数 对照表