fphpr/fastphp

Night 框架。

维护者

详细信息

github.com/fphpr/fastphp

源代码

问题

安装: 21

依赖: 0

建议者: 0

安全: 0

星星: 0

观察者: 0

分支: 0

类型:项目

1.2.12 2020-03-12 19:22 UTC

This package is auto-updated.

Last update: 2024-09-18 20:48:30 UTC


README

PHP Composer

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;
   
  }
}

选择

$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 方法将其与第二个查询联合

$first = DB::table('users')
            ->whereNull('first_name');

$users = DB::table('users')
            ->whereNull('last_name')
            ->union($first)
            ->get();

计数

// ⇒ get count users list
$count_users = DB::table('users')->count(); // return int

求和

// ⇒ get count users list
$score = DB::table('users')->sum(`score`); // return int

连接 / 左连接 / 右连接

// => 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 方法允许您按给定列对查询结果进行排序。

$users = DB::table('users')
                ->orderBy('name', 'desc')
                ->get();

最新 / 最老

latest 和 oldest 方法允许您轻松按日期对结果进行排序。默认情况下,结果将按 created_at 列排序。或者,您也可以传递您希望排序的列名

$user = DB::table('users')
                ->latest()
                ->first();

随机排序

inRandomOrder 方法可用于随机排序查询结果。例如,您可以使用此方法获取一个随机用户

$randomUser = DB::table('users')
                ->inRandomOrder()
                ->first();

分组 / having / 重复

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 方法

$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 方法更新现有记录。update 方法与 insert 方法类似,接受一个包含要更新的列和值的数组的列和值对。您可以使用 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');
  }
}

路径 / 到存储

使用 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(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();