Yii中开创和煦的Widget实例

本文实例叙述了yii中widget的用法。分享给大家供大家参考。具体深入分析如下:

正文实例陈述了Yii中开创协调的Widget实现情势。分享给我们供大家参照他事他说加以考察,具体如下:

正文实例陈说了YII
Framework框架使用YIIC急迅创造YII应用的主意。分享给我们供大家参考,具体如下:

WIDGET定义很简短,如若您在/protected/widget/test/下创制了八个名称叫“testWidget.php”的widget。

那边以三个随便广告图片为例表明Yii中Widget的用法

yii提供了强劲的命令行工具来快捷的创制连锁组件和平运动用。</span> 

在视图中调用必要这么写:

1. 调用Widget

cd步向yii/framework框架的根目录。

复制代码 代码如下:

复制代码 代码如下:

执行

<?php
$this->beginWidget(‘application.widget.test.testWidget’);
?>
//…大概小部件要求用到此处择要内容…
<?php
$this->endWidget();
?>

<?php $this->widget(‘WidgetName’); ?>

# php yiic
Yii command runner (based on Yii v1.1.8)
Usage: yiic <command-name> [parameters...]

The following commands are available:
 - message
 - migrate
 - shell
 - webapp

To see individual command help, use the following:
  yiic help <command-name>

testWidget.php文件的定义格局:

或者

能够见见

复制代码 代码如下:

<?php $widget=$this->beginWidget('path.to.WidgetClass'); ?>
...可能会由小物件获取的内容主体...
<?php $this->endWidget(); ?>

yiic的为主命令参数准绳

/**
* test widget
*/
class testWidget extends CWidget
{   
    public function init()
    {
        //当视图中实施$this->beginWidget()时候会实行那么些点子
        //能够在此地实行查询数据操作
    }
 
    public function run()
    {
        //当视图中施行$this->endWidget()的时候会试行这一个办法
       
//能够在此处开始展览渲染试图的操作,注意这里涉及的视图是widget的视图
       
//注意widget的视图是身处跟widget同级的views目录下边,譬喻下边包车型大巴视图会放置在
        //  /protected/widget/test/views/test.php
        $this->render(‘test’, array(
            ‘str’=>’WIDGET视图变量’,
        ));
    }
}

也足以传参到Widget类

yiic <command-name>
[parameters…]

可望本文所述对我们基于Yii框架的PHP程序设计有着扶助。

<?php $userId = 1; ?>
<?php $this->widget('WidgetName',array('userId'=>$userId)); ?>

command-name对应的有
 – message
 – migrate
 – shell
 – webapp

你可能感兴趣的篇章:

  • yii2框架中应用下拉菜单的机动搜索yii-widget-select2实例深入分析
  • Yii中开创协调的Widget实例
  • PHP的Yii框架中创立视图和渲染视图的方法详解
  • YII
    Framework框架使用YIIC飞速创造YII应用之migrate用法实例详解
  • YII
    Framework框架教程之使用YIIC飞速创设YII应用详解
  • Yii中Model(模型)的创立及选择方法
  • yii达成创造验证码实例分析
  • yii框架通过调节台命令创建定期任务示例
  • yii框架源码深入分析之创立controller代码
  • yii2.0兑现创制轻松widgets示例

参数userId自动映射到Widget类的同名属性,所以在概念Widget时,别忘记了声称该属性。

举个例子要查阅具体的吩咐的效果可以用

2. 创建Widget

yiic help <command-name>

自定义Widget类要持续CWidget,覆盖方法run

例如:

<?php
class BannerMagic extends CWidget {
  public function run(){
  }
}
php yiic shell

或者:

1.php yiic webapp

class MyWidget extends CWidget {
  public function init() {
    // 此方法会被 CController::beginWidget() 调用
  }
   public function run() {
    // 此方法会被 CController::endWidget() 调用
  }
}

执行 php yiic help webapp

下边是是BannerMagicWidget完毕

/* 
USAGE 
 yiic webapp <app-path> 
DESCRIPTION 
 This command generates an Yii Web Application at the specified location. 
PARAMETERS 
 * app-path: required, the directory where the new application will be created. 
  If the directory does not exist, it will be created. After the application 
  is created, please make sure the directory can be accessed by Web users. 
*/ 
<?php class BannerMagicWidget extends CWidget {
  public function run() {
   $random = rand(1,3);
   if ($random == 1) {
    $advert = "advert1.jpg";
   } else if ($random == 2) {
    $advert = "advert2.jpg";
   } else {
    $advert = "advert3.jpg";
   } 
   $this->render('bannermagic',array(
    "advert"=>$advert,
   ));
  }
}

在内定的地方创立一个基于yii的完整结构的webapp。app-path是webapp的寄存目录。假若目录不设有会被成立。

存储到protected\components\BannerMagicWidget.php

例如:

相应的view文件或许的内容如下:

