zend框架_启动Zend框架

zend框架

After developing some projects with Zend Framework for three years, I decided to share some of my experience by writing this article.  I  hope it helps someone out there :)   Please do not hesitate to contribute by posting your own comments after the article.

在使用Zend Framework开发了一些项目三年之后,我决定通过撰写本文来分享一些经验。 希望对您有所帮助:)请不要犹豫,在文章后发表您自己的评论。

Like most of the other frameworks in PHP, ZF implements most of design patterns with great success and has a huge development staff that really works well in a rapid development environment, etc.   But those are not the subject of this article.

像PHP中的其他大多数框架一样,ZF实现了大多数设计模式并取得了巨大的成功,并且拥有庞大的开发人员,这些人员在快速开发环境中确实可以很好地工作,等等。但是,这些不是本文的主题。

In this article, I'll try to explain the basics of the framework.  I have to confess that at the beginning of my ZF workout, I was almost giving up.

在本文中,我将尝试解释该框架的基础。 我不得不承认,在我的ZF锻炼开始时,我几乎已经放弃了。

We should always remember that unlike most of other frameworks we can use ZF components separately without implementing the framework completely, but we will have a look at basic requirements that we need to use framework in its full functionality.

我们应该始终记住,与大多数其他框架不同,我们可以单独使用ZF组件而无需完全实现该框架,但是我们将了解在框架的全部功能中使用框架所需的基本要求。

ZF works with MVC design pattern (if you ask yourself what it that? don't commit suicide... it basically separates database operations, interface development, CSS, HTML, JavaScript, etc., and the development language that handles process between models and views to create dynamic content which is completely based on URL.

ZF使用MVC设计模式工作(如果您问自己这是什么?不要自杀...它基本上将数据库操作,接口开发,CSS,HTML,JavaScript等,以及处理模型之间流程的开发语言分开)和视图来创建完全基于URL的动态内容。

If we have a look at ZF-based project, we will realize that we point the related controller in our browser address bar.  In addition to that, ZF looks for the related section in that controller as an action in URL.  For example:

如果我们看一下基于ZF的项目,我们将意识到我们将相关的控制器指向了浏览器地址栏中。 除此之外,ZF还在该控制器中查找相关部分作为URL中的操作。 例如:

http://localhost/Controller/Action  http:// localhost / Controller / Action

in a real life project, we may consider as...

在现实生活中,我们可以考虑...

http://localhost/Index/login http:// localhost / Index / login

Each controller is a class that extends Zend_Controller_Action with appendix of "Controller" and each action is a method in that class that takes appendix of "Action" , so we should imagine as:

每个控制器都是使用Zend_Controller_Action扩展带有“ Controller”附录的类,并且每个动作都是该类中带有“ Action”附录的方法,因此我们可以想象为:

    class IndexController extends Zend_Controller_Action {
        public function loginAction() {
            // login related operations triggered from here
        }
    }

1) Enable mod_rewrite

1)启用mod_rewrite

Due to the URL-based build of ZF, first we must enable mod_rewrite  in our web servers, for Apache / Linux users, it is as easy as:

由于基于ZF的基于URL的构建,首先我们必须在Web服务器中启用mod_rewrite,对于Apache / Linux用户而言,它非常简单:

    sudo a2enmod rewrite

sudo a2enmod重写

Then changing "allowoverride" directive to "all" from http.conf or the default file from "sites-available" will let us use the .htaccess file, so again we need this to use http://localhost/Controller/Action URL build.  Here is a working .htaccess file:

然后将http.conf中的“ allowoverride”指令更改为“ all”或将“ sites-available”的默认文件更改为.htaccess文件,因此我们再次需要使用http:// localhost / Controller / Action URL建立。 这是一个有效的.htaccess文件:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]

RewriteCond %{REQUEST_FILENAME} -d

RewriteRule ^.*$ - [NC,L]

RewriteRule ^.*$ index.php [NC,L]

RewriteRule !\.(js|ico|txt|gif|jpg|png|css)$ index.php

2) Installing ZendFramework (Do not run!! we ll just copy some folders :P )

2)安装ZendFramework(不要运行!我们只复制一些文件夹:P)

In fact, this is the easiest part .  After downloading ZF from http://www.zendframework.com/

实际上,这是最简单的部分。 从http://www.zendframework.com/下载ZF之后

just upload the Zend folder to your web server and add the path to "include_path" directive in php.ini .

只需将Zend文件夹上传到您的Web服务器,然后将路径添加到php.ini中的“ include_path”指令即可。

While your php.ini is open, let me add something:  The basic Zend framework folder build is something like ;

当您打开php.ini时,让我添加一些内容:基本的Zend框架文件夹构建类似于;

  Applications /

应用/

        controllers/

控制器/

        models/

楷模/

        views/

观看次数/

                scripts/

脚本/

                        layouts/

布局/

        config.ini

config.ini

  www/

万维网/

        .htacccess

.htacccess

        index.php

index.php

So if we add those three main folders to include_path we won't have to deal with requiring include operations in our project...  although some experts insist that this is an optimization fault, I don't care at all :) .  For example:

因此,如果我们将这三个主文件夹添加到include_path中,我们将不必处理在项目中需要包含操作的问题……尽管一些专家坚持认为这是优化错误,但我完全不在乎:)。 例如:

    include_path = ".:/var/applications:/var/applications/controllers:/var/applications/models"

include_path =“。:/ var / applications:/ var / 应用 ns /控制 lers:/ var / 应用 ns / models”

I usually upload the Zend folder under the models directory, so I don't need to re-type inclusion directive for it in that case.

3) Let's have a look at our index.php aka bootstrap file

3)让我们看看我们的index.php aka引导文件

I am not sure why people try to give complex names to such files anyway it is index.php for me here is a working sample :) ;

我不确定为什么人们总是尝试给此类文件使用复杂的名称,因为对我来说,它是index.php :)

require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload() ; # __autoload() magic function used in ZF 
$frontController = Zend_Controller_Front::getInstance();
$frontController->throwExceptions(true); 
$frontController->setControllerDirectory('/var/applications/controllers');

$section = getenv('PLACES_CONFIG') ? getenv('PLACES_CONFIG') : 'live'; # We parse the live section of our config.ini file 
$config = new Zend_Config_Ini('/var/applications/config.ini', $section);  # We call our config.ini file
Zend_Registry::set('config', $config); 								   # And save it in config var
Zend_Controller_Action_HelperBroker::addPrefix('Cal_Helper'); 
$layout = Zend_Layout::startMvc('/var/applications/views/scripts/layouts'); # Two step view pattern implementation starts here , also we can assign layout name as first parameter otherwise it will be [layout].phtml as default

$db = Zend_Db::factory($config->db->adapter, $config->db->config->toArray()); #Connecting to database
Zend_Db_Table::setDefaultAdapter($db);						                     #Setting default database adapter
Zend_Registry::set('db', $db);	#We populate $db var 
	try {    ## And we dispatch here
		$frontController->dispatch(); 
	}
	catch (Exception $exp) {
		$contentType = 'text/html';
		header("Content-Type: $contentType; charset=utf-8");
		echo 'Unexpected Error :';
		echo '<h2>Hata: ' . $exp->getMessage() . '</h2><br /><pre>';
		echo $exp->getTraceAsString();
	}

Also, here is a working config.ini example:

另外,这是一个有效的config.ini示例:

[general]

db.adapter = PDO_MYSQL
db.config.host = "localhost"
db.config.username = "usernamehere"
db.config.password = "passwordhere" 
db.config.dbname = "yourdbnamehere"

[live : general]

[dev : general]

[test : general]

As you see, ZF has cross-database support.  Also, you should need an .htaccess file.

如您所见,ZF具有跨数据库支持。 另外,您还需要一个.htaccess文件。

To summarize:

总结一下:

1) Enable mod_rewrite ,set allowoverride directive to on, check mysqli extension if you will use mysql as DB adapter.

1)启用mod_rewrite,将allowoverride指令设置为on,如果将mysql用作数据库适配器,请检查mysqli扩展名。

2) Build your folder structure, upload Zend directory, create your model,view,controller directories, configure your include_path

2)建立您的文件夹结构,上传Zend目录,创建您的模型,视图,控制器目录,配置您的include_path

3) Create your index.php,config.ini and .htaccess files

3)创建您的index.php,config.ini和.htaccess文件

It seems a little complicated, but when you start to use, it is worth all the mess, accelerates development  by %60 and makes your project sustainable, robust and easy to develop.

看起来有点复杂,但是当您开始使用它时,值得一团糟,将开发速度提高了60%,并使您的项目可持续,强大且易于开发。

翻译自: https://www.experts-exchange.com/articles/4118/Starting-Zend-Framework.html

zend框架

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值