您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 汕尾分类信息网,免费分类信息发布

Yii2.0经常调用的常量、变量、方法、函数

2024/5/15 18:55:53发布25次查看
<?php defined('yii_debug') or define('yii_debug', true); //当在调试模式下,应用会保留更多日志信息,如果抛出异常,会显示详细的错误调用堆栈。因此,调试模式主要适合在开发阶段使用,yii_debug 默认值为 false。 return $this->render('list'); //加载视图模板,一定要return 否则会空白 $this->render('_overview'); //在视图模板中,可以通过这种方式加载视图模板 //#############################请求####################### $request = yii::$app->request; $get = $request->get(); // 等价于: $get = $_get; $id = $request->get('id'); // 等价于: $id = isset($_get['id']) ? $_get['id'] : null; $id = $request->get('id', 1); // 等价于: $id = isset($_get['id']) ? $_get['id'] : 1; $post = $request->post(); // 等价于: $post = $_post; $name = $request->post('name'); // 等价于: $name = isset($_post['name']) ? $_post['name'] : null; $name = $request->post('name', ''); // 等价于: $name = isset($_post['name']) ? $_post['name'] : ''; //#############################请求方式####################### $request = yii::$app->request; if ($request->isajax) { /* 该请求是一个 ajax 请求 */ } if ($request->isget) { /* 请求方法是 get */ } if ($request->ispost) { /* 请求方法是 post */ } if ($request->isput) { /* 请求方法是 put */ } //#############################请求类属性####################### yii::$app->request->url; //结果:/admin/index.php/product?id=100, 此url不包括host info部分。 yii::$app->request->absoluteurl; //结果:http://www.phpxs.com/post/index.php/product?id=100, 包含host infode的整个url。 yii::$app->request->hostinfo; //结果:http://www.phpxs.com, 只有host info部分。 yii::$app->request->pathinfo; //结果:/product, 这个是入口脚本之后,问号之前(查询字符串)的部分。 yii::$app->request->querystring; //结果:id=100,问号之后的部分。 yii::$app->request->baseurl; //结果:/admin, host info之后, 入口脚本之前的部分。 yii::$app->request->scripturl; //结果:/admin/index.php, 没有path info和查询字符串部分。 yii::$app->request->servername; //结果:example.com, url中的host name。 yii::$app->request->serverport; //结果:80, 这是web服务中使用的端口。 yii::$app->request->useragent; //结果:返回 user-agent 头 yii::$app->request->contenttype; //结果:返回 content-type 头的值, content-type 是请求体中mime类型数据。 yii::$app->request->acceptablecontenttypes; //结果:返回用户可接受的内容mime类型。 返回的类型是按照他们的质量得分来排序的。得分最高的类型将被最先返回。 yii::$app->request->acceptablelanguages; //结果:返回用户可接受的语言。 返回的语言是按照他们的偏好层次来排序的。第一个参数代表最优先的语言。 yii::$app->request->getpreferredlanguage();//结果:这个方法通过 yiiwebrequest::acceptablelanguages 在你的应用中所支持的语言列表里进行比较筛选,返回最适合的语言。 //#############################客户端信息####################### yii::$app->request->userhost; yii::$app->request->userip; #############################http头部####################### $headers = yii::$app->response->headers; $headers->add('pragma', 'no-cache'); // 增加一个 pragma 头,已存在的pragma 头不会被覆盖。 $headers->set('pragma', 'no-cache'); // 设置一个pragma 头. 任何已存在的pragma 头都会被丢弃 $values = $headers->remove('pragma'); // 删除pragma 头并返回删除的pragma 头的值到数组 //#############################文件下载####################### yiiwebresponse::sendfile(); //发送一个已存在的文件到客户端 yiiwebresponse::sendcontentasfile(); //发送一个文本字符串作为文件到客户端 yiiwebresponse::sendstreamasfile(); //发送一个已存在的文件流作为文件到客户端 public function actiondownload(){ return yii::$app->response->sendfile('path/to/file.txt'); } //#############################301跳转######################## yii::$app->response->redirect('http://example.com/new', 301)->send(); //#############################状态码######################## yii::$app->response->statuscode = 200; //#############################session使用######################## $session = yii::$app->session; if ($session->isactive) // 检查session是否开启 $session->open(); // 开启session $session->close(); // 关闭session $session->destroy(); // 销毁session中所有已注册的数据 //#########################cookie的使用############# $cookies = yii::$app->request->cookies; $language = $cookies->getvalue('language', 'en'); // 获取名为 "language" cookie 的值,如果不存在,返回默认值"en" // 另一种方式获取名为 "language" cookie 的值 if (($cookie = $cookies->get('language')) !== null) { $language = $cookie->value; } // 可将 $cookies当作数组使用 if (isset($cookies['language'])) { $language = $cookies['language']->value; } // 在要发送的响应中添加一个新的cookie $cookies->add(new yiiwebcookie([ 'name' => 'language', 'value' => 'zh-cn', ])); // 判断是否存在名为"language" 的 cookie if ($cookies->has('language')) if (isset($cookies['language'])) ; //#####################yii2.0 对数据库 查询的一些简单的操作 ##################### class usermodel extends yiidbactiverecord{ public function test(){ self::find()->all(); //此方法返回所有数据; self::findone($id); //此方法返回 主键 id=1 的一条数据(举个例子); self::find()->where(['name' => '小伙儿'])->one(); //此方法返回 ['name' => '小伙儿'] 的一条数据; self::find()->where(['name' => '小伙儿'])->all(); //此方法返回 ['name' => '小伙儿'] 的所有数据; self::find()->orderby('id desc')->all(); //此方法是排序查询; self::findbysql('select * from user')->all(); //此方法是用 sql 语句查询 user 表里面的所有数据; self::findbysql('select * from user')->one(); //此方法是用 sql 语句查询 user 表里面的一条数据; self::find()->andwhere(['sex' => '男', 'age' => '24'])->count('id'); //统计符合条件的总条数; self::find()->one(); //此方法返回一条数据; self::find()->all(); //此方法返回所有数据; self::find()->count(); //此方法返回记录的数量; self::find()->average('age'); //此方法返回指定列的平均值; self::find()->min('age'); //此方法返回指定列的最小值 ; self::find()->max('age'); //此方法返回指定列的最大值 ; self::find()->scalar(); //此方法返回值的第一行第一列的查询结果; self::find()->column(); //此方法返回查询结果中的第一列的值; self::find()->exists(); //此方法返回一个值指示是否包含查询结果的数据行; self::find()->batch(10); //每次取 10 条数据 self::find()->each(10); //每次取 10 条数据, 迭代查询 } }
以上就是yii2.0经常调用的常量、变量、方法、函数的内容。
汕尾分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录