4.yiic shell

此功能是最常用的功能。他可以帮助我们创建大部分的程序结构。具体实现的内容需要我们自己来实现。

如何使用yiic shell太和其他的命令有点不同。因为他是依赖与一个web应用的。

通过如下命令进入指定web应用的shell模式

/www/yii_dev/yii/framework# php yiic shell  ../../testwebap/index.php

例如上述,进入 testwebap的命令模式,注意指定入口文件,一般是index.php

/www/yii_dev/yii/framework# php yiic shell ../../testwebap/index.php
Yii Interactive Tool v1.1 (based on Yii v1.1.8)
Please type 'help' for help. Type 'exit' to quit.
>> 

exit退出

help列出帮助信息


/*>> help
At the prompt, you may enter a PHP statement or one of the following commands:- controller- crud- form- help- model- moduleType 'help <command-name>' for details about a command.To expand the above command list, place your command class files
under 'protected/commands/shell', or a directory specified
by the 'YIIC_SHELL_COMMAND_PATH' environment variable. The command class
must extend from CConsoleCommand.*/

shell模式提供了controller,crud,form,help,model,module几个命令。

了解mvc的,应该不用说了。

如果我们要查看具体命令的使用方法可以

help 命令

来进行查看。


上面说要确保'protected/commands/shell'有必要的内容,一般用yiic webapp创建的应用都有。这个是是哟给你yiic shel模式必备的。你也可以对命令进行扩展。


help module

>> help module
USAGEmodule <module-ID>DESCRIPTIONThis command generates an application module.PARAMETERS* module-ID: required, module ID. It is case-sensitive.

创建一个模块

例如

>> module testmodmkdir /www/yii_dev/testwebap/protected/modulesmkdir /www/yii_dev/testwebap/protected/modules/testmodmkdir /www/yii_dev/testwebap/protected/modules/testmod/modelsmkdir /www/yii_dev/testwebap/protected/modules/testmod/componentsmkdir /www/yii_dev/testwebap/protected/modules/testmod/controllersgenerate controllers/DefaultController.phpmkdir /www/yii_dev/testwebap/protected/modules/testmod/viewsmkdir /www/yii_dev/testwebap/protected/modules/testmod/views/defaultgenerate views/default/index.phpmkdir /www/yii_dev/testwebap/protected/modules/testmod/views/layoutsmkdir /www/yii_dev/testwebap/protected/modules/testmod/messagesgenerate TestmodModule.phpModule 'testmod' has been created under the following folder:/www/yii_dev/testwebap/protected/modules/testmodYou may access it in the browser using the following URL:http://hostname/path/to/index.php?r=testmodNote, the module needs to be installed first by adding 'testmod'
to the 'modules' property in the application configuration.>>

生成如下代码

├── models
│   ├── ContactForm.php
│   └── LoginForm.php
├── modules
│   └── testmod
│       ├── components
│       ├── controllers
│       ├── messages
│       ├── models
│       ├── TestmodModule.php
│       └── views
├── runtime
├── tests


我们生成的testwebap项目默认不是modules,controller模式的。这里需要修改配置文件才可以使用

在配置文件中修改如下

'modules'=>array('testmod',),

通过

http://www.localyii.com/testwebap/index.php?r=testmod

就可以访问了

 help controller

>>  help controller
USAGEcontroller <controller-ID> [action-ID] ...DESCRIPTIONThis command generates a controller and views associated withthe specified actions.PARAMETERS* controller-ID: required, controller ID, e.g., 'post'.If the controller should be located under a subdirectory,please specify the controller ID as 'path/to/ControllerID',e.g., 'admin/user'.If the controller belongs to a module, please specifythe controller ID as 'ModuleID/ControllerID' or'ModuleID/path/to/Controller' (assuming the controller isunder a subdirectory of that module).* action-ID: optional, action ID. You may supply one or severalaction IDs. A default 'index' action will always be generated.EXAMPLES* Generates the 'post' controller:controller post* Generates the 'post' controller with additional actions 'contact'and 'about':controller post contact about* Generates the 'post' controller which should be located underthe 'admin' subdirectory of the base controller path:controller admin/post* Generates the 'post' controller which should belong tothe 'admin' module:controller admin/postNOTE: in the last two examples, the commands are the same, but
the generated controller file is located under different directories.
Yii is able to detect whether 'admin' refers to a module or a subdirectory.

