安装
应用项目:https://github.com/top-think/think
核心框架:https://github.com/top-think/framework
核心框架重命名为thinkphp放入应用项目跟目录
public下的.htaccess
修改RewriteRule ^(.*)$ index.php\$1 [QSA,PT,L]
为RewriteRule ^(.*)$ index.php?$1 [QSA,PT,L]
application下的route.php
use think\Route;
Route::get('banner/:id','api/v1.Banner/getBanner');
控制器中获取参数
//第一种
public function hello($id)
{
return $id;
}
//第二种
use think\Request;
public function hello()
{
$all = Request::instance()->param();
$id = Request::instance()->param('id');
return $id;
}
//第三种
public function hello()
{
$all = input('param.');
$id = input('param.id');
$id = input('get.id');
$id = input('post.id');
return $id;
}
//第四种
public function hello(Request $request)
{
$all = $request->param();
}
独立验证
use think\Request;
public function getBanner()
{
$data = [
'name' => 'vendor',
'email' => '123163.com'
];
$validate = new Validate([
'name' => 'require|max:10',
'email' => 'email'
]);
$res = $validate->batch()->check($data);
if (!$res) {
var_dump($validate->getError());
}
}
验证器
api/validate/TestValidate.php
<?php
namespace app\api\validate;
use think\Validate;
class TestValidate extends Validate
{
protected $rule = [
'name' => 'require|max:10',
'email' => 'email'
];
}
使用
public function getBanner()
{
$data = [
'name' => 'vendor',
'email' => '123163.com'
];
// $validate = new Validate([
// 'name' => 'require|max:10',
// 'email' => 'email'
// ]);
$validate = new TestValidate();
$res = $validate->batch()->check($data);
if (!$res) {
var_dump($validate->getError());
}
}
网友评论