YII Framework学习教程-用YIIC快速创建YII应用之三


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列出帮助信息


  1. /* 
  2.  
  3. >> help 
  4. At the prompt, you may enter a PHP statement or one of the following commands: 
  5.  - controller 
  6.  - crud 
  7.  - form 
  8.  - help 
  9.  - model 
  10.  - module 
  11.  
  12. Type 'help <command-name>' for details about a command. 
  13.  
  14. To expand the above command list, place your command class files 
  15. under 'protected/commands/shell', or a directory specified 
  16. by the 'YIIC_SHELL_COMMAND_PATH' environment variable. The command class 
  17. must extend from CConsoleCommand. 
  18.  
  19. */  


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

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

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

help 命令

来进行查看。


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


help module

  1. >> help module  
  2. USAGE  
  3.   module <module-ID>  
  4.   
  5. DESCRIPTION  
  6.   This command generates an application module.  
  7.   
  8. PARAMETERS  
  9.  * module-ID: required, module ID. It is case-sensitive.  

创建一个模块

例如

  1.   
  1. >> module testmod  
  2.       mkdir /www/yii_dev/testwebap/protected/modules  
  3.       mkdir /www/yii_dev/testwebap/protected/modules/testmod  
  4.       mkdir /www/yii_dev/testwebap/protected/modules/testmod/models  
  5.       mkdir /www/yii_dev/testwebap/protected/modules/testmod/components  
  6.       mkdir /www/yii_dev/testwebap/protected/modules/testmod/controllers  
  7.    generate controllers/DefaultController.php  
  8.       mkdir /www/yii_dev/testwebap/protected/modules/testmod/views  
  9.       mkdir /www/yii_dev/testwebap/protected/modules/testmod/views/default  
  10.    generate views/default/index.php  
  11.       mkdir /www/yii_dev/testwebap/protected/modules/testmod/views/layouts  
  12.       mkdir /www/yii_dev/testwebap/protected/modules/testmod/messages  
  13.    generate TestmodModule.php  
  14.   
  15. Module 'testmod' has been created under the following folder:  
  16.     /www/yii_dev/testwebap/protected/modules/testmod  
  17.   
  18. You may access it in the browser using the following URL:  
  19.     http://hostname/path/to/index.php?r=testmod  
  20.   
  21. Note, the module needs to be installed first by adding 'testmod'  
  22. to the 'modules' property in the application configuration.  
  23.   
  24. >>   

生成如下代码

├── 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

  1. >>  help controller  
  2. USAGE  
  3.   controller <controller-ID> [action-ID] ...  
  4.   
  5. DESCRIPTION  
  6.   This command generates a controller and views associated with  
  7.   the specified actions.  
  8.   
  9. PARAMETERS  
  10.  * controller-ID: required, controller ID, e.g., 'post'.  
  11.    If the controller should be located under a subdirectory,  
  12.    please specify the controller ID as 'path/to/ControllerID',  
  13.    e.g., 'admin/user'.  
  14.   
  15.    If the controller belongs to a module, please specify  
  16.    the controller ID as 'ModuleID/ControllerID' or  
  17.    'ModuleID/path/to/Controller' (assuming the controller is  
  18.    under a subdirectory of that module).  
  19.   
  20.  * action-ID: optional, action ID. You may supply one or several  
  21.    action IDs. A default 'index' action will always be generated.  
  22.   
  23. EXAMPLES  
  24.  * Generates the 'post' controller:  
  25.         controller post  
  26.   
  27.  * Generates the 'post' controller with additional actions 'contact'  
  28.    and 'about':  
  29.         controller post contact about  
  30.   
  31.  * Generates the 'post' controller which should be located under  
  32.    the 'admin' subdirectory of the base controller path:  
  33.         controller admin/post  
  34.   
  35.  * Generates the 'post' controller which should belong to  
  36.    the 'admin' module:  
  37.         controller admin/post  
  38.   
  39. NOTE: in the last two examples, the commands are the same, but  
  40. the generated controller file is located under different directories.  
  41. 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

  1. >> controller test   action1 action2 action3  
  2.    generate TestController.php  
  3.       mkdir /www/yii_dev/testwebap/protected/views/test  
  4.    generate action1.php  
  5.    generate action2.php  
  6.    generate action3.php  
  7.    generate index.php  
  8.   
  9. Controller 'test' has been created in the following file:  
  10.     /www/yii_dev/testwebap/protected/controllers/TestController.php  
  11.   
  12. You may access it in the browser using the following URL:  
  13.     http://hostname/path/to/index.php?r=test  
  14.   
  15. >>   

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

在项目testwebap中多了一个

