zend框架css
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.html
在使用Zend Framework開發了一些項目三年以後,我決定經過撰寫本文來分享一些經驗。 但願對您有所幫助:)請不要猶豫,在文章後發表您本身的評論。 mysql
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.web
像PHP中的其餘大多數框架同樣,ZF實現了大多數設計模式並取得了巨大的成功,而且擁有龐大的開發人員,這些人員在快速開發環境中確實能夠很好地工做,等等。可是,這些不是本文的主題。 sql
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鍛鍊開始時,我幾乎已經放棄了。 bootstrap
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組件而無需徹底實現該框架,可是咱們將瞭解在框架的所有功能中使用框架所需的基本要求。 api
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框架