您的位置:首页 > 编程语言 > PHP开发

yii 常用一些调用

2015-09-05 16:58 786 查看


在view中得到当前controller的ID方法:Yii::app()->getController()->id;
  

在view中得到当前action的ID方法:Yii::app()->getController()->getAction()->id;

输出一组url(yii url 默认样式)

</div><div><div><?php $this->widget('zii.widgets.CMenu',array(</div><div>            'items'=>array(</div><div>                array('label'=>'主菜单', 'url'=>array('/admin/left_menu')),</div><div>                array('label'=>'内容发布', 'url'=>array('/admin/page')),</div><div>                array('label'=>'内容维护', 'url'=>array('/site/contact')),</div><div>                array('label'=>'系统主页', 'url'=>array('/site/login')),</div><div>                array('label'=>'网站主页', 'url'=>array('/site/logout')),</div><div>                array('label'=>'会员中心', 'url'=>array('/site/login')),</div><div>                array('label'=>'注销', 'url'=>array('/site/login')),</div><div>            ),</div><div>        )); ?>

</div></div><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; border: 0px; font-size: 13px; font-family: 'Microsoft YaHei', 微软雅黑, Arial, 'Lucida Grande', Tahoma, sans-serif; line-height: 24px;">//除域名外的URL</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; border: 0px; font-size: 13px; font-family: 'Microsoft YaHei', 微软雅黑, Arial, 'Lucida Grande', Tahoma, sans-serif; line-height: 24px;">  Yii::app()->request->getUrl();</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; border: 0px; font-size: 13px; font-family: 'Microsoft YaHei', 微软雅黑, Arial, 'Lucida Grande', Tahoma, sans-serif; line-height: 24px;"> 除域名外的首页地址</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; border: 0px; font-size: 13px; font-family: 'Microsoft YaHei', 微软雅黑, Arial, 'Lucida Grande', Tahoma, sans-serif; line-height: 24px;"> Yii::app()->user->returnUrl;</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; border: 0px; font-size: 13px; font-family: 'Microsoft YaHei', 微软雅黑, Arial, 'Lucida Grande', Tahoma, sans-serif; line-height: 24px;">6、//除域名外的根目录地址
Yii::app()->homeUrl;</p></div>



输出一组url(yii url 默认样式)

<?php $this->widget('zii.widgets.CMenu',array(
            'items'=>array(
                array('label'=>'主菜单', 'url'=>array('/admin/left_menu')),
                array('label'=>'内容发布', 'url'=>array('/admin/page')),
                array('label'=>'内容维护', 'url'=>array('/site/contact')),
                array('label'=>'系统主页', 'url'=>array('/site/login')),
                array('label'=>'网站主页', 'url'=>array('/site/logout')),
 
4000
              array('label'=>'会员中心', 'url'=>array('/site/login')),
                array('label'=>'注销', 'url'=>array('/site/login')),
            ),
        )); ?>

//除域名外的URL

Yii::app()->request->getUrl();

除域名外的首页地址

Yii::app()->user->returnUrl;

6、//除域名外的根目录地址 Yii::app()->homeUrl;
YII
FRAMEWORK的COOKIE使用方法
设置cookie:

$cookie = new CHttpCookie('mycookie','this is my cookie');
$cookie->expire = time()+60*60*24*30;  //有限期30天
Yii::app()->request->cookies['mycookie']=$cookie;
读取cookie:
$cookie = Yii::app()->request->getCookies();
echo $cookie['mycookie']->value;
销毁cookie:

$cookie = Yii::app()->request->getCookies();
unset($cookie[$name]);
在控制器添加CSS文件或JAVASCRIPT文件

public function init()
{
parent::init();
Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl.'/css/my.css');
Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl.'/css/my.js');
}

YII
FRAMEWORK的用户验证与授权

yii提供了CUserIdentity类,这个类一般用于验证用户名和密码的类.继承后我们需要重写其中的authenticate()方法来实现我们自己的验证方法.具体代码如下:

class UserIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
$record=User::model()->findByAttributes(array('username'=>$this->username));
if($record===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($record->password!==md5($this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id=$record->id;
$this->setState('title', $record->title);
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
}
}


在用户登陆时则调用如下代码: 

// 使用提供的用户名和密码登录用户
$identity=new UserIdentity($username,$password);
if($identity->authenticate())
Yii::app()->user->login($identity);
else
echo $identity->errorMessage;

用户退出时,则调用如下代码: 

// 注销当前用户
Yii::app()->user->logout();

其中的user是yii的一个components.需要在protected/config/main.php中定义

'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
'loginUrl' => array('site/login'),
),

