`

TP5.1简单笔记

 
阅读更多

创建项目

composer create-project topthink/think tp5

这里的tp5目录名你可以任意更改,执行完毕后,会在当前目录下的tp5子目录安装最新版本的ThinkPHP,这个目录就是我们后面会经常提到的应用根目录。

 

命令行创建控制器

默认创建一个资源型控制器 

php think make:controller index/Article
Controller created successfully.

默认创建一个空白控制器

php think make:controller index/Article --plain
Controller created successfully.

 

路由分析

路由route.php最优先被加载

第一个参数是定义的路由规则

第二个参数是该路由规则调用的方法或找到的控制中的方法

Route::get('think', function () {
	return 'hello,ThinkPHP5!';
});

自定义路由规则

Route::get('alist', function () {
	return '文章中心列表';
});
自定义第二种路由规则
如果设置了路由器制定方法,就只能通过路由形式访问,不能使用模块/控制器/方法名的方式来访问
Route::get('add', 'index/article/add');

定义资源路由

php think make:controller index/Stu

第一个参数是定义路由规则

第二个参数是找的控制器名称

资源路由会自动根据资源路由的定义方式,生成对应的每一个方法

Route::resource('stu','index/Stu');

 

如果在定义路由的时候需要传参和可以用:定义参数

该参数会自动的被传递给当前路由所调用的方法,作为方法的参数所自动接收

Route::get('hello/:name', 'index/hello');
	return [
];  

 

数据迁移

安装

composer require topthink/think-migration

查看是否安装完成

php think

migrate
  migrate:breakpoint  Manage breakpoints
  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

 

创建表

php think migrate:create User
 编辑database/migrations/时间戳_user.php
    public function change() {
        $table = $this->table('admin', ['id'=>'admin_id']);
        $table
        // ->addColumn('admin_id','integer',array('limit' => 11,'null'=>false,'comment'=>'管理员ID'))
        ->addColumn('admin_username','string',array('limit' => 45,'null'=>false,'default'=>'','comment'=>'用户名'))  
        ->addColumn('admin_password','string',array('limit' => 45,'null'=>false,'default'=>md5('123'),'comment'=>'登录密码'))  
        ->create();
    }
php think migrate:run

 == 20180726051822 User: migrating
 == 20180726051822 User: migrated 0.2040s

All Done. Took 0.4250s

数据库迁移回退

php think migrate:rollback

 == 20180726051822 User: reverting
 == 20180726051822 User: reverted 0.1112s

All Done. Took 0.1437s

多文件迁移回退,是一步一步回退的

 

数据填充

php think seed:create UserSeed

编辑\database\seeds\UserSeed.php

public function run()
    {
        // 使用db()方法定义想哪一个表填充数据,使用insert()方法插入数据
        db('users')->insert(['username'=>'admin','password'=>md5('admin'),'nick'=>'leo']);
        db('users')->insert(['password'=>md5('admin')]);
    }
php think seed:run

 

公共区域父子模板继承分析

创建父模板/view/common/father.html,使用{block}进行区块划分

<body>
	{block name='mid'}中间区块{/block}</br>
	{block name='tj'}推荐区块{/block}
</body>

子模板集成父模板并附加内容

{extend name='./common/father' /}

{block name='mid'}<h1>刘洋</h1>{/block}
{block name='tj'}<h1>leovlys</h1>{/block}

  

模型层分析及创建

php think make:model Users

使用模型的时候要在控制器上加载

use app\common\model\Users;

空白控制器接收页面传参要是加载request

use think\Request;

 

session操作

助手操作use think\facade\Session;

    // 首先向session中添加一个值,
    // Session::set('uid','23');
    // Session::set('username','thinkphp');
    // 用get方法从session获取
    // Session::get('username');
    // 从session中删除某一个参数
    // Session::delete('uid');
    // 从session中删除所有数据
    // Session::clear();
    // 从session中取出某个数据值,然后删除
    // Session::pull('name');
    // 打印所有session
    // halt(Session::get());

session方法操作 use think\Session;

    // 使用session方法存,取,删除session
    // session('name','刘洋');
    // 删除session或者某一个键值
    // session('name',null);
    // 删除session中的所有数据
    // session(null);

 

缓存助手方法操作缓存: use think\facade\Cache;

        // 使用cache类来存储缓存数据
        Cache::set('name','刘洋');
        // 获取缓存数据
        Cache::get('name');
        // 存储一个有效期为5秒的缓存数据
        Cache::set('name','刘洋1',5);
        // 删除cache中的数据
        Cache::rm('name');
        // 取出某个缓存数据然后删除
        Cache::pull('name');

使用缓存方法操作缓存: use think\Cache;

        // 使用cache方法操作缓存
        cache('name','刘洋_方法');
        // 设置缓存有效时间内的缓存
        cache('name','刘洋_方法',5);
        // 获取缓存数据
        cache('name')
        // 删除cache中的数据
        cache('name',null);
        // 取出某个缓存数据然后删除
        cache(null);

 

验证码安装:composer require topthink/think-captcha

<input type="text" name="code">{:captcha_img()}
<img src="/index/reg/code" alt="">

 设置验证码:

 

$config = [
			// 验证码字符集合
			'codeSet'	=>	'1234567890',
		    // 验证码过期时间(s)
		    'expire'	=>	1800, 
		    // 中文验证开启
		    'useZh'	=>	false,
		    // 使用背景图片
		    'useImgBg'	=>	false,
			// 验证码字体大小
		    'fontSize'	=>	30,   
		    // 验证码字体大小(px)
		    'fontSize'	=>	25,
		    // 是否画混淆曲线
		    'useCurve'	=>	true,
		    // 关闭验证码杂点
		    'useNoise'	=>	true, 
		    // 验证码图片高度,设置为0为自动计算
	    	'imageH'	=>	0,
		    // 验证码图片宽度,设置为0为自动计算
		    'imageW'	=>	0,
		    // 验证码位数
		    'length'	=>	4, 
		    // 背景颜色
		    'bg'	=>	[255,231,186],
		    // 验证成功后是否重置
		    'reset'	=>	true,
		];
		$captcha = new Captcha($config);
		return $captcha->entry();

 验证验证码正确性

'code' =>'require|captcha',

 

自定义错误模板:config/app.php

// 异常页面的模板文件
    // 'exception_tmpl'         => Env::get('think_path') . 'tpl/think_exception.tpl',
    'exception_tmpl'         => Env::get('public_path') . 'error/error.tpl',

  

自定义跳转模板:   config/app.php

// 默认跳转页面对应的模板文件
    'dispatch_success_tmpl'  => Env::get('think_path') . 'tpl/dispatch_jump.tpl',
    'dispatch_error_tmpl'    => Env::get('think_path') . 'tpl/dispatch_jump.tpl',

 

模型关联操作

模板:

class Users extends Model
{
//    模型关联方法
//    用户模型管理文章模型
    public function article() {
//        第一个参数是当前模型需要去关联的模型名称,在这里当前模型是users模型
//        第二个参数是外键参数,其实就是被关联模型中和当前模型关联的那个字段名称
//        第三个参数是內键,其实就是当前模型中和被关联模型关联的那个字段名称
        return $this->hasMany('Articles','author','username');
    }
}

  

class Articles extends Model
{
    public function user() {
        return $this->belongsTo('Users','username','author');
    }
}

 控制器:

    public function edit(Request $request) {
    	if ($request->isPost()) {
    		$post = $request->param();
    		$uid = \session('uid');
    		$user = Users::get($uid);
    		$result = $user->article()->save($post);
    		if ($result) {
    			$this->success('修改成功', 'Index/article/index');
    		} else {
    			$this->error('修改失败');
    		}
    	} else {
    		$id = $request->param();
    		$articles = Articles::find($id);
    		// halt($articles);
    		return view('',compact('articles'));
    	}
    }
    public function delete(Request $request) {
        $id = $request->param('id');
        $user = Users::get($id,'article');
        $user->together('article')->delete();
        $user->delete();
        $this->success('删除成功','/index/index/index');
    }

 读取session

前端中:
设置session:  session('name',$admin['name']);
读取session:  {$Request.session.name}
url传session参数的写法:
{:url('admin/edit',array('id'=>$Request.session.id))}

后端:
设置:Session::set('name','thinkphp');
取值:Session::get('name');

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics