parsgit / night
Night 框架。
Requires
- php: ^7.0.0
README
Night 框架
一个简单轻量级的框架,让您获得更多速度。
安装
composer create-project parsgit/night blog
基础知识
输入值
// input all params $all = input(); // sample $name = input('name'); $name = input('name','default_value_if_not_exists');
// get Requests $name = get('name','Default value'); // post Requests $name = post('name'); $age = post('age',18);
路径助手
root_path(); // root project directory path public_path(); // public directory path app_path(); // app directory path storage_path(); // storage directory path views_path(); // views directory path
night 框架在路径助手中的示例使用
$app_path=app_path('Storage/text.txt'); echo $app_path; //output sample : var/www/site/html/app/Storage/text.txt
URL 助手
url
获取和使用 URL 地址
url();// yoursite.com url('account/login'); // yoursite.com/account/login
route_url / params_url
获取路由地址 例如我们在浏览器中输入 http://yoursite.com/account/login
地址,输出将如下所示
$route=route_url(); //output: account/login $route=params_url(); // output array:['account','login']
方法 / isGet / isPost
//Returns the REQUEST METHOD type $request = method(); // POST or GET //If the REQUEST METHOD of the get type returns true if(isGet()){ // code } //If the REQUEST METHOD of the post type returns true if(isPost()){ // post }
基本控制器
pageController.php
<?php namespace Controllers; class pageController{ // yoursite.com/page function Action(){ return view('html_name'); } // yoursite.com/page/id function idAction(){ $id=get('id',0); return $id; } }
数据库
开始使用
正常使用查询示例
$users=DB::select('select * from users where active=?',[true]); DB::insert('insert into users (name,age,username,password) values (?,?,?,?)', [ 'ben', 25, 'ben25', Hash::create('123456') ]); DB::update('...'); DB::delete('...');
使用多个数据库连接
当使用多个连接时,您可以通过 DB 门面的 in
方法访问每个连接。
//DB::in(database_config_name)
$pages = DB::in('blog')->select('select * from pages');
$users = DB::in('site')->select('select * from users');
//or in query builder
$count=DB::in('site')->table('users')->count();
查询构建器
night 框架数据库查询构建器提供了一个方便的、流畅的接口来创建和运行数据库查询。
<?php namespace Controllers; use App\Web\DB; class pageController{ // yoursite.com/page/get-list function get_listAction(){ $pages=DB::table('page')->get(); return $pages; } }
select
$user = DB::table('users')->where('name', 'John')->first(); echo $user->name; // ⇒ find user by id field $user = DB::table('users')->find(20); //support other functions: // where / orWhere // whereBetween / orWhereBetween // whereNotBetween / orWhereNotBetween // whereIn / whereNotIn / orWhereIn / orWhereNotIn // whereNull / whereNotNull / orWhereNull / orWhereNotNull // whereDate / whereMonth / whereDay / whereYear / whereTime // whereColumn / orWhereColumn
union
查询构建器还提供了一种快速将两个查询“联合”起来的方法。例如,您可以先创建一个初始查询,然后使用 union 方法将其与第二个查询联合
$first = DB::table('users') ->whereNull('first_name'); $users = DB::table('users') ->whereNull('last_name') ->union($first) ->get();
count
// ⇒ get count users list $count_users = DB::table('users')->count(); // return int
sum
// ⇒ get count users list $score = DB::table('users')->sum(`score`); // return int
join / leftJoin / rightJoin
// => join $users = DB::table('users') ->join('car', 'users.id=car.user_id') ->get(); // => left Join $users = DB::table('users') ->leftJoin('posts', 'users.id=posts.user_id') ->get(); // right join $users = DB::table('users') ->rightJoin('posts', 'users.id=posts.user_id') ->get(); // full Join $users = DB::table('users') ->fullJoin('posts', 'users.id=posts.user_id') ->get();
orderBy
orderBy
方法允许您按给定列对查询结果进行排序。
$users = DB::table('users') ->orderBy('name', 'desc') ->get();
latest / oldest
latest
和 oldest
方法允许您轻松按日期排序结果。默认情况下,结果将按 created_at
列排序。或者,您也可以传递您希望排序的列名
$user = DB::table('users') ->latest() ->first();
inRandomOrder
inRandomOrder
方法可用于随机排序查询结果。例如,您可以使用此方法获取随机用户
$randomUser = DB::table('users') ->inRandomOrder() ->first();
groupBy / having / duplicate
groupBy
和 having
方法可用于对查询结果进行分组。having
方法的签名与 where
方法类似
$users = DB::table('users') ->groupBy('account_id') ->having('account_id', '>', 100) ->get();
您可以向 groupBy
方法传递多个参数以按多个列进行分组
$users = DB::table('users') ->groupBy('first_name', 'status') ->having('account_id', '>', 100) ->get();
要查找包含重复信息字段。以下代码将找到电话号码重复的用户
$users = DB::table('users') ->duplicate('phone', 2) ->get();
skip / take
要限制查询返回的结果数量,或要跳过查询中的给定数量结果,您可以使用 skip
和 take
方法
$users = DB::table('users')->skip(10)->take(5)->get();
或者,您也可以使用 limit
和 offset
方法
$users = DB::table('users') ->offset(10) ->limit(5) ->get();
插入
查询构建器还提供了一个 insert
方法,用于将记录插入数据库表。insert
方法接受一个包含列名和值的数组
DB::table('users')->insert( ['email' => 'john@example.com', 'votes' => 0] );
更新
除了将记录插入数据库外,查询构建器还可以使用 update
方法更新现有记录。与 insert
方法一样,update
方法接受一个包含要更新的列和值的数组的数组。您可以使用 where
子句约束 update
查询
DB::table('users') ->where('id', 1) ->update(['votes' => 1]);
查询构建器还提供了一些方便的方法来递增或递减给定列的值。这是一个快捷方式,提供了一种比手动编写 update
语句更易于表达和简练的接口。
这两个方法至少接受一个参数:要修改的列。您可以可选地传递第二个参数来控制列应递增或递减的量
DB::table('users')->increment('votes'); DB::table('users')->increment('votes', 5); DB::table('users')->decrement('votes'); DB::table('users')->decrement('votes', 5);
删除
查询构建器也可以通过delete
方法从表中删除记录。您可以通过在调用delete
方法之前添加where
子句来限制delete
语句。
DB::table('users')->delete(); DB::table('users')->where('votes', '>', 100)->delete();
如果您想截断整个表,这将删除所有行并将自动递增的ID重置为零,您可以使用truncate
方法。
DB::table('users')->truncate();
文件
上传
<?php namespace Controllers; use App\Web\File; class fileController{ // yoursite.com/file/upload function uploadAction(){ $upload=File::upload('myfile'); } }
路径 / toStorage
使用path
方法保存文件路径。
$upload->path(public_path('imge'));
或存储路径
$upload->toStorage('image/products');
maxSize / limit_ext / limit_type
maxSize(int 数量)
maxSize
方法用于限制文件大小。
$upload->maxSize(250); // limit max size 250 kb $upload->maxSize(3 * 1023); // limit max size 3 mg
limit_ext(扩展名数组)
通过limit_ext
方法限制文件的扩展名。
$upload->limit_ext(['jpg','png','gif']);
limit_type(扩展名数组)
通过limit_type
方法限制文件的MIME类型。
$upload->limit_type(['image/jpage']);
rename / randomName
rename(string 文件名)
rename
方法用于以自定义名称保存文件。
$uplaod->rename('my_file_name'); // or change custom file extension $uplaod->rename('my_file_name','pngo');//my_file_name.pngo
randomName()
randomName
方法为文件创建一个唯一名称。名称包含10个随机数字和秒的时间样本名称:f_25813_38893_158593089589.png
$upload->randomName();
保存
save
方法用于将文件保存到服务器。
$upload->save();
代码示例
<?php namespace Controllers; use App\Web\File; class fileController{ // yoursite.com/file/upload function uploadAction(){ $upload=File::upload('myfile') ->limit_ext(['png','jpg']) ->maxSize(1024) //1 mg ->toStorage('image') //Storage/image directory ->save(); // save file if($upload->status()==true){ // Upload Is Done :) return['upload'=>true,'name'=>$upload->getFileName()]; } else{ // Upload Is Failed :( $errors=$upload->getErrors(); return['upload'=>false,'errors'=>$errors]; } } }
状态
status
方法用于检查文件上传状态,如果上传正确完成则返回true
,否则返回false
。
if($upload->status()==true){ //Code ... }
getErrors
获取错误数组。
$upload->getErrors();