controller  控制器名称  action名称列表

控制器名称是必须的,action名称是可以选的,也可以是多个。没有则默认有一个index

如果要为指定的应用模块创建一个控制器需要指定模块名称路径。例如

controller admin/post

位置admin模块创建post控制器类

创建test控制器,action有action1,action2,action3

>> controller test   action1 action2 action3generate TestController.phpmkdir /www/yii_dev/testwebap/protected/views/testgenerate action1.phpgenerate action2.phpgenerate action3.phpgenerate index.phpController 'test' has been created in the following file:/www/yii_dev/testwebap/protected/controllers/TestController.phpYou may access it in the browser using the following URL:http://hostname/path/to/index.php?r=test>>

├── controllers
│   ├── SiteController.php
│   └── TestController.php

在项目testwebap中多了一个

TestController.php的文件

文件内容

<?phpclass TestController extends Controller
{public function actionAction1(){$this->render('action1');}public function actionAction2(){$this->render('action2');}public function actionAction3(){$this->render('action3');}public function actionIndex(){$this->render('index');}// -----------------------------------------------------------// Uncomment the following methods and override them if needed/*public function filters(){// return the filter configuration for this controller, e.g.:return array('inlineFilterName',array('class'=>'path.to.FilterClass','propertyName'=>'propertyValue',),);}public function actions(){// return external action classes, e.g.:return array('action1'=>'path.to.ActionClass','action2'=>array('class'=>'path.to.AnotherActionClass','propertyName'=>'propertyValue',),);}*/
}

view下也自动为我们创建了相关页面

├── views
│   ├── layouts
│   │   ├── column1.php
│   │   ├── column2.php
│   │   ├── main.php
│   │   └── main.php~
│   ├── site
│   │   ├── contact.php
│   │   ├── error.php
│   │   ├── index.php
│   │   ├── login.php
│   │   └── pages
│   └── test
│       ├── action1.php
│       ├── action2.php
│       ├── action3.php
│       └── index.php
<?php
$this->breadcrumbs=array('Test'=>array('test/index'),'Action1',
);?>
<h1><?php echo $this->id . '/' . $this->action->id; ?></h1><p>You may change the content of this page by modifying the file <tt><?php echo __FILE__; ?></tt>.</p>

具体内容就需要自己来修改了。

通过
http://www.localyii.com/testwebap/index.php?r=test
可以访问。
以下操作需要数据库的。
配额之文件中修改如下
     /*'db'=>array('connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',),*/// uncomment the following to use a MySQL database'db'=>array('connectionString' => 'mysql:host=localhost;dbname=testdrive','emulatePrepare' => true,'username' => 'root','password' => '','charset' => 'utf8',),

然后创建在mysql中创建

testdrive数据库
CREATE TABLE `tbl_user` (`id` int(11) NOT NULL AUTO_INCREMENT,`username` varchar(128) NOT NULL,`password` varchar(128) NOT NULL,`email` varchar(128) DEFAULT NULL,PRIMARY KEY (`id`)
)

tbl_user数据表

