PHP学习笔记
一、调用控制器
1、调用当前前台index模块的User控制器 代码如下(示例):
$model = new \app\index\controller\User;
$model->index();
use \app\index\controller\User;
$model = new User;
$model->index();
$model = controller('User');
$model->index();
2、跨模块调用控制器 在当前前台index模块调用后台admin模块控制器 代码如下(示例):
$model = new \app\admin\controller\Index;
$model->index();
use \app\admin\controller\Index as AdminIndex;
$model = new AdminIndex;
$model->index();
$model = controller('admin/Index');
$model->index();
二、调用方法
1.调用当前Index控制器中的test()方法 代码如下(示例):
$this->test();
self::test();
Index::test();
action('test');
2.调用其他控制器的方法 代码如下(示例):
$model = new \app\index\controller\User;
$model->index();
action('User/index');
3.调用后台模块控制器的方法 代码如下(示例):
$model = new \app\admin\controller\Index;
$model->index();
action('admin/Index/index');
三、数据模型
1.模型创建 代码如下(示例):
$res=User::get(1);
dump($res->toArray());
$user = new User();
$res = $user::get(2);
dump($res->toArray());
$user = Loader::model("user");
$res = $user::get(2);
dump($res->toArray());
$user = model("user");
$res = $user::get(2);
dump($res->toArray());
2.模型查询 代码如下(示例):
$user = User::get(1);
echo $user->name;
$user = User::get(['name' => 'thinkphp']);
$user = User::get(function($query){
$query->where('name', 'thinkphp');
});
$user = new User();
$user->where('name', 'thinkphp')
->find();
|