ThinkPHP5.1.*完全开发手册
1:命令行新建控制器
// 生成index模块的Blog资源控制器
php think make:controller index/Blog
ps:如果不写index模块则在tp5Dome\application\common\controller下生成Blog控制器
2:生成模块
php think build --module test
3:批量生成模块
如果需要批量生成多个模块的目录和文件,需要定义规则文件build.php并放入应用目录下面。
默认的框架的根目录下面自带了一个build.php示例参考文件(把该文件修改后放入应用根目录下面即可),内容如下:
return [
// 生成应用公共文件
'__file__' => ['common.php'],
// 定义demo模块的自动生成 (按照实际定义的文件名生成)
'demo' => [
'__file__' => ['common.php'],
'__dir__' => ['behavior', 'controller', 'model', 'view'],
'controller' => ['Index', 'Test', 'UserType'],
'model' => ['User', 'UserType'],
'view' => ['index/index'],
],
// 其他更多的模块定义
];
我们还可以在应用目录下面自动生成其它的文件和目录,或者增加多个模块的自动生成,例如:
return [
// 定义demo模块的自动生成
'demo' => [
'__file__' => ['tags.php', 'user.php', 'hello.php'],
'__dir__' => ['config', 'controller', 'model', 'view'],
'controller' => ['Index', 'Test', 'UserType'],
'model' => [],
'view' => ['index/index'],
],
// 定义test模块的自动生成
'test'=>[
'__dir__' => ['config','controller','model','widget'],
'controller'=> ['Index','Test','UserType'],
'model' => ['User','UserType'],
'view' => ['index/index','index/test'],
],
];
定义好生成规则文件后,我们在命令行下面输入命令:
php think build
如果看到输出
Successed
则表示自动生成成功
ps:运行php think build时需要把build.php文件放到application文件夹下
4:Controller的跳转及应用
data code | type(数据类型) | |
---|---|---|
success()//成功 | 1 | json |
error()//失败 | 0 | json |
redirect()//重定向,没有成功失败跳转模板 | 1 | |
result()//自由度高 | 可自定义 | typejson,xml |
5:Controller中指定方法进行验证
tp3.2操作示例
action();//获取实际的操作方法名
//ps:如果 request()->action()报错可以试用MODULE_NAME(获取模型名),CONTROLLER_NAME(控制器),ACTION_NAME(方法)
if (in_array($action,$this->needCheckLogin)){
exit('请登录');
}
}
public function index()
{
// return view();
$this->result(['name'=>'xt'],200,'获取信息成功','json',['X-PowerBy'=>'WangTing']);
}
public function test(){
return 'test';
}
public function demo(){
return 'demo';
}
}
tp5前置操作
[
'only'=>'index,test',//only只验证 , 'index,test'需要验证的方法名
// except
],
];
//验证方法,检测用户是否登录
public function checkLogin(){
exit('需要登陆');
}
public function index()
{
// return view();
$this->result(['name'=>'xt'],200,'获取信息成功','json',['X-PowerBy'=>'WangTing']);
}
public function test(){
return 'test';
}
public function demo(){
return 'demo';
}
}
6:数据库迁移工具migration
首先通过 composer 安装
composer require topthink/think-migration
在命令行下运行查看帮助,可以看到新增的命令
php think
migrate
migrate:create Create a new migration创建一个新的迁移
migrate:rollback Rollback the last or to a specific migration回滚最后一个或特定的迁移
migrate:run Migrate the database迁移数据库
migrate:status Show migration status显示迁移状态
optimize
optimize:autoload Optimizes PSR0 and PSR4 packages to be loaded wit
h classmaps too, good for production.
optimize:config Build config and common file cache.
optimize:route Build route cache.
optimize:schema Build database schema cache.
seed
seed:create Create a new database seeder
seed:run Run database seeders
创建迁移类,首字母必须为大写
php think migrate:create Users
可以看到目录下有新文件 .\database\migrations\20161117144043_users.php
/**
* Migrate Up.migrate:run执行的操作
*/
public function up()
{
// create the table
$table = $this->table('users',array('engine'=>'MyISAM'));
$table->addColumn('username', 'string',array('limit' => 15,'default'=>'','comment'=>'用户名,登陆使用'))
->addColumn('password', 'string',array('limit' => 32,'default'=>md5('123456'),'comment'=>'用户密码'))
->addColumn('login_status', 'boolean',array('limit' => 1,'default'=>0,'comment'=>'登陆状态'))
->addColumn('login_code', 'string',array('limit' => 32,'default'=>0,'comment'=>'排他性登陆标识'))
->addColumn('last_login_ip', 'integer',array('limit' => 11,'default'=>0,'comment'=>'最后登录IP'))
->addColumn('last_login_time', 'datetime',array('default'=>0,'comment'=>'最后登录时间'))
->addColumn('is_delete', 'boolean',array('limit' => 1,'default'=>0,'comment'=>'删除状态,1已删除'))
->addIndex(array('username'), array('unique' => true))
->create();
}
/**
* Migrate Down.migrate:rollback执行的操作
*/
public function down()
{
//数据表的删除
$this->dropTable('users');
}
在项目根目录下打开命令行输入
php think migrate:run
ps:回滚命令
php think migrate:rollback -t 0 //回滚所有
php think migrate:rollback //回滚最后一个执行文件
migration操作
表的方法
列的方法
自定义主键名
自定义时间戳字段名
增加软删除 softDelete 字段
参考链接
7:model操作
根目录下命令提示符
创建模型
php think make:model users
关联关系
return $this->hasMany(comment::class,'post_id'); //一对多
return $this->belongsTo(Post::class,'post_id'); //反向关联
return $this->belongsToMany(tage::class,'posts_tags','post_id'); //多对多,注意有中间关联表
return $this->belongsToMany(Post::class,'posts_tags','tag_id'); //多对多,注意有中间关联表
示例
hasMany(comment::class,'post_id');
}
//关联标签,多对多
public function tags(){
return $this->belongsToMany(tage::class,'posts_tags','post_id');
}
}