TestController.php的文件

文件内容

  1. <?php  
  2.   
  3. class TestController extends Controller  
  4. {  
  5.     public function actionAction1()  
  6.     {  
  7.         $this->render('action1');  
  8.     }  
  9.   
  10.     public function actionAction2()  
  11.     {  
  12.         $this->render('action2');  
  13.     }  
  14.   
  15.     public function actionAction3()  
  16.     {  
  17.         $this->render('action3');  
  18.     }  
  19.   
  20.     public function actionIndex()  
  21.     {  
  22.         $this->render('index');  
  23.     }  
  24.   
  25.     // -----------------------------------------------------------  
  26.     // Uncomment the following methods and override them if needed  
  27.     /* 
  28.     public function filters() 
  29.     { 
  30.         // return the filter configuration for this controller, e.g.: 
  31.         return array( 
  32.             'inlineFilterName', 
  33.             array( 
  34.                 'class'=>'path.to.FilterClass', 
  35.                 'propertyName'=>'propertyValue', 
  36.             ), 
  37.         ); 
  38.     } 
  39.  
  40.     public function actions() 
  41.     { 
  42.         // return external action classes, e.g.: 
  43.         return array( 
  44.             'action1'=>'path.to.ActionClass', 
  45.             'action2'=>array( 
  46.                 'class'=>'path.to.AnotherActionClass', 
  47.                 'propertyName'=>'propertyValue', 
  48.             ), 
  49.         ); 
  50.     } 
  51.     */  
  52. }  

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
  1. <?php  
  2. $this->breadcrumbs=array(  
  3.     'Test'=>array('test/index'),  
  4.     'Action1',  
  5. );?>  
  6. <h1><?php echo $this->id . '/' . $this->action->id; ?></h1>  
  7.   
  8. <p>You may change the content of this page by modifying the file <tt><?php echo __FILE__; ?></tt>.</p>  



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


通过
可以访问。



以下操作需要数据库的。
配额之文件中修改如下
  1. /* 
  2. 'db'=>array( 
  3.     'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db', 
  4. ), 
  5. */  
  6. // uncomment the following to use a MySQL database  
  7.   
  8. 'db'=>array(  
  9.     'connectionString' => 'mysql:host=localhost;dbname=testdrive',  
  10.     'emulatePrepare' => true,  
  11.     'username' => 'root',  
  12.     'password' => '',  
  13.     'charset' => 'utf8',  
  14. ),  

然后创建在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


  1. >> help model  
  2. USAGE  
  3.   model <class-name> [table-name]  
  4.   
  5. DESCRIPTION  
  6.   This command generates a model class with the specified class name.  
  7.   
  8. PARAMETERS  
  9.  * class-name: required, model class name. By default, the generated  
  10.    model class file will be placed under the directory aliased as  
  11.    'application.models'. To override this default, specify the class  
  12.    name in terms of a path alias, e.g., 'application.somewhere.ClassName'.  
  13.   
  14.    If the model class belongs to a module, it should be specified  
  15.    as 'ModuleID.models.ClassName'.  
  16.   
  17.    If the class name ends with '*', then a model class will be generated  
  18.    for EVERY table in the database.  
  19.   
  20.    If the class name contains a regular expression deliminated by slashes,  
  21.    then a model class will be generated for those tables whose name  
  22.    matches the regular expression. If the regular expression contains  
  23.    sub-patterns, the first sub-pattern will be used to generate the model  
  24.    class name.  
  25.   
  26.  * table-name: optional, the associated database table name. If not given,  
  27.    it is assumed to be the model class name.  
  28.   
  29.    Note, when the class name ends with '*', this parameter will be  
  30.    ignored.  
  31.   
  32. EXAMPLES  
  33.  * Generates the Post model:  
  34.         model Post  
  35.   
  36.  * Generates the Post model which is associated with table 'posts':  
  37.         model Post posts  
  38.   
  39.  * Generates the Post model which should belong to module 'admin':  
  40.         model admin.models.Post  
  41.   
  42.  * Generates a model class for every table in the current database:  
  43.         model *  
  44.   
  45.  * Same as above, but the model class files should be generated  
  46.    under 'protected/models2':  
  47.         model application.models2.*  
  48.   
  49.  * Generates a model class for every table whose name is prefixed  
  50.    with 'tbl_' in the current database. The model class will not  
  51.    contain the table prefix.  
  52.         model /^tbl_(.*)$/  
  53.   
  54.  * Same as above, but the model class files should be generated  
  55.    under 'protected/models2':  
  56.         model application.models2./^tbl_(.*)$/  
  57.   
  58. >>   




  1. >> model User tbl_user  
  2.    generate models/User.php  
  3.    generate fixtures/tbl_user.php  
  4.    generate unit/UserTest.php  
  5.   
  6. The following model classes are successfully generated:  
  7.     User  
  8.   
  9. If you have a 'db' database connection, you can test these models now with:  
  10.     $model=User::model()->find();  
  11.     print_r($model);  


