YII2中自己写的matlab自定义函数调用怎么调用

翻译自:/wiki/760/yii-2-0-write-use-a-custom-component-in-yii2-0-advanced-template/
简单模版中添加自定义组件:/wiki/747/write-use-a-custom-component-in-yii2-0/
我们实现的是添加一个读取真实IP的组件,下面是详细步骤:
1. 在项目根目录的common目录中新建components目录。
2. 在components目录里新建ReadHttpHeader.php。这个是组件要实现的功能。
namespace common\
use yii\base\C
class ReadHttpHeader extends Component {
function RealIP()
$ip = false;
$seq = array('HTTP_CLIENT_IP',
'HTTP_X_FORWARDED_FOR'
, 'HTTP_X_FORWARDED'
, 'HTTP_X_CLUSTER_CLIENT_IP'
, 'HTTP_FORWARDED_FOR'
, 'HTTP_FORWARDED'
, 'REMOTE_ADDR');
foreach ($seq as $key) {
if (array_key_exists($key, $_SERVER) === true) {
foreach (explode(',', $_SERVER[$key]) as $ip) {
if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
return $ip;
3. 引入组件。打开common/config/main-local.php
添加下面的代码
'components' =& [
'class' =& 'yii\db\Connection',
'dsn' =& 'mysql:host=dbname=yii2_demo',
'username' =& 'root',
'password' =& '',
'charset' =& 'utf8',
'tablePrefix' =& 'au_',
'ReadHttpHeader' =& [
'class' =& 'common\components\ReadHttpHeader'
'mailer' =& [
'class' =& 'yii\swiftmailer\Mailer',
'viewPath' =& '@common/mail',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' =& true,
4. 调用自定义组件。
打开任意一个Controller文件,比如我打开的是backend\controllers\SiteController.php。
在合适的地方调用组件。
public function actionIndex()
//自定义组件
echo Yii::$app-&ReadHttpHeader-&RealIP();
return $this-&render('index');
阅读(...) 评论()Yii CGridView columns调用自定义函数_懒人程序
支付宝赞助帐号:
Yii CGridView columns调用自定义函数
Yii CGridView columns调用自定义函数
第一步:& 设置视图:admin.php
&?php $this-&widget('zii.widgets.grid.CGridView', array(
&& &'id'=&'mz-qingcomment-grid',
&& &'dataProvider'=&$model-&search(),
&& &'filter'=&$model,
&& &'columns'=&array(
&& &&& &'id',
&& &&& &'content',
&&&&&&& array(
&&&&&&&&&&& 'name'=&'type',
&&&&&&&&&&& 'type'=&'raw',
&&&&&&&&&&& //调用controller下面的方法get_type_text,$this就是当前controller。 会自动传入值。
&&&&&&&&&&&&'value'=&array($this,'get_type_text'),&& //调用自定义的函数
&&&&&&&&&&& 'htmlOptions'=&array('width'=&&90px&),&& //设置样式
&&&&&&& ),&& &&&&&
&&&&&& array( 'class'=&'CButtonColumn'& ),
第二步:在Controller控制器中写函数:
&&& //判断推荐的类型&& $data 代表行数据,$row 代表行号。
&&& public function get_type_text($data,$row){
&&&&&&& $type = $data-&
&&&&&&& if( $type == 1 ) {
&&&&&&&&&&& echo '待审核';
&&&&&&& }elseif( $type == 2 ) {
&&&&&&&&&&& echo '已审核';
&&&&&&& }elseif( $type == 3 ) {
&&&&&&&&&&& echo '未通过';
支持键盘 ← →热门搜索:         
Yii使用公共函数
来源:未知
时间: 20:23
作者:xxadmin
[导读] 在网站项目中,没必要把公用的函数写成一个工具类,有时候面向过程其实更方便。 在入口文件index.php里添加 require_once(protected/function.php); 即可对其引用,成为公用的函数集合。 func...
在网站项目中,没必要把公用的函数写成一个工具类,有时候面向过程其实更方便。
在入口文件index.php里添加
require_once('protected/function.php');
即可对其引用,成为公用的函数集合。
function.php如下:
& * This is the shortcut to DIRECTORY_SEPARATOR
defined('DS')&or&define('DS',DIRECTORY_SEPARATOR);
defined('TIME')&or&define('TIME',&time());
defined('MTIME')&or&define('MTIME',&intval(microtime(true)));//返回当前unix时间戳
& * This is the shortcut to Yii::app()
function app()
&return Yii::app();
& * This is the shortcut to Yii::app()-&clientScript
function&cs()
& & &// You could also call the client script instance via Yii::app()-&clientScript
& & &// But this is faster
& & &return Yii::app()-&getClientScript();
& * This is the shortcut to Yii::app()-&user.
function&user()
& & &return&Yii::app()-&getUser();
& * This is the shortcut to Yii::app()-&createUrl()
function&url(&$route&,&$params&=&array&(),&$ampersand&=&'&'&)
& & &return&Yii::app()-&createUrl(&$route&,&$params&,$ampersand&);
& * This is the shortcut to CHtml::encode
/* function h( $text )
& & &return htmlspecialchars( $text ,ENT_QUOTES,Yii::app()-&charset);
& * This is the shortcut to Yii::app()-&request-&baseUrl
& * If the parameter is given, it will be returned and prefixed with the app baseUrl.
function&baseDir(&$url&=null)
& & &//static $baseUrl =
& & &//if ( $baseUrl ===null)
& & &$baseUrl =Yii::app()-&getRequest()-&getBaseUrl();
& & &return $url ===null ? &$baseUrl : &$baseUrl . '/' .ltrim( $url , '/' );
& * Returns the named application parameter.
& * This is the shortcut to Yii::app()-&params[$name].
function&param(&$name&)
& & &return&Yii::app()-&params[&$name&];
& * A useful one that I use in development is the following
& * which dumps the target with syntax highlighting on by default
function&dump(&$target&)
& &return&CVarDumper::dump(&$target&,&10,&true)&;
function&mk_dir($dir,&$mode&=&0777)
&if&(is_dir($dir)&||&@mkdir($dir,$mode))&return&true;
&if&(!mk_dir(dirname($dir),$mode))&return&false;
&return&@mkdir($dir,$mode);
//自定义更多函数...
上一篇: 下一篇:
更多文章推荐
<span style="display: padding-left:5 font-size:12 color:#FF
<span style="display: padding-left:5 font-size:12 color:#FF
<span style="display: padding-left:5 font-size:12 color:#FF
<span style="display: padding-left:5 font-size:12 color:#FF
<span style="display: padding-left:5 font-size:12 color:#FF
<span style="display: padding-left:5 font-size:12 color:#FF
<span style="display: padding-left:5 font-size:12 color:#FF
<span style="display: padding-left:5 font-size:12 color:#FF
<span style="display: padding-left:5 font-size:12 color:#FF
<span style="display: padding-left:5 font-size:12 color:#FF}

我要回帖

更多关于 yii 自定义函数 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信