一、路由定义
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'=>'[email protected]','votes'=>0],['email'=>'[email protected]','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');
推荐: 浮生无事的博客