/yii/framework# php yiic webapp ../../testwebap
 Create a Web application under '/home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap'? [Yes|No] Yes
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap
  generate index-test.php
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/themes
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/themes/classic
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/themes/classic/views
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/themes/classic/views/layouts
  generate themes/classic/views/.htaccess
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/themes/classic/views/site
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/themes/classic/views/system
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/css
  generate css/ie.css
  generate css/bg.gif
  generate css/screen.css
  generate css/form.css
  generate css/print.css
  generate css/main.css
  generate index.php
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/assets
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/models
  generate protected/models/LoginForm.php
  generate protected/models/ContactForm.php
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/components
  generate protected/components/UserIdentity.php
  generate protected/components/Controller.php
  generate protected/yiic.bat
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/controllers
  generate protected/controllers/SiteController.php
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/migrations
  generate protected/yiic
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/data
  generate protected/data/testdrive.db
  generate protected/data/schema.mysql.sql
  generate protected/data/schema.sqlite.sql
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/config
  generate protected/config/test.php
  generate protected/config/console.php
  generate protected/config/main.php
  generate protected/.htaccess
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/layouts
  generate protected/views/layouts/column1.php
  generate protected/views/layouts/column2.php
  generate protected/views/layouts/main.php
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/site
  generate protected/views/site/contact.php
  generate protected/views/site/login.php
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/site/pages
  generate protected/views/site/pages/about.php
  generate protected/views/site/index.php
  generate protected/views/site/error.php
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/messages
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/tests
  generate protected/tests/bootstrap.php
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/tests/fixtures
  generate protected/tests/phpunit.xml
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/tests/report
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/tests/unit
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/tests/functional
  generate protected/tests/functional/SiteTest.php
  generate protected/tests/WebTestCase.php
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/runtime
  generate protected/yiic.php
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/extensions
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/commands
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/commands/shell
   mkdir /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/images

复制代码 代码如下:

Your application has been created successfully under
/yii_dev/testwebap.

<img src=”images/adverts/<?php%20echo%20$advert;%20?>” alt=”whatever”
/>

变动的目录结构如下

存储到protected\components\views\bannermagic.php

testwebap
│   ├── assets
│   ├── css
│   ├── images
│   ├── index.php
│   ├── index-test.php
│   ├── protected
│   └── themes
详细目录结构如下
├── assets
├── css
│   ├── bg.gif
│   ├── form.css
│   ├── ie.css
│   ├── main.css
│   ├── print.css
│   └── screen.css
├── images
├── index.php
├── index-test.php
├── protected
│   ├── commands
│   │   └── shell
│   ├── components
│   │   ├── Controller.php
│   │   └── UserIdentity.php
│   ├── config
│   │   ├── console.php
│   │   ├── main.php
│   │   └── test.php
│   ├── controllers
│   │   └── SiteController.php
│   ├── data
│   │   ├── schema.mysql.sql
│   │   ├── schema.sqlite.sql
│   │   └── testdrive.db
│   ├── extensions
│   ├── messages
│   ├── migrations
│   ├── models
│   │   ├── ContactForm.php
│   │   └── LoginForm.php
│   ├── runtime
│   ├── tests
│   │   ├── bootstrap.php
│   │   ├── fixtures
│   │   ├── functional
│   │   │   └── SiteTest.php
│   │   ├── phpunit.xml
│   │   ├── report
│   │   ├── unit
│   │   └── WebTestCase.php
│   ├── views
│   │   ├── layouts
│   │   │   ├── column1.php
│   │   │   ├── column2.php
│   │   │   └── main.php
│   │   └── site
│   │       ├── contact.php
│   │       ├── error.php
│   │       ├── index.php
│   │       ├── login.php
│   │       └── pages
│   │           └── about.php
│   ├── yiic
│   ├── yiic.bat
│   └── yiic.php
└── themes
    └── classic
        └── views
            ├── layouts
            ├── site
            └── system

3. 调用该Widget

透过浏览器查看一下

复制代码 代码如下:

<?php $this->widget(‘BannerMagicWidget’); ?>

现实职能前边讲。这里向来说命令的选取。

企望本文所述对大家基于Yii框架的PHP程序设计有着支持。

2.php yiic messag

您恐怕感兴趣的稿子:

  • yii2框架中运用下拉菜单的活动找寻yii-widget-select2实例深入分析
  • yii中widget的用法
  • PHP的Yii框架中创设视图和渲染视图的不二秘诀详解
  • YII
    Framework框架使用YIIC神速创造YII应用之migrate用法实例详解
  • YII
    Framework框架教程之使用YIIC飞快创立YII应用详解
  • Yii中Model(模型)的创办及运用办法
  • yii完结创造验证码实例剖析
  • yii框架通过决定台命令成立定期职责示例
  • yii框架源码深入分析之创设controller代码
  • yii2.0兑现存立轻松widgets示例

php yiic help message打字与印刷message命令的连锁用法

/* 
USAGE 
 yiic message <config-file> 
DESCRIPTION 
 This command searches for messages to be translated in the specified 
 source files and compiles them into PHP arrays as message source. 
PARAMETERS 
 * config-file: required, the path of the configuration file. You can find 
  an example in framework/messages/config.php. 
  The file can be placed anywhere and must be a valid PHP script which 
  returns an array of name-value pairs. Each name-value pair represents 
  a configuration option. 
  The following options are available: 
  - sourcePath: string, root directory of all source files. 
  - messagePath: string, root directory containing message translations. 
  - languages: array, list of language codes that the extracted messages 
   should be translated to. For example, array('zh_cn','en_au'). 
  - fileTypes: array, a list of file extensions (e.g. 'php', 'xml'). 
   Only the files whose extension name can be found in this list 
   will be processed. If empty, all files will be processed. 
  - exclude: array, a list of directory and file exclusions. Each 
   exclusion can be either a name or a path. If a file or directory name 
   or path matches the exclusion, it will not be copied. For example, 
   an exclusion of '.svn' will exclude all files and directories whose 
   name is '.svn'. And an exclusion of '/a/b' will exclude file or 
   directory 'sourcePath/a/b'. 
  - translator: the name of the function for translating messages. 
   Defaults to 'Yii::t'. This is used as a mark to find messages to be 
   translated. 
  - overwrite: if message file must be overwritten with the merged messages. 
*/ 

一言九鼎用以webapp的国际化。

比如将地方生产的testwebap的

报到页面包车型客车Username和Password   翻译成中文

地点说必要配备文件。yii为大家提供了陈设文件的模版。

framework/messages/config.php

复制framework\messages\config.php 文件到 protected\messages\下

具体内容修改如下:

dirname(__FILE__).DIRECTORY_SEPARATOR.'..', 
  'messagePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'messages', 
  'languages'=>array('zh_cn'), 
  'fileTypes'=>array('php'), 
  'overwrite'=>true, 
  'exclude'=>array( 
    '.svn', 
    'yiilite.php', 
    'yiit.php', 
    '/i18n/data', 
    '/messages', 
    '/vendors', 
    '/web/js', 
  ), 
); 

/www/yii_dev/testwebap/protected/views/site/login.php

修改如下

labelEx($model,Yii::t('login_message','username')); ?>
textField($model,'username'); ?>
error($model,'username'); ?>
labelEx($model,Yii::t('login_message','password')); ?>
passwordField($model,'password'); ?>
error($model,'password'); ?>

推行如下命令

testwebap/protected/messages
/www/yii_dev/yii/framework# php yiic message "../../testwebap/protected/messages/config.php"
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/components/Controller.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/components/UserIdentity.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/config/console.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/config/main.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/config/test.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/controllers/SiteController.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/models/ContactForm.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/models/LoginForm.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/tests/WebTestCase.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/tests/bootstrap.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/tests/functional/SiteTest.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/layouts/column1.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/layouts/column2.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/layouts/main.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/site/contact.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/site/error.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/site/index.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/site/login.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/views/site/pages/about.php...
Extracting messages from /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/yiic.php...
Saving messages to /home/coder/adata/liuyuqiang/wamp/www/yii_dev/testwebap/protected/messages/../messages/zh_cn/login_message.php...saved.

目录下转移如下文件

/www/yii_dev/testwebap/protected/messages/zh_cn/login_message.php

return array (
'password' => '',
'username' => '',
);

改为

return array (
'password' => '密码',
'username' => '用户名',
);

修改

正如配置文件

/www/yii_dev/testwebap/protected/config/main.php

增加如下

// application-level parameters that can be accessed
// using Yii::app()->params['paramName']
'params'=>array(
// this is used in contact page
'adminEmail'=>'webmaster@example.com',
),
'language'=>'zh_cn',
'sourceLanguage'=>'en_us',
);

做客一须臾间

能够看来翻译成汉语了。

能够有乱码的标题,注意编码难题。文件,浏览器要安装统一

越多关于Yii相关内容感兴趣的读者可查看本站专题:《Yii框架入门及常用本事总计》、《php特出开拓框架计算》、《smarty模板入门基础教程》、《php日期与时间用法计算》、《php面向对象程序设计入门教程》、《php字符串(string)用法总计》、《php+mysql数据库操作入门教程》及《php常见数据库操作技术汇总》

期望本文所述对我们基于Yii框架的PHP程序设计有所帮忙。

您大概感兴趣的篇章:

  • yii2框架中运用下拉菜单的活动搜索yii-widget-select2实例深入分析
  • Yii中创制自身的Widget实例
  • yii中widget的用法
  • PHP的Yii框架中创立视图和渲染视图的主意详解
  • YII
    Framework框架使用YIIC飞速成立YII应用之migrate用法实例详解
  • Yii中Model(模型)的创始及运用方法
  • yii实现创造验证码实例分析
  • yii框架通过调节台命令成立定时职务示例
  • yii框架源码分析之创立controller代码
  • yii2.0兑现创设简单widgets示例