├── migrations
├── models
│   ├── ContactForm.php
│   ├── LoginForm.php
│   └── User.php
├── modules
│   └── testmod
│       ├── components

  1. <?php  
  2.   
  3. /** 
  4.  * This is the model class for table "tbl_user". 
  5.  * 
  6.  * The followings are the available columns in table 'tbl_user': 
  7.  * @property integer $id 
  8.  * @property string $username 
  9.  * @property string $password 
  10.  * @property string $email 
  11.  */  
  12. class User extends CActiveRecord  
  13. {  
  14.     /** 
  15.      * Returns the static model of the specified AR class. 
  16.      * @return User the static model class 
  17.      */  
  18.     public static function model($className=__CLASS__)  
  19.     {  
  20.         return parent::model($className);  
  21.     }  
  22.   
  23.     /** 
  24.      * @return string the associated database table name 
  25.      */  
  26.     public function tableName()  
  27.     {  
  28.         return 'tbl_user';  
  29.     }  
  30.   
  31.     /** 
  32.      * @return array validation rules for model attributes. 
  33.      */  
  34.     public function rules()  
  35.     {  
  36.         // NOTE: you should only define rules for those attributes that  
  37.         // will receive user inputs.  
  38.         return array(  
  39.             array('username, password''required'),  
  40.             array('username, password, email''length''max'=>128),  
  41.             // The following rule is used by search().  
  42.             // Please remove those attributes that should not be searched.  
  43.             array('id, username, password, email''safe''on'=>'search'),  
  44.         );  
  45.     }  
  46.   
  47.     /** 
  48.      * @return array relational rules. 
  49.      */  
  50.     public function relations()  
  51.     {  
  52.         // NOTE: you may need to adjust the relation name and the related  
  53.         // class name for the relations automatically generated below.  
  54.         return array(  
  55.         );  
  56.     }  
  57.   
  58.     /** 
  59.      * @return array customized attribute labels (name=>label) 
  60.      */  
  61.     public function attributeLabels()  
  62.     {  
  63.         return array(  
  64.             'id' => 'Id',  
  65.             'username' => 'Username',  
  66.             'password' => 'Password',  
  67.             'email' => 'Email',  
  68.         );  
  69.     }  
  70.   
  71.     /** 
  72.      * Retrieves a list of models based on the current search/filter conditions. 
  73.      * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions. 
  74.      */  
  75.     public function search()  
  76.     {  
  77.         // Warning: Please modify the following code to remove attributes that  
  78.         // should not be searched.  
  79.   
  80.         $criteria=new CDbCriteria;  
  81.   
  82.         $criteria->compare('id',$this->id);  
  83.   
  84.         $criteria->compare('username',$this->username,true);  
  85.   
  86.         $criteria->compare('password',$this->password,true);  
  87.   
  88.         $criteria->compare('email',$this->email,true);  
  89.   
  90.         return new CActiveDataProvider('User'array(  
  91.             'criteria'=>$criteria,  
  92.         ));  
  93.     }  
  94. }  





├── tests
│   ├── bootstrap.php
│   ├── fixtures
│   │   └── tbl_user.php
│   ├── functional
│   │   └── SiteTest.php
│   ├── phpunit.xml
│   ├── report
│   ├── unit
│   │   └── UserTest.php
│   └── WebTestCase.php


 


help crud

 
  1. >> help crud  
  2. USAGE  
  3.   crud <model-class> [controller-ID] ...  
  4.   
  5. DESCRIPTION  
  6.   This command generates a controller and views that accomplish  
  7.   CRUD operations for the specified data model.  
  8.   
  9. PARAMETERS  
  10.  * model-class: required, the name of the data model class. This can  
  11.    also be specified as a path alias (e.g. application.models.Post).  
  12.    If the model class belongs to a module, it should be specified  
  13.    as 'ModuleID.models.ClassName'.  
  14.   
  15.  * controller-ID: optional, the controller ID (e.g. 'post').  
  16.    If this is not specified, the model class name will be used  
  17.    as the controller ID. In this caseif the model belongs to  
  18.    a module, the controller will also be created under the same  
  19.    module.  
  20.   
  21.    If the controller should be located under a subdirectory,  
  22.    please specify the controller ID as 'path/to/ControllerID'  
  23.    (e.g. 'admin/user').  
  24.   
  25.    If the controller belongs to a module (different from the module  
  26.    that the model belongs to), please specify the controller ID  
  27.    as 'ModuleID/ControllerID' or 'ModuleID/path/to/Controller'.  
  28.   
  29. EXAMPLES  
  30.  * Generates CRUD for the Post model:  
  31.         crud Post  
  32.   
  33.  * Generates CRUD for the Post model which belongs to module 'admin':  
  34.         crud admin.models.Post  
  35.   
  36.  * Generates CRUD for the Post model. The generated controller should  
  37.    belong to module 'admin', but not the model class:  
  38.         crud Post admin/post  
  39.   
  40. >>   