YII
FRAMEWORK中TRASACTION事务的应用

$model=Post::model();
$transaction=$model->dbConnection->beginTransaction();
try
{
// find and save are two steps which may be intervened by another request
// we therefore use a transaction to ensure consistency and integrity
$post=$model->findByPk(10);
$post->title='new post title';
$post->save();
$transaction->commit();
}
catch(Exception $e)
{
$transaction->rollBack();
}


Yii Framework中截取字符串(UTF-8)的方法

Helper.php
class Helper extends CController
{
public static function truncate_utf8_string($string, $length, $etc = '...')
{
$result = '';
$string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES, 'UTF-8');
$strlen = strlen($string);
for ($i = 0; (($i < $strlen) && ($length > 0)); $i++)
{
if ($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0'))
{
if ($length < 1.0)
{
break;
}
$result .= substr($string, $i, $number);
$length -= 1.0;
$i += $number - 1;
}
else
{
$result .= substr($string, $i, 1);
$length -= 0.5;
}
}
$result = htmlspecialchars($result, ENT_QUOTES, 'UTF-8');
if ($i < $strlen)
{
$result .= $etc;
}
return $result;
}
}

将Helper.php放进protected\components文件夹下。

使用方法:
Helper::truncate_utf8_string($content,20,false);   //不显示省略号

Helper::truncate_utf8_string($content,20);  //显示省略号 

CBREADCRUMBS简介~俗称:面包屑

功能介绍:zii.widgets 下的CBreadcrumbs类,其继承关系: CBreadcrumbs » CWidget » 
CBaseController » CComponent .源代码位置: 
framework/zii/widgets/CBreadcrumbs.php 
面包屑类显示一个链接列表以表明当前页面在整个网站中的位置.
由于面包屑通常会出现在网站的近乎所有的页面,此插件最好在视图的layout中进行部署.
你可以定义一个breadcrumbs属性并且在布局文件中指派给(网站)基础控制器插件,如下所示:

$this->widget('zii.widgets.CBreadcrumbs', array(
'links'=>$this->breadcrumbs,
));
于是乎,你需要时,只需要在每个视图脚本中,指定breadcrumbs属性(就可以显示出网页导航了).
以上是官方提供的文档文件的介绍.
下面介绍视图文件中写法:

$this->breadcrumbs=array(
'Users'=>array('index'),
'Create',
// 形式 :  'key' =>'value'  key的位置相当于最后显示出来的a标签内的名字, value则相当于a标签的href属性.
// 'Create'表示当前页  故没有设置链接.
);
YII
FRAMEWORK中验证码的使用

1.在controller中修改:
public function actions()
{
return array(
// captcha action renders the CAPTCHA image displayed on the contact page
'captcha'=>array(
'class'=>'CCaptchaAction',
'backColor'=>0xFFFFFF,  //背景颜色
'minLength'=>4,  //最短为4位
'maxLength'=>4,   //是长为4位
'transparent'=>true,  //显示为透明
),
);
}

在view的form表单中添加如下代码:
<?php if(CCaptcha::checkRequirements()): ?>
<div class="row">
<?php echo $form->labelEx($model,'verifyCode'); ?>
<div>
<?php $this->widget('CCaptcha'); ?>
<?php echo $form->textField($model,'verifyCode'); ?>
</div>
<div class="hint">Please enter the letters as they are shown in the image above.
<br/>Letters are not case-sensitive.</div>
<?php echo $form->error($model,'verifyCode'); ?>
</div>
<?php endif; ?>

YII
FRAMEWORK在WEB页面查看SQL语句配置

'components'=>array(
'errorHandler'=>array(
// use 'site/error' action to display errors
'errorAction'=>'site/error',
),
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CFileLogRoute',
'levels'=>'error, warning',
),
// 下面显示页面日志
array(
'class'=>'CWebLogRoute',
'levels'=>'trace',     //级别为trace
'categories'=>'system.db.*' //只显示关于数据库信息,包括数据库连接,数据库执行语句
),
),
),
),



yii 数据save后得到插入id

$post->save();

//得到上次插入的Insert id

$id = $post->attributes['id'];

如此很简单


yii execute后获取insert id

$id = Yii::app()->db->getLastInsertID();


yii获取get,post过来的数据

Yii::app()->request->getParam('id');

yii如何设置时区

可以在config/main.php 里'timeZone'=>'Asia/Chongqing',设定时区.

yii如何将表单验证提示弄成中文的

将main.php里的app配置加上language=>'zh_cn',系统默认的提示就是中文的了,要自定义消息就像楼上说的定义message

yii如何获得上一页的url以返回

Yii::app()->request->urlReferrer;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: