国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php框架 > ZendFramework > Zend Framework教程-用Zend命令行快速创建Zend应用

Zend Framework教程-用Zend命令行快速创建Zend应用

来源:程序员人生   发布时间:2013-12-27 22:21:36 阅读次数:4041次

  

      Zend Framework提供了命令行的方式快速创建应用,如果是用ZendStudio9,可能步采用命令行的方式,当时你稀饭的话,可以用。

      下载完整的库文件包,在bin目录的几个脚本就是用来创建应用的。

root@coder-671T-M:/mydev_src/zend_framework_learn/ZendFramework-1.11.11# tree -L 2.├── bin│   ├── zf.bat│   ├── zf.php│   └── zf.sh├── demos│   └── Zend├── documentation│   ├── api│   └── manual├── externals│   └── dojo├── extras│   ├── library│   └── tests├── incubator├── INSTALL.txt├── library│   └── Zend├── LICENSE.txt├── README.txt├── resources│   └── languages├── src└── tests    ├── AllTests.php    ├── phpunit.xml    ├── resources    ├── runtests.sh    ├── TestConfiguration.php.dist    ├── TestHelper.php    └── Zend20 directories, 11 files


具体给出的命令格式如下:

Zend Framework Command Line Console Tool v1.11.11Usage:    zf [--global-opts] action-name [--action-opts] provider-name [--provider-opts] [provider parameters ...]    Note: You may use "?" in any place of the above usage string to ask for more specific help information.    Example: "zf ? version" will list all available actions for the version provider.Providers and their actions:  Version    zf show version mode[=mini] name-included[=1]    Note: There are specialties, use zf show version.? to get specific help on them.  Config    zf create config    zf show config    zf enable config    Note: There are specialties, use zf enable config.? to get specific help on them.    zf disable config    Note: There are specialties, use zf disable config.? to get specific help on them.  Phpinfo    zf show phpinfo  Manifest    zf show manifest  Profile    zf show profile  Project    zf create project path name-of-profile file-of-profile    zf show project    Note: There are specialties, use zf show project.? to get specific help on them.  Application    zf change application.class-name-prefix class-name-prefix  Model    zf create model name module  View    zf create view controller-name action-name-or-simple-name module  Controller    zf create controller name index-action-included[=1] module  Action    zf create action name controller-name[=Index] view-included[=1] module  Module    zf create module name  Form    zf enable form module    zf create form name module  Layout    zf enable layout    zf disable layout  DbAdapter    zf configure db-adapter dsn section-name[=production]  DbTable    zf create db-table name actual-table-name module force-overwrite    Note: There are specialties, use zf create db-table.? to get specific help on them.  ProjectProvider    zf create project-provider name actions

 为了运行方便可以把如下目录加入到path

/mydev_src/zend_framework_learn/ZendFramework-1.11.11/bin

设置临时的path,重启会失效的。

export PATH=$PATH:/mydev_src/zend_framework_learn/ZendFramework-1.11.11/bin


显示框架版本

root@coder-671T-M:~# zf.sh show version
Zend Framework Version: 1.11.11


Project 创建一个项目
    zf create project path name-of-profile file-of-profile
    zf show project
    Note: There are specialties, use zf show project.? to get specific help on them.



root@coder-671T-M:/mydev_src/zend_framework_learn/www# zf.sh create project zf_demo2Creating project at /mydev_src/zend_framework_learn/www/zf_demo2Note: This command created a web project, for more information setting up your VHOST, please see docs/READMETesting Note: PHPUnit was not found in your include_path, therefore no testing actions will be created.root@coder-671T-M:/mydev_src/zend_framework_learn/www# ll总用量 28drwxr-xr-x 6 root root 4096 2011-12-06 16:35 ./drwxr-xr-x 9 root root 4096 2011-12-06 16:08 ../drwxr-xr-x 4 root root 4096 2011-12-06 12:48 .metadata/-rw-r--r-- 1 root root   17 2011-12-06 12:30 phpinfo.phpdrwxr-xr-x 2 root root 4096 2011-12-06 12:48 RemoteSystemsTempFiles/drwxr-xr-x 8 root root 4096 2011-12-06 13:56 zf_demo1/drwxr-xr-x 7 root root 4096 2011-12-06 16:35 zf_demo2/root@coder-671T-M:/mydev_src/zend_framework_learn/www# 
创建成功,访问一下:

http://localzend.com/zf_demo2/public/

显示项目结构信息

 

root@coder-671T-M:/mydev_src/zend_framework_learn/www/zf_demo2# zf.sh show profileProjectDirectory    ProjectProfileFile    ApplicationDirectory        ConfigsDirectory            ApplicationConfigFile        ControllersDirectory            ControllerFile                ActionMethod            ControllerFile        ModelsDirectory        ViewsDirectory            ViewScriptsDirectory                ViewControllerScriptsDirectory                    ViewScriptFile                ViewControllerScriptsDirectory                    ViewScriptFile            ViewHelpersDirectory        BootstrapFile    DocsDirectory        file    LibraryDirectory    PublicDirectory        PublicIndexFile        HtaccessFile    TestsDirectory        TestPHPUnitConfigFile        TestPHPUnitBootstrapFile        TestApplicationDirectory            TestApplicationControllerDirectory                TestApplicationControllerFile        TestLibraryDirectoryroot@coder-671T-M:/mydev_src/zend_framework_learn/www/zf_demo2# 

显示Phpinfo
    zf.sh show phpinfo



查看指定命令的帮助信息:

zf.sh ?  (命令)



Config创建include_path配置文件
    zf create config
    zf show config
    zf enable config
    Note: There are specialties, use zf enable config.? to get specific help on them.
    zf disable config
    Note: There are specialties, use zf disable config.? to get specific help on them.
root@coder-671T-M:/mydev_src/zend_framework_learn/www/zf_demo2# zf.sh create config
Successfully written Zend Tool config.
It is located at: /root/.zf.ini

大体内容

php.include_path = "/mydev_src/zend_framework_learn/ZendFramework-1.11.11/library:.:/usr/share/php:/usr/share/pear"

作用不言而喻。





Model 创建Model,可以指定模块,也是创建全局的

    zf create model name module

root@coder-671T-M:/mydev_src/zend_framework_learn/www/zf_demo2# zf.sh create model myModel testmod
Note: The canonical model name that is used with other providers is "MyModel"; not "myModel" as supplied
Creating a model at /mydev_src/zend_framework_learn/www/zf_demo2/application/modules/testmod/models/MyModel.php
Updating project profile '/mydev_src/zend_framework_learn/www/zf_demo2/.zfproject.xml'

 /zf_demo2/application/modules/testmod/models/MyModel.php

<?phpclass Testmod_Model_MyModel{}



Controller 创建Controller,可以指定模块,也是创建全局的
    zf create controller name index-action-included[=1] module

root@coder-671T-M:/mydev_src/zend_framework_learn/www/zf_demo2# zf.sh create controller NewsManage  1  testmodNote: PHPUnit is required in order to generate controller test stubs.Creating a controller at /mydev_src/zend_framework_learn/www/zf_demo2/application/modules/testmod/controllers/NewsManageController.phpCreating an index action method in controller NewsManageCreating a view script for the index action method at /mydev_src/zend_framework_learn/www/zf_demo2/application/modules/testmod/views/scripts/news-manage/index.phtmlUpdating project profile '/mydev_src/zend_framework_learn/www/zf_demo2/.zfproject.xml'
root@coder-671T-M:/mydev_src/zend_framework_learn/www/zf_demo2/application/modules# tree .└── testmod    ├── controllers    │   └── NewsManageController.php    ├── forms    │   └── TestForm.php    ├── models    │   └── MyModel.php    └── views        ├── filters        ├── helpers        └── scripts            └── news-manage                └── index.phtml9 directories, 4 files
/zf_demo2/application/modules/testmod/controllers/NewsManageController.php

<?phpclass Testmod_NewsManageController extends Zend_Controller_Action{    public function init()    {        /* Initialize action controller here */    }    public function indexAction()    {        // action body    }}




Action为指定controller创建action,默认是的controller是Index
    zf create action name controller-name[=Index] view-included[=1] module


root@coder-671T-M:/mydev_src/zend_framework_learn/www/zf_demo2# zf.sh create action add NewsManage 1 testmod
Note: PHPUnit is required in order to generate controller test stubs.
Creating an action named add inside controller at /mydev_src/zend_framework_learn/www/zf_demo2/application/modules/testmod/controllers/NewsManageController.php
Updating project profile '/mydev_src/zend_framework_learn/www/zf_demo2/.zfproject.xml'
Creating a view script for the add action method at /mydev_src/zend_framework_learn/www/zf_demo2/application/modules/testmod/views/scripts/news-manage/add.phtml
Updating project profile '/mydev_src/zend_framework_learn/www/zf_demo2/.zfproject.xml'

root@coder-671T-M:/mydev_src/zend_framework_learn/www/zf_demo2/application/modules# tree.└── testmod    ├── controllers    │   └── NewsManageController.php    ├── forms    │   └── TestForm.php    ├── models    │   └── MyModel.php    └── views        ├── filters        ├── helpers        └── scripts            └── news-manage                ├── add.phtml                └── index.phtml9 directories, 5 files

<?phpclass Testmod_NewsManageController extends Zend_Controller_Action{    public function init()    {        /* Initialize action controller here */    }    public function indexAction()    {        // action body    }    public function addAction()    {        // action body    }}





 View 创建视图脚本
    zf create view controller-name action-name-or-simple-name module

root@coder-671T-M:/mydev_src/zend_framework_learn/www/zf_demo2# zf.sh create action edit  NewsManage  testmodNote: PHPUnit is required in order to generate controller test stubs.Creating an action named edit inside controller at /mydev_src/zend_framework_learn/www/zf_demo2/application/modules/testmod/controllers/NewsManageController.phpUpdating project profile '/mydev_src/zend_framework_learn/www/zf_demo2/.zfproject.xml'Creating a view script for the edit action method at /mydev_src/zend_framework_learn/www/zf_demo2/application/views/scripts/news-manage/edit.phtmlUpdating project profile '/mydev_src/zend_framework_learn/www/zf_demo2/.zfproject.xml'root@coder-671T-M:/mydev_src/zend_framework_learn/www/zf_demo2# zf.sh create view  NewsManage edit  testmodUpdating project profile '/mydev_src/zend_framework_learn/www/zf_demo2/.zfproject.xml' 
root@coder-671T-M:/mydev_src/zend_framework_learn/www/zf_demo2# zf.sh create view  NewsManage  editx  testmodUpdating project profile '/mydev_src/zend_framework_learn/www/zf_demo2/.zfproject.xml'

.└── testmod    ├── controllers    │   └── NewsManageController.php    ├── forms    │   └── TestForm.php    ├── models    │   └── MyModel.php    └── views        ├── filters        ├── helpers        └── scripts            └── news-manage                ├── add.phtml                ├── edit.phtml                ├── editx.phtml                └── index.phtml9 directories, 7 files



Module
    zf create module name
为你的项目增加一个模块

 

root@coder-671T-M:/mydev_src/zend_framework_learn/www/zf_demo2# zf.sh create module testmodCreating the following module and artifacts:/mydev_src/zend_framework_learn/www/zf_demo2/application/modules/testmod/controllers/mydev_src/zend_framework_learn/www/zf_demo2/application/modules/testmod/models/mydev_src/zend_framework_learn/www/zf_demo2/application/modules/testmod/views/mydev_src/zend_framework_learn/www/zf_demo2/application/modules/testmod/views/scripts/mydev_src/zend_framework_learn/www/zf_demo2/application/modules/testmod/views/helpers/mydev_src/zend_framework_learn/www/zf_demo2/application/modules/testmod/views/filtersAdded a key for path module directory to the application.ini fileUpdating project profile '/mydev_src/zend_framework_learn/www/zf_demo2/.zfproject.xml'
配置文件中增加了

resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"



Form创建Form
    zf enable form module

    zf create form name module

为指定模块创建Form

root@coder-671T-M:/mydev_src/zend_framework_learn/www/zf_demo2# zf.sh create form testForm testmodCreating a form at /mydev_src/zend_framework_learn/www/zf_demo2/application/modules/testmod/forms/TestForm.phpUpdating project profile '/mydev_src/zend_framework_learn/www/zf_demo2/.zfproject.xml'

/zf_demo2/application/modules/testmod/forms/TestForm.php

<?phpclass Testmod_Form_TestForm extends Zend_Form{    public function init()    {        /* Form Elements & Other Definitions Here ... */    }}

创建全局的Form

root@coder-671T-M:/mydev_src/zend_framework_learn/www/zf_demo2# zf.sh create form testForm 
Creating a form at /mydev_src/zend_framework_learn/www/zf_demo2/application/forms/TestForm.php
Updating project profile '/mydev_src/zend_framework_learn/www/zf_demo2/.zfproject.xml'
/zf_demo2/application/forms/TestForm.php

<?phpclass Application_Form_TestForm extends Zend_Form{    public function init()    {        /* Form Elements & Other Definitions Here ... */    }}



Layout配置layout

    zf enable layout
    zf disable layout


root@coder-671T-M:/mydev_src/zend_framework_learn/www/zf_demo2# zf.sh enable layoutA layout entry has been added to the application config file.A default layout has been created at /mydev_src/zend_framework_learn/www/zf_demo2/application/layouts/scripts/layout.phtmlUpdating project profile '/mydev_src/zend_framework_learn/www/zf_demo2/.zfproject.xml'

resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"

/zf_demo2/application/layouts/scripts/layout.phtml

<?php echo $this->layout()->content; ?>


DbAdapter 添加数据库配置信息

    zf configure db-adapter dsn section-name[=production]


root@coder-671T-M:/mydev_src/zend_framework_learn/www/zf_demo2# zf.sh configure db-adapter "adapter=Mysqli&username=uname&password=mypass&dbname=mydb"
A db configuration for the production section has been written to the application config file.


resources.db.adapter = "Mysqli"resources.db.params.username = "uname"resources.db.params.password = "mypass"resources.db.params.dbname = "mydb"


DbTable 创建一个DbTable。
    zf create db-table name actual-table-name module force-overwrite
    Note: There are specialties, use zf create db-table.? to get specific help on them.

root@coder-671T-M:/mydev_src/zend_framework_learn/www/zf_demo2# zf.sh create db-table  TblUser  tbl_user testmod 
Creating a DbTable at /mydev_src/zend_framework_learn/www/zf_demo2/application/modules/testmod/models/DbTable/TblUser.php
Updating project profile '/mydev_src/zend_framework_learn/www/zf_demo2/.zfproject.xml'

 
es# tree.└── testmod    ├── controllers    │   └── NewsManageController.php    ├── forms    │   └── TestForm.php    ├── models    │   ├── DbTable    │   │   └── TblUser.php    │   └── MyModel.php    └── views

<?phpclass Testmod_Model_DbTable_TblUser extends Zend_Db_Table_Abstract{    protected $_name = 'tbl_user';}


 

以上是zendframework提供的命令工具,可以根据需要使用,使用命令行要确保所有的东西都是用命令行完成的,因为命令行的运行是通过记录在配置文件中完成的,如果手动加入一些功能,命令行就无法进行正确的运行了。



生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生