help model
>> help model
USAGEmodel <class-name> [table-name]DESCRIPTIONThis command generates a model class with the specified class name.PARAMETERS* class-name: required, model class name. By default, the generatedmodel class file will be placed under the directory aliased as'application.models'. To override this default, specify the classname in terms of a path alias, e.g., 'application.somewhere.ClassName'.If the model class belongs to a module, it should be specifiedas 'ModuleID.models.ClassName'.If the class name ends with '*', then a model class will be generatedfor EVERY table in the database.If the class name contains a regular expression deliminated by slashes,then a model class will be generated for those tables whose namematches the regular expression. If the regular expression containssub-patterns, the first sub-pattern will be used to generate the modelclass name.* table-name: optional, the associated database table name. If not given,it is assumed to be the model class name.Note, when the class name ends with '*', this parameter will beignored.EXAMPLES* Generates the Post model:model Post* Generates the Post model which is associated with table 'posts':model Post posts* Generates the Post model which should belong to module 'admin':model admin.models.Post* Generates a model class for every table in the current database:model ** Same as above, but the model class files should be generatedunder 'protected/models2':model application.models2.** Generates a model class for every table whose name is prefixedwith 'tbl_' in the current database. The model class will notcontain the table prefix.model /^tbl_(.*)$/* Same as above, but the model class files should be generatedunder 'protected/models2':model application.models2./^tbl_(.*)$/>>
>> model User tbl_usergenerate models/User.phpgenerate fixtures/tbl_user.phpgenerate unit/UserTest.phpThe following model classes are successfully generated:UserIf you have a 'db' database connection, you can test these models now with:$model=User::model()->find();print_r($model);
├── migrations
├── models
│   ├── ContactForm.php
│   ├── LoginForm.php
│   └── User.php
├── modules
│   └── testmod
│       ├── components
<?php/*** This is the model class for table "tbl_user".** The followings are the available columns in table 'tbl_user':* @property integer $id* @property string $username* @property string $password* @property string $email*/
class User extends CActiveRecord
{/*** Returns the static model of the specified AR class.* @return User the static model class*/public static function model($className=__CLASS__){return parent::model($className);}/*** @return string the associated database table name*/public function tableName(){return 'tbl_user';}/*** @return array validation rules for model attributes.*/public function rules(){// NOTE: you should only define rules for those attributes that// will receive user inputs.return array(array('username, password', 'required'),array('username, password, email', 'length', 'max'=>128),// The following rule is used by search().// Please remove those attributes that should not be searched.array('id, username, password, email', 'safe', 'on'=>'search'),);}/*** @return array relational rules.*/public function relations(){// NOTE: you may need to adjust the relation name and the related// class name for the relations automatically generated below.return array();}/*** @return array customized attribute labels (name=>label)*/public function attributeLabels(){return array('id' => 'Id','username' => 'Username','password' => 'Password','email' => 'Email',);}/*** Retrieves a list of models based on the current search/filter conditions.* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.*/public function search(){// Warning: Please modify the following code to remove attributes that// should not be searched.$criteria=new CDbCriteria;$criteria->compare('id',$this->id);$criteria->compare('username',$this->username,true);$criteria->compare('password',$this->password,true);$criteria->compare('email',$this->email,true);return new CActiveDataProvider('User', array('criteria'=>$criteria,));}
}
├── tests
│   ├── bootstrap.php
│   ├── fixtures
│   │   └── tbl_user.php
│   ├── functional
│   │   └── SiteTest.php
│   ├── phpunit.xml
│   ├── report
│   ├── unit
│   │   └── UserTest.php
│   └── WebTestCase.php
help crud
>> help crud
USAGEcrud <model-class> [controller-ID] ...DESCRIPTIONThis command generates a controller and views that accomplishCRUD operations for the specified data model.PARAMETERS* model-class: required, the name of the data model class. This canalso be specified as a path alias (e.g. application.models.Post).If the model class belongs to a module, it should be specifiedas 'ModuleID.models.ClassName'.* controller-ID: optional, the controller ID (e.g. 'post').If this is not specified, the model class name will be usedas the controller ID. In this case, if the model belongs toa module, the controller will also be created under the samemodule.If the controller should be located under a subdirectory,please specify the controller ID as 'path/to/ControllerID'(e.g. 'admin/user').If the controller belongs to a module (different from the modulethat the model belongs to), please specify the controller IDas 'ModuleID/ControllerID' or 'ModuleID/path/to/Controller'.EXAMPLES* Generates CRUD for the Post model:crud Post* Generates CRUD for the Post model which belongs to module 'admin':crud admin.models.Post* Generates CRUD for the Post model. The generated controller shouldbelong to module 'admin', but not the model class:crud Post admin/post>>

>> crud User
   generate UserController.php
   generate UserTest.php
      mkdir /www/yii_dev/testwebap/protected/views/user
   generate create.php
   generate update.php
   generate index.php
   generate view.php
   generate admin.php
   generate _form.php
   generate _view.php
   generate _search.php

Crud 'user' has been successfully created. You may access it via:
http://hostname/path/to/index.php?r=user

>> 

http://www.localyii.com/testwebap/index.php?r=user
如果添加删除数据是需要登录的。密码用户名是admin,admin
官方实例
http://www.yiiframework.com/doc/guide/1.1/zh_cn/quickstart.first-app-yiic
可以使用web版本的yii
http://www.yiiframework.com/doc/guide/1.1/zh_cn/quickstart.first-app#sec-3
help form
>> help form
USAGEform <model-class> <view-name> [scenario]DESCRIPTIONThis command generates a form view that can be used to collect inputsfor the specified model.PARAMETERS* model-class: required, model class. This can be either the name ofthe model class (e.g. 'ContactForm') or the path alias of the modelclass file (e.g. 'application.models.ContactForm'). The former canbe used only if the class can be autoloaded.* view-name: required, the name of the view to be generated. This shouldbe the path alias of the view script (e.g. 'application.views.site.contact').* scenario: optional, the name of the scenario in which the model is used(e.g. 'update', 'login'). This determines which model attributes thegenerated form view will be used to collect user inputs for. If thisis not provided, the scenario will be assumed to be '' (empty string).EXAMPLES* Generates the view script for the 'ContactForm' model:form ContactForm application.views.site.contact>>

为User创建form

userformtest
>> form User application.views.user.userformtestgenerate userformtest.php
The following form view has been successfully created:
/www/yii_dev/testwebap/protected/views/user/userformtest.phpYou may use the following code in your controller action:public function actionUser()
{$model=new User;// uncomment the following code to enable ajax-based validation/*if(isset($_POST['ajax']) && $_POST['ajax']==='user-form'){echo CActiveForm::validate($model);Yii::app()->end();}*/if(isset($_POST['User'])){$model->attributes=$_POST['User'];if($model->validate()){// form inputs are valid, do something herereturn;}}$this->render('userformtest',array('model'=>$model));
}>>

├── data
│   ├── schema.mysql.sql
│   ├── schema.sqlite.sql
│   └── testdrive.db
├── extensions
├── messages
│   ├── config.php
│   └── zh_cn
│       ├── login_message.php
│       └── login_message.php~
├── migrations
├── models
│   ├── ContactForm.php
│   ├── LoginForm.php
│   └── User.php
├── modules
│   └── testmod
│       ├── components
│       ├── controllers
│       ├── messages
│       ├── models
│       ├── TestmodModule.php
│       └── views
├── runtime
│   └── application.log
├── tests
│   ├── bootstrap.php
│   ├── fixtures
│   │   └── tbl_user.php
│   ├── functional
│   │   ├── SiteTest.php
│   │   └── UserTest.php
│   ├── phpunit.xml
│   ├── report
│   ├── unit
│   │   └── UserTest.php
│   └── WebTestCase.php
├── views
│   ├── layouts
│   │   ├── column1.php
│   │   ├── column2.php
│   │   ├── main.php
│   │   └── main.php~
│   ├── site
│   │   ├── contact.php
│   │   ├── error.php
│   │   ├── index.php
│   │   ├── login.php
│   │   └── pages
│   ├── test
│   │   ├── action1.php
│   │   ├── action2.php
│   │   ├── action3.php
│   │   └── index.php
│   └── user
│       ├── admin.php
│       ├── create.php
│       ├── _form.php
│       ├── index.php
│       ├── _search.php
│       ├── update.php
│       ├── userformtest.php
│       ├── _view.php
│       └── view.php

<div class="form"><?php $form=$this->beginWidget('CActiveForm', array('id'=>'user-form','enableAjaxValidation'=>false,
)); ?><p class="note">Fields with <span class="required">*</span> are required.</p><?php echo $form->errorSummary($model); ?><div class="row"><?php echo $form->labelEx($model,'username'); ?><?php echo $form->textField($model,'username'); ?><?php echo $form->error($model,'username'); ?></div><div class="row"><?php echo $form->labelEx($model,'password'); ?><?php echo $form->textField($model,'password'); ?><?php echo $form->error($model,'password'); ?></div><div class="row"><?php echo $form->labelEx($model,'email'); ?><?php echo $form->textField($model,'email'); ?><?php echo $form->error($model,'email'); ?></div><div class="row buttons"><?php echo CHtml::submitButton('Submit'); ?></div><?php $this->endWidget(); ?></div><!-- form -->

大概用法就是这样。 接下来就是详细的了解yii生成项目的结构和设计思路。

YII Framework学习教程-用YIIC快速创建YII应用之三-2011-11-11相关推荐

  1. YII Framework学习教程-用YIIC快速创建YII应用之二-2011-11-11

    3.yii migrate 查看帮助 /*/www/yii_dev/yii/framework# php yiic migrate help Error: Unknown action: helpUS ...

  2. YII Framework学习教程-YII的Model-开发规范-路径别名-命名空间-2011-11-22

    到这里,大概的YII开发已经基本可以,但是下面要将的所有课程,学完之后可以让你更爱YII.下面的教程是讲的MVC的M,model.数据,业务,代码的集中地区.所以开始之前,学学开发规范-路径别名-命名 ...

  3. YII Framework学习教程-YII的日志

    日志的作用(此处省略1000字) YII中的日志很好很强大,允许你把日志信息存放到数据库,发送到制定email,存放咋文件中,意见显示页面是,甚至可以用来做性能分析. YII中日志的基本配置:/yii ...

  4. yiilite.php,YII Framework学习教程-YII的V-view的render若干函数-2011-11-17 | 学步园

    YII中,在action可以通过$this->render来指定它的view.其实还其他一$this->render开头的函数. yiilite.php中有这么几个函数. public f ...

  5. 通过yiic来创建yii应用

    一.通过yiic来创建yii应用(*yiic命令在yii下载包的framework目录下)1.把你自已的php环境添加到系统环境变量中. 2.在命令行下输入: yiic webapp 位置\名称 yi ...

  6. Yii Framework 开发教程(31) Zii组件-DetailView 示例

     CDetailView为某个Model显示详细内容.这个要显示的Model可以为CModel或是关联数组. CDetailView通过配置 attributes来决定Model的那些属性需要显示 ...

  7. ExtAspNet学习-利用AppBox框架快速创建项目(五)—完成项目含源代码

    我们前边四个部分已经完成了框架需要的基础配置, 现在我们来完成项目 1.Subsonic 配置,首先在OraSurvey.DAO中添加App.config配置相关信息 View Code 1 < ...

  8. Yii Framework 开发教程(32) Zii组件-GridView示例

     CGridView 以表格的形式显示数据,CGridView 也支持分页和排序,CGridView最基本的用法和ListView类型,也是通过设置 data provider,通常是CActiv ...

  9. Yii Framework 开发教程(30) Zii组件-ListView 示例

    CListView可以用来显示列表,CListView支持使用自定义的View模板显示列表的的记录,因此可以非常灵活的显示数据的表,这点有点像Android的ListView:-). CListVie ...

最新文章

  1. python四大软件-9个使用Python的世界级软件公司
  2. 为什么说Lucene不好
  3. monkeyrunner环境配置
  4. php mysql记录用户行为_PHP实现用session来实现记录用户登陆信息
  5. 区分Debug版还是Relase版
  6. php加水印功能,PHP图片加水印功能
  7. Spring 事务失效的 8 大场景,面试官直呼666...
  8. Python3中遇到UnicodeEncodeError: 'ascii' codec can't encode characters in ordinal not in range(128)...
  9. Boot目录下内容丢失导致系统无法启动
  10. wxpython textctrl绑键盘事件_wxPython控件学习之TextCtrl(三)响应文本控件事件
  11. 【项目.源码】深度学习实现任意风格任意内容的极速风格迁移
  12. 在计算机里分数线怎么表示什么意思,高考投档分数线是什么意思 怎么定的
  13. 俄罗斯方块代码 java_俄罗斯方块java代码-java编写俄罗斯方块代码详解分享
  14. html用记事本打字显示问号,电脑记事本问号怎么办
  15. 用 Python 总结分析男篮世界杯
  16. 计算机前沿技术科论文,计算机前沿技术论文
  17. unity帧动画事件多次播放
  18. Java集成PayPal支付
  19. python 费马检测
  20. EasyPusher安卓Android手机直播推送之RTSP流媒体协议流程

热门文章

  1. Android运行时权限一览表
  2. pyqt5制作指示灯
  3. 虚拟化 VMware ESXi(二)
  4. Nginx配置模块详解及多站点共用80端口案例
  5. android 获得手机MAC 和 IP
  6. 零基础学软件测试,最先开始学什么?
  7. APP运营需要注重的细节
  8. effective c++第四章(条款18-25)
  9. arduino+A4889+步进电机
  10. vue获取当前路由的几种方式