>> 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


>> 



如果添加删除数据是需要登录的。密码用户名是admin,admin



官方实例

可以使用web版本的yii






help form


  1. >> help form  
  2. USAGE  
  3.   form <model-class> <view-name> [scenario]  
  4.   
  5. DESCRIPTION  
  6.   This command generates a form view that can be used to collect inputs  
  7.   for the specified model.  
  8.   
  9. PARAMETERS  
  10.  * model-class: required, model class. This can be either the name of  
  11.    the model class (e.g. 'ContactForm'or the path alias of the model  
  12.    class file (e.g. 'application.models.ContactForm'). The former can  
  13.    be used only if the class can be autoloaded.  
  14.   
  15.  * view-name: required, the name of the view to be generated. This should  
  16.    be the path alias of the view script (e.g. 'application.views.site.contact').  
  17.   
  18.  * scenario: optional, the name of the scenario in which the model is used  
  19.    (e.g. 'update''login'). This determines which model attributes the  
  20.    generated form view will be used to collect user inputs for. If this  
  21.    is not provided, the scenario will be assumed to be '' (empty string).  
  22.   
  23. EXAMPLES  
  24.  * Generates the view script for the 'ContactForm' model:  
  25.         form ContactForm application.views.site.contact  
  26.   
  27. >>   



为User创建form 

  1. userformtest  
  1. >> form User application.views.user.userformtest  
  2.    generate userformtest.php  
  3. The following form view has been successfully created:  
  4. <span style="white-space:pre">  </span>/www/yii_dev/testwebap/protected/views/user/userformtest.php  
  5.   
  6.   
  7. You may use the following code in your controller action:  
  8.   
  9.   
  10. public function actionUser()  
  11. {  
  12.     $model=new User;  
  13.   
  14.   
  15.     // uncomment the following code to enable ajax-based validation  
  16.     /* 
  17.     if(isset($_POST['ajax']) && $_POST['ajax']==='user-form') 
  18.     { 
  19.         echo CActiveForm::validate($model); 
  20.         Yii::app()->end(); 
  21.     } 
  22.     */  
  23.   
  24.   
  25.     if(isset($_POST['User']))  
  26.     {  
  27.         $model->attributes=$_POST['User'];  
  28.         if($model->validate())  
  29.         {  
  30.             // form inputs are valid, do something here  
  31.             return;  
  32.         }  
  33.     }  
  34.     $this->render('userformtest',array('model'=>$model));  
  35. }  
  36.   
  37.   
  38. >>   



├── 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

  1. <div class="form">  
  2.   
  3. <?php $form=$this->beginWidget('CActiveForm'array(  
  4.     'id'=>'user-form',  
  5.     'enableAjaxValidation'=>false,  
  6. )); ?>  
  7.   
  8.     <p class="note">Fields with <span class="required">*</span> are required.</p>  
  9.   
  10.     <?php echo $form->errorSummary($model); ?>  
  11.   
  12.     <div class="row">  
  13.         <?php echo $form->labelEx($model,'username'); ?>  
  14.         <?php echo $form->textField($model,'username'); ?>  
  15.         <?php echo $form->error($model,'username'); ?>  
  16.     </div>  
  17.   
  18.     <div class="row">  
  19.         <?php echo $form->labelEx($model,'password'); ?>  
  20.         <?php echo $form->textField($model,'password'); ?>  
  21.         <?php echo $form->error($model,'password'); ?>  
  22.     </div>  
  23.   
  24.     <div class="row">  
  25.         <?php echo $form->labelEx($model,'email'); ?>  
  26.         <?php echo $form->textField($model,'email'); ?>  
  27.         <?php echo $form->error($model,'email'); ?>  
  28.     </div>  
  29.   
  30.     <div class="row buttons">  
  31.         <?php echo CHtml::submitButton('Submit'); ?>  
  32.     </div>  
  33.   
  34. <?php $this->endWidget(); ?>  
  35.   
  36. </div><!-- form --> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值