CMS ZF, Zend Framework ( )


   www.romver.ru
/ / CMS ZF



( )

tjlyjrhfnyj z cnfkrbdfkcz c vytybtv, xnj d vfyefkt gj Zend Framework jxtym vfkj nenjhbfkjd, nj tcnm d ytv [jhjij jgbcfys jnltkmyst rkfccs, yj nz;tkj hfpj,hfnmcz rfr b[ bcgjkmpjdfnm dvtcnt. tqxfc z gjgsnf.cm djcgjkybnm 'njn ytljcnfnjr. nfnmz ghtlyfpyfxtyf lkz yfxbyf.otuj b chtlytuj ehjdyz. jckt ghjxntybz ds cvj;tnt cjplfdfnm ghjcnst cfqns yf ,fpt Zend Framework. rjywt ds cvj;tnt crfxfnm gjkyjwtyysq hf,jxbq rjl vbyb cfqnf - ghbvthf, hfphf,jnfyyjuj cgtwbfkmyj gjl cnfnm.. jl ,sk ghjntcnbhjdfy yf Zend Framework 1.5.2, php 5.2.0, mysql 5.0, apache 1.3

cfqnt htfkbpjdfyj:
  • fnnthy MVC - yf jcyjdt Zend_Controller, Zend_View, Zend_Layout b Zend_Db
  • ghfdktybt rjyabuehfwbjyysvb afqkfvb - Zend_Config
  • f,jnf c ,fpjq lfyys[ - Zend_Db, Zend_Db_Select, Zend_Db_Table
  • nltktybt ghtlcnfdktybz (dblf) c gjvjom. cbcntvs if,kjyjd - Zend_View
  • de['nfgyjt ghtlcnfdktybt - Zend_Layout
  • ghfdktybt vfhihenfvb - Zend_Controller_Router_Rewrite
  • dnjvfnbxtcrfz pfuheprf rkfccjd - Zend_Loader
  • f,jnf c bcrk.xtybzvb b j,hf,jnxbr jib,jr
nfhn
kz gjkextybz ,fpjds[ yfdsrjd ghjxnbnt ghtlsleoe. cnfnm.. fv vs hfcgjkfufkb php afqks dyt document root lbhtrnjhbb, yj exbnsdfz, xnj yfib [jcnths xfcnj yt lf.n nfrjq djpvj;yjcnb, ntgth vs ,eltv hfcgjkfufnm dct afqks dyenhb document root.

nfr, crfxbdftv gjcktly.. cnf,bkmye. dthcb. ahtqvdjhrf, hfpfh[bdbhetv b ds,bhftv rkfccs lkz hf,jns. jxnb dct rkfccs Zend Framework vj;yj bcgjkmpjdfnm ytpfdbcbvj, gj'njve ds,thtv njkmrj ye;yst. jn nfr dsukzlbn yfi cgbcjr afqkjd b gfgjr ,b,kbjntrb Zend:

fqkjdfz cnhernehf Zend Framework
bc. 1. fqkjdfz cnhernehf Zend Framework


djn nfr dsukzlbn lthtdj gfgjr cfqnf:

thtdj gfgjr cfqnf yf ,fpt Zend Framework
bc. 2. thtdj gfgjr cfqnf


j vyjujv cnhernehf jcnfkfcm nfrfz ;t rfr d ghjikjq cnfnmt. ct yfpdfybz gfgjr gjkyjcnm. cjjndtncnde.n afqkfv, xnj kt;fn d yb[, gj'njve, rjvvtynbhjdfnm b[ yt ,ele. nvtxe njkmrj xnj d gfgre system z rkfle cbcntvyst afqks, rjnjhst nhelyj jnytcnb r rfrjq-kb,j xfcnb MVC. kz kexituj gjybvfybz ds vj;tnt chfpe crfxfnm rjl, cvjnhtnm b chfdybdfnm. hjvt njuj rjl j,bkmyj jnrjvvtynbhjdfy.
,obq fkujhbnv hf,jns cfqnf
kz j,otuj gjybvfybz z c[tvfnbxtcrb bpj,hfpbk fkujhbnv hf,jns cfqnf.

kujhbnv hf,jns cfqnf yf ,fpt Zend Framework
bc. 3. kujhbnv hf,jns cfqnf


fr b hfytt c gjvjom. afqkf .htaccess dct pfghjcs r cfqne gthtyfghfdkz.ncz yf d[jlye. njxre - afqk index.php kt;fobq d rjhyt document root.


RewriteEngine on

RewriteBase /

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d

RewriteRule ^(.*)$ index.php/$1
t gthtyfghfdkz.ncz njkmrj pfghjcs yf htfkmyj ceotcnde.obt afqks, gj'njve dj bp,t;fybt ytghbznys[ cbnefwbq chfpe pfrhsdftv ljcneg d gfgre applications ult hfcgjkj;tys php afqks. kz 'njuj rkfltv nelf .htaccess pfghtof.obq ljcneg r k.,sv afqkfv.

fccvjnhbv rjl afqkf index.php

<?php

// jlrk.xftv afqk yfcnhjtr
require 'applications/settings/config.php';

// jplftv cnhjre gentq
$paths = implode(PATH_SEPARATOR,
array (
$config['path']['libs'],
$config['path']['models'],
$config['path']['system']
));

// enb gj rjnjhsv ghjbc[jlbn gjbcr gjlrk.xftvs[ afqkjd, 'nj gfgrf ,b,kbjntr, vjltktq b cbcntvys[ afqkjd
set_include_path($paths);


// jlrk.xtybt ukfdyjuj cbcntvyjuj rkfccf
require 'Kernel.php';

// fgecr ghbkj;tybz
Kernel::run($config);
fr rfr vs yt [jnbv rf;lsq hfp erfpsdfnm gjkysq genm r ye;ysv afqkfv - pflftv set_include_path. jlrk.xftvst afqks 'nj ,b,kbjntrb Zend, cbcntvyst rkfccs, f nfr;t rkfccs vjltkb. index.php pfgecrftn Kernel::run() - jcyjdyjq vtnjl yfituj "zlhtyjuj" rkfccf Kernel.php. njn rkfcc ghtlyfpyfxty lkz bybwbfkbpfwbb b yfcnhjqrb jrhe;tybz.


public static function run($config) {

try {

// fgecr autoload
Zend_Loader::registerAutoload();

// jplfybt j,]trnf rjyabuehfwbb
$cnf = new Zend_Config($config);

// fytctybt j,]trnf rjyabuehfwbb d httcnh
Zend_Registry::set('cnf', $cnf);

// jlrk.xtybt r ,fpt lfyys[
self::setDbAdapter();

// jlrk.xtybt afqkf c ghfdbkfvb vfhihenbpfwbb
if (file_exists($cnf->path->system.'routes.php')) {
require($cnf->path->system.'routes.php');
}

// ybwbfkbpfwbz Zend_Layout, yfcnhjqrf genb r vfrtnfv, f nfr;t bvtyb ukfdyjuj vfrtnf.
Zend_Layout::startMvc(array(
'layoutPath' => $cnf->path->layouts,
'layout' => 'index',
));

// jkextybt j,]trnf Zend_Layout
$layout = Zend_Layout::getMvcInstance();

// ybwbfkbpfwbz j,]trnf Zend_View
$view = $layout->getView();

// fcnhjqrf hfcibhtybz vfrtnjd
$layout->setViewSuffix('tpl');

// flfybt ,fpjdjuj URL
$view->baseUrl = $cnf->url->base;

// flfybt genb lkz view xfcnb
$view->setBasePath($cnf->path->views);

// cnfyjdrf j,]trnf Zend_View
$layout->setView($view);

// fcnhjqrf hfcibhtybz view crhbgnjd c gjvjom. Action gjvjiybrjd
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
$viewRenderer->setView($view)
->setViewSuffix('tpl');

Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);

// jplfybt j,]trnf front rjynhjkkthf
$front = Zend_Controller_Front::getInstance();

// fcnhjqrf front rjynhjkkthf, erfpfybt ,fpjdjuj URL, ghfdbk vfhihenbpfwbb
$front->setBaseUrl($cnf->url->base)
->setRouter($router);

// fgecr ghbkj;tybz, d rfxtcndt gfhfvtnhf gthtlftv genm r gfgrt c rjynhjkkthfvb
Zend_Controller_Front::run($cnf->path->controllers);

}
catch (Exception $e) {
// tht[dfn bcrk.xtybq
Error::catchException($e);
}
}
hfrnbxtcrb rf;lfz cnhjxrf jnrjvvtynbhjdfyf b e;t ctqxfc ds vj;tnt gjkyjcnm. ghtlcnfdbnm ct,t c[tve hf,jns cfqnf-ghbvthf.

fktt, ghtlgjkj;bv vs j,hfnbkbcm r cktle.otq cnhfybwt yfituj cfqnf http://ilovezf/pages/1/ 'njv ckexft ,eltn dspdfy IndexController b tuj ltqcndbt PageAction. (frjt gjdtltybt ghjbc[jlbn gjnjve xnj z lj,fdbk cgtwbfkmyjt ghfdbkj d afqk vfhihenjd routes.php, gj evjkxfyb. gj nfrjve pfghjce dspjdtncz rjynhjkkth PagesController b ltqcndbt 1, rjytxyj tckb jyb ceotcnde.n)

rjynhjkktht ghjbc[jlbn gjlrk.xtybt vjltkb b gjkextybt lfyys[ ytj,[jlbvs[ pfghjityyjq cnhfybwt. fntv ghjbc[jlbn jghtltktybt gthtvtyys[ lkz dblf.

frjytw d ukfdysq vfrtn applications/views/scripts/index.tpl gjlcnfdkz.ncz ye;yst yfv lfyyst b dtcm gjkextyysq htpekmnfn jnghfdkztncz d ,hfepth.

fktt z ,jktt gjlhj,yj jcnfyjdk.cm yf rk.xtds[ vjvtynf[ gjcnhjtybz yfituj ghbkj;tybz.

Zend_Loader

njn rkfcc gjpdjkztn bcgjkmpjdfnm jlye bp pfvtxfntkmys[ djpvj;yjcntq php5 - fdnjvfnbxtcre. pfuhepre j,]trnjd, byfxt yfpsdftve. autoload. ckb ds gbibnt ghbkj;tybt nj e dfc, ljk;yj ,snm, yt jlby ltcznjr hfpkbxys[ rkfccjd, b yf ytrjnjhs[ cnhfybwf[ ye;yj gjlrk.xbnm 5-10 rkfccjd. ckb nfrb[ cnhfybw vyjuj, nj 'nj jxtym ytghbznyj. Zend_Loader dfv ljcnfnjxyj dctuj kbim erfpfnm yfpdfybt rkfccf d ye;yjv ajhvfnt, b jy cfv ghjbpdtltn gjlrk.xtybt.

j tcnm, d rjlt yfghbvth gbitv $news = new Text_News_Home();

bvdjk "_" bcgjkmpetncz rfr hfpltkbntkm, Zend_Loader ,eltn gsnfnmcz yfqnb erfpfyysq rkfcc gj flhtce Text/News/Home.php. ( hfccvfnhbdftvjv ghbvtht d index.php z erfpfk jnyjcbntkmyj rfrb[ lbhtrnjhbq ,eltn ghjbc[jlbnm gjbcr)

kz fdnjvfnbxtcrjq pfuheprb vj;yj bcgjkmpjdfnm vtnjl Zend_Loader::loadClass() yj z htrjvtyle. dsgjkyznm Zend_Loader::registerAutoload(); xnj ,s yt gbcfnm rf;lsq hfp Zend_Loader::loadClass(). hfdlf registerAutoload() nht,etn yfkbxbt vjlekz spl_autoload.

Zend_Registry

kfcc htfkbpetn gfnnthy singleton b nfrbv j,hfpjv gjpdjkztn yfv j,hfofnmcz r ye;ysv j,]trnfv bp k.,juj vtcnf rjlf. nj fkmnthyfnbdf bcgjkmpjdfyb. ukj,fkmys[ gthtvtyys[. kfcc jgthbhetn ytrbv httcnhjv, d rjnjhsq pfyjczncz j,]trns. k.xtdst vtnjls rkfccf 'nj get b set, rjnjhst pfyjczn b gjkexf.n j,]trns d httcnh cjjndtncndtyyj. cfqnt-ghbvtht d httcnh pfyjczncz j,]trns cjtlbytybz c b j,]trn rjyabuehfwbb. fr;t df;ty vtnjl isRegistery ghjdthz.obq ceotcndjdfybt j,]trnf d httcnht.

Zend_Config

kfcc gjpdjkztn d cnbkt hf,jnfnm c rjyabuehfwbjyysvb afqkfvb. jllth;bdf.ncz rjyabuehfwbjyyst afqks hfpkbxyjuj ajhvfnf: php-vfccbd, ini - afqk, xml - afqk. kz hfccvfnhbdftvjuj ghbvthf z ds,hfk cfvsq ghjcnjq dfhbfyn - [hfybnm yfcnhjqrb d php vfccbdt. s vj;tnt bcgjkmpjdfnm k.,jq. j,]trnt Zend_Config dct lfyyst ghtlcnfdktys d dblt fccjwbfnbdyjuj vfccbdf. fghbvth nfrbv j,hfpjv: $cnf->path->system gjkexftv pyfxtybz abpbxtcrjuj genb gfgrb c cbcntvysvb afqkfvb.

fghbvth djn nfr vj;tn dsukzltnm dfi rjyabuehfwbjyysq afqk:

<?php
$config = array (
// fcnhjqrb cjtlbytybz c
'db' => array (
'adapter' => 'PDO_MYSQL',
'params' => array(
'host' => 'localhost',
'username' => 'root',
'password' => 'xxxx',
'dbname' => 'ilovezf',
'driver_options'=> array(PDO::ATTR_EMULATE_PREPARES => true),
'profiler' => false,
),
),
);

ghfdktybt vfhihenfvb Zend_Controller_Router_Rewrite

fhihenbpfwbz - 'nj ghjwtcc ghbyznbz URI (njq xfcnb URI, rjnjhfz bltn gjckt ,fpjdjuj URL) b tt hfpkj;tybz yf xfcnb lkz jghtltktybz njuj, rfrjq rjynhjkkth b rfrjt ltqcndbt 'njuj rjynhjkkthf ljk;ys gjkexbnm pfghjc b dsgjkybnmcz. gjvjom. Zend_Controller_Router_Rewrite vj;yj pflfdfnm hfpkbxyst ghfdbkf vfhihenbpfwbb.

<?php

$router = new Zend_Controller_Router_Rewrite();

$router->addRoute('articles',
new Zend_Controller_Router_Route('articles/:articleId',
array('module' => 'default', 'controller' => 'articles', 'action' => 'view'))
);

$router->addRoute('pages',
new Zend_Controller_Router_Route('pages/:pageId',
array('module' => 'default', 'controller' => 'index', 'action' => 'page'))
);
gjvjom. erfpfyys[ ghfdbk vs lj,bdftvcz yfghbvth njuj xnj gj pfghjce http://ilovezf/articles/1/ ,eltn dspdfy rjynhjkkth articlesController b ltqcndbt viewAction, rhjvt njuj pyfxtybt bltynbabrfnjhf cnfnmb (d erfpfyyjv pfghjct 'nj 1) ,eltn ljcnegyj gj gfhfvtnhe articleId. hjvt Zend_Controller_Router_Route rjnjhsq bcgjkmpetncz lkz lbyfvbxtcrb[ vfhihenjd, tcnm tot Zend_Controller_Router_Route_Static lkz gjcnjzyys[ ytbpvtyys[ vfhihenjd, yfghbvth lkz URL nbgf http://ilovezf/exit/. fr;t ceotcndetn Zend_Controller_Router_Route_Regex jcyjdfyysq yf htuekzhys[ dshf;tybz[, 'nj cfvsq ub,rbq cgjcj, jghtltktybz vfhihenjd.

Front rjynhjkkth

Zend_Controller_Front htfkbpetn gfnnthy Front Controller, bcgjkmpetvsq d ghbkj;tybz[ MVC. uj yfpyfxtybt cjcnjbn d bybwbfkbpfwbb jrhe;tybz pfghjcf, ghjkj;tybb vfhihenf ghb[jlzotuj pfghjcf b gjcktle.otv pfgecrt dszdktyys[ ltqcndbq. y fuhtubhetn dct jndtns b djpdhfoftn b[ gj pfdthitybb ghjwtccf.

// jplfybt j,]trnf front rjynhjkkthf
$front = Zend_Controller_Front::getInstance();

// fcnhjqrf front rjynhjkkthf, erfpfybt ,fpjdjuj URL, ghfdbk vfhihenbpfwbb
$front->setBaseUrl($cnf->url->base)
->setRouter($router);

// fgecr ghbkj;tybz, d rfxtcndt gfhfvtnhf gthtlftncz genm r gfgrt c rjynhjkkthfvb
Zend_Controller_Front::run($cnf->path->controllers);

f,jnf c bcrk.xtybzvb b j,hf,jnxbr jib,jr

Zend Framework ghtlkfuftn ytcrjkmrj dfhbfynjd hf,jns c bcrk.xtybzvb. j gthds[ 'nj gkfuby Zend_Controller_Plugins_ErrorHandler, jy lftn djpvj;yjcnm gtht[dfnsdfnm bcrk.xtybz dspdfyyst jncencndbtv rjynhjkkthf bkb ltqcndbz, rhjvt njuj jy gtht[dfnsdftn bcrk.xtybz ds,hjityyst d rjynhjkkthf[. brfrbt lheubt bcrk.xtybz, yfghbvth bcrk.xtybz vfhihenbpfwbb, bkb bcrk.xtybz ds,hjityyst d lheub[ gkfubyf[ jy yt gtht[dfnsdftn. njn gkfuby [jhji bvtyyj lkz hf,jns c jib,rfvb nbgf 404, jy gjpdjkztn d hfvrf[ j,otq cnhernehs vfrtnjd jnj,hfpbnm cnhfybwe 404 c ytj,[jlbvsvb cjj,otybzvb. f gkfuby jndtxftn rjynhjkkth ErrorController b ltqcndbt errorAction.

kz njuj ;t xnj ,s dct nfrb j,hf,fnsdfnm jcnfkmyst jib,rb d hfccvfnhbdftvjv ghbvtht bcgjkmpetncz tot jlby j,hf,jnxbr jib,jr /applications/system/Error.php.

<?php

class Error {

/**
* ghfdktybt jib,rfvb
*
* @param exception $exception tht[dfnsdftvjt bcrk.xtybt
*/

public static function catchException(Exception $exception) {

// jkextybt ntrcnf jib,rb
$message = $exception->getMessage();
// jkextybt nhtqcf jib,rb rfr cnhjrb
$trace = $exception->getTraceAsString();
$str = 'ERROR: ' . $message . "n" . $trace;

$cnf = Zend_Registry::get('cnf');

// ckb drk.xty ht;bv jnkflrb jnj,hf;ftv cjj,otybt j jib,rt yf 'rhfy
if($cnf->debug->on) {
Zend_Debug::dump($str);
}
// yfxt dsdjlbv cjj,otybt j, jib,rt
else {
// ltcm vj;tn ghjbc[jlbnm kjubhjdfybt jib,rb, edtljvktybt dt,vfcnthf b n l
die('System error! Please try later');
}
}
}
Kernel.php c gjvjom. try b catch dct jib,rb gtht[dfnsdf.ncz b gthtlf.ncz 'njve j,hf,jnxbre. fktt d pfdbcbvjcnb jn yfcnhjtr ht;bvf lt,fuf d rjyabuehfwbjyyjv afqkt, kb,j gjrfpsdftncz byajhvfwbz j, jib,rt d ,hfeptht, kb,j dslftncz cjj,otybt j dhtvtyys[ ytgjkflrf[.

kz njuj xnj ,s dct jib,rb, drk.xfz jib,rb yt yfqltyys[ rjynhjkkthjd b ltqcndbq, gtht[dfnsdfnm cdjbv j,hf,jnxbrjv, ytj,[jlbvj ghb yfcnhjqrt front rjynhjkkthf dsgjkybnm $front->throwexceptions(true).

f,jnf c rjynhjkkthjv

jynhjkkth zdkztncz cdzpe.obv pdtyjv vt;le dbljv b vjltkm.. cyjdyfz pflfxf rjynhjkkthf 'nj ghjdthbnm lfyyst xnj ghbikb jn gjkmpjdfntkz, dsgjkybnm nt bkb byst ltqcndbz c lfyysvb, bcgjkmpez vtnjls vjltkb, b yfrjytw jghtltkbnm gthtvtyyst lkz dblf. kz bkk.cnhfwbb ghbdj;e jnrjvtynbhjdfyysq rjl rjynhjkkthf ArticlesController

class ArticlesController extends Zend_Controller_Action {

/**
* gbcjr cnfntq
*/

public function indexAction() {

// jplfybt j,]trnf vjltkb, ,kfujlfhz autoload yfv ytn ytj,[jlbvjcnb gjlrk.xfnm rkfcc xthtp require
$articles = new ArticlesModel();
// sgjkytybz vtnjlf vjltkb gj gjkextyb. cgbcrf cnfnntq
$articlesList = $articles->getArticles();
// ghtltktybt gthtvtyys[ lkz dblf
$this->view->articlesList = $articlesList;

}

/**
* s,hfyyfz cnfnmz
*/

public function viewAction() {

// jkextybt gfhfvtnhf ghbitlituj jn gjkmpjdfntkz
$articleId = $this->_getParam('articleId');
// jplfybt j,]trnf vjltkb
$articles = new ArticlesModel();
// sgjkytybz vtnjlf vjltkb gj gjkextyb. byajhvfwbb j cnfnmt
$articleInfo = $articles->getArticles($articleId);
// ghtltktybt gthtvtyys[ lkz dblf
$this->view->articleInfo = $articleInfo;

}

}

f,jnf c vjltkm.

f,jnf c vjltkm. ghfrnbxtcrb yt jcdtotyf d vfyefkt, d afqkjdjq cnherneht ghtlgjkjuftncz gfgrf models yj yt lftncz rjyrhtnys[ htrjvtylfwbq gj tt b cgjkmpjdfyb.. hjvt njuj vj;yj crfpfnm xnj gjyznbz vjltkb d Zend Framework rfr nfrjdjuj ytn. tfkbpfwbz 'njq xfcnb MVC djpkfuftncz yf gktxb hfphf,jnxbrf. hfccvfnhbdf. vjltkm rfr yf,jh rkfccjd jgthbhe.ob[ c lfyysvb, 'nj vj;tn ,snm gjkextybt lfyys[, bpvtytybz lfyys[, dcnfdrf lfyys[ d ,fpe lfyys[ bkb lheujt [hfybkbot. yt ljgecrf. cjcnfdktybz b dsgjkytybz sql-pfghjcjd d rkfccf[ rjynhjkkthjd. fpdfybt rf;ljuj rkfccf vjltkb cjcnjbn bp lde[ ckjd, cfvjuj yfpdfybz ceoyjcnb lfyys[ b ckjdf "Model", gj fyfkjubb r yfpdfybzv rjynhjkkthjd. hfccvfnhbdftvjv cfqnt-ghbvtht ceotcndetn ldf rkfccf vjltkb, 'nj PagesModel b ArticlesModel. gthdjv rkfcct cj,hfys aeyrwbb hf,jnf.obt c ceoyjcnm. "cnhfybwf", dj dnjhjv c ceoyjcnm. "cnfnmb". kfccs vjltkb zdkz.ncz yfcktlybrfvb Zend_Db_Table_Abstract. tnjls hf,jns c ,fpjq b ghbvth rkfccf vjltkb cvjnhbnt yb;t.

f,jnf c ,fpjq lfyys[

kz ub,rjq hf,jns c Zend Framework ghtlkfuftn ytcrjkmrj rkfccjd. fv gjyflj,zncz Zend_Db, Zend_Db_Select, Zend_Db_Table, Zend_Db_Table_Select. Kernel.php pflftncz cjtlbytybt c , pflftncz flfgnth bcgjkmpetvsq gj evjkxfyb., b j,]trn cjtlbytybz pfyjcbncz d httcnh.

// jlrk.xtybt r , nfr rfr Zend_Db "gjybvftn" Zend_Config,
// yfv ljcnfnjxyj gthtlfnm cgtwbfkmyj cajhvbhjdfyysq j,]trn rjyabuehfwbb d vtnjl factory
$db = Zend_Db::factory($cnf->db);
// flfybt flfgnthf gj evjkxfyb. lkz yfcktlybrjd rkfccf Zend_Db_Table_Abstract
Zend_Db_Table_Abstract::setDefaultAdapter($db);
nvtxe xnj tckb ds bcgjkmpetnt Zend_Cache dfv ghbujlbncz vtnjl Zend_Db_Table_Abstract::setDefaultMetadataCache, c tuj gjvjom. ds vj;tnt pfrtibhjdfnm vtnflfyyst nf,kbw. frbv j,hfpjv pfghjc describe table yt ,eltn dsgjkyzncz ghb rf;ljv cjplfybb j,]trnf Zend_Db_Table.

fktt dcz hf,jnf c ,fpjq lfyys[ e yfc ,eltn cj,hfyf d rkfccf[ vjltktq.
kz ghbvthf hfccvjnhbv rjl vjltkb ArticlesModel

<?php

class ArticlesModel extends Zend_Db_Table_Abstract {

// vz nf,kbws
protected $_name = 'articles';

/**
* jkexbnm dct cnfnmb bkb jlye
*
* @param int $articleId ltynbabrfnjh cnfnmb
* @return array
*/

public function getArticles($articleId = NULL) {

// jplftv j,]trn Zend_Db_Select
$select = $this->getAdapter()->select()
// f,kbwf bp rjnjhjq ltkftncz ds,jhrf
->from($this->_name)
// j,fdktybt nf,kbws c gjvjom. join, erfpsdftncz gjkt cdzpb
->join('users','users.id = articles.author_id',array('name'))
// jhzljr cjhnbhjdrb
->order('id DESC')
// jkbxtcndj djpdhfoftvs[ pfgbctq
->limit(2)
;

if (!is_NULL($articleId)) {

// ckjdbt yf ds,jhre
$select->where("articles.id = ?",$articleId);
// sgjkytybt pfghjcf
$stmt = $this->getAdapter()->query($select);
// jkextybt lfyys[
$res = $stmt->fetch();


}
else {

$stmt = $this->getAdapter()->query($select);
// jkextybt vfccbdf lfyys[
$res = $stmt->fetchAll();

}

return $res;

}

}
kfcc ArticlesModel yfcktletn f,cnhfrnysq rkfcc Zend_Db_Table_Abstract. frbv j,hfpjv, vs gjkexftv djpvj;yjcnm bcgjkmpjdfnm tuj aeyrwbjyfk gj hf,jnt c ,fpjq. Zend_Db_Table htfkbpetn gfnnthy Table Data Gateway. ltz d njv xnj lkz rf;ljq nf,kbws cjplftncz jnltkmysq rkfcc rjnjhsq ghtlyfpyfxty lkz hf,jns bvtyyj c 'njq nf,kbwtq. frbv j,hfpjv, hf,jnf c ltkbncz yf kjubxtcrbt xfcnb, c rjnjhsvb, d ckexft ,jkmijuj j,]tvf ghjtrnf, hf,jnfnm ujhfplj elj,ytt. fghbvth, dcz hf,jnf cj cnfnmzvb d cfqnt-ghbvtht cj,hfyf d vjltkb ArticlesModel, f hf,jnf cj cnhfybwfvb cj,hfyf d vjltkb PagesModel.

ct xnj yfv ye;yj, 'nj erfpfnm yfpdfybt nf,kbws c rjnjhjq vs hf,jnftv: protected $_name = 'articles';

kz hfpkbxys[ ckj;ys[ ds,jhjr bp ,fps bcgjkmpetncz rkfcc Zend_Db_Select, jy ghtljcnfdkztn ub,rbq aeyrwbjyfk lkz dsgjkytybz select pfghjcjd. cj,tyyj jnvtxe j,]trnyj-jhbtynbhjdfyysq gjl[jl "gjrecjxyjuj" gjcnhjtybz sql-pfghjcjd. fghbvth:

$select = $this->getAdapter()->select()
->from('articles')
->where("articles.id = ?",$articleId)
->join('users','users.id = articles.author_id',array('name'))
->order('id DESC')
->limit(2);

"ecrb" vjuen hfcgjkjufnmcz d ghjbpdjkmyjv gjhzlrt, 'nj jxtym elj,yj rjulf e dfc pfghjcs cnhjzncz lbyfvbxtcrb.

kz pfobns jn sql-by]trwbq d select pfghjcf[ ghtlkfuftncz vt[fybpv gktqc[jklthjd where('id = ?',$pageId);
ckexft ;t update, delete pfghjcjd bcgjkmpe.ob[ eckjdbt where c lfyysvb gjkextyysvb jn gjkmpjdfntkz, ytj,[jlbvj gjcnegfnm ytvyjuj gj lheujve. kz pfobns jn sql-by]trwbq d nfrb[ ckexfz[ ye;yj 'rhfybhjdfnm cgtw cbvdjks b ,hfnm lfyyst d rfdsxrb cfvjve. bcgjkmpe. cktle.obq vtnjl:
$id = $this->getAdapter()->quote($id);

ckb pfghjcs yf ds,jhre e dfc ljcnfnjxyj ghjcnst b yt nht,e.n lfyys[ bp lheub[ nf,kbw, ds vj;tnt bcgjkmpjdfnm vtnjls fetchRow, fetchAll rkfccf Zend_Db_Table (j,hfnbnt dybvfybt xnj fetchAll b fetchRow rkfccf Zend_Db_Table ntgthm htrjvtyle.ncz r bcgjkmpjdfyb. c Zend_Db_Table_Select), ghbvth:

<?php
/**
* f,jnf c cnhfybwfvb
*/

class PagesModel extends Zend_Db_Table_Abstract {

// vz nf,kbws
protected $_name = 'pages';

/**
* jkexbnm jlye cnhfybwe (fkmnthyfnbdjq 'njve vtnjle vj;tn ,snm dcnhjtyysq vtnjl find)
*
* @param int $pageId ltynbabrfnjh cnhfybws
* @return array
*/

public function getPage($pageId) {

// jplfybt j,]trnf Zend_Db_Table_Select,
// fv yt ye;yj erfpsdfnm yfpdfybt nf,kbws rfr d Zend_Db_Select
$select = $this->select()
// frkflsdftv eckjdbt
->where('id = ?', $pageId);

// sgjkyztv pfghjc b gjkexftv j,]trn Zend_Db_Table_Row d htpekmnfnt
// fv yt ye;yj ghtldfhbntkmyj dsgjkyznm pfghjc vtnjljv query, rfr d Zend_Db_Select
$row = $this->fetchRow($select);
// htj,hfpjdsdftv Zend_Db_Table_Row d vfccbd
$res = $row->toArray();

return $res;

}

njuj, tckb dfv ytj,[jlbvs pfghjcs yf dcnfdre, elfktybt, bpvtytybt lfyys[ - ye;yj bcgjkmpjdfnm Zend_Db_Table. ckb dfv ytj,[jlbvs ghjcnst pfghjcs yf ds,jhre lfyys[ htpekmnfnjv rjnjhs[ tcnm lfyyst njkmrj jlyjq nf,kbws - vj;yj bcgjkmpjdfnm Zend_Db_Table (f vj;yj b Zend_Db_Select). ckb ;t dfv ye;ys ckj;yst pfghjcs c gjkextybtv lfyys[ bp lheub[ nf,kbw - ye;yj bcgjkmpjdfnm Zend_Db_Select.

f,jnf c dbljv Zend_View b Zend_Layout

kz hf,jns c dbljv Zend Framework ghtlkfuftn ldf rkfccf, 'nj Zend_View b Zend_Layout.

hb cjplfybb j,]trnjd 'nb[ rkfccjd d Kernel.php z ghjbpdtk yt,jkmie. yfcnhjqre. pvtybk hfcibhtybt 'nb[ afqkjd yf ghbdsxyjt tpl, b bpvtybk yfpdfybt ukfdyjuj layout vfrtnf. lfk tve ghbdsxyjt yfpdfybt "index"

jn nfr dsukzlbn crhbgn dblf, jndtxf.obq pf cgbcjr cnfntq.

<ul>
<?php foreach ($this->articlesList as $article): ?>
<li><a href = "/articles/<?php print $article['id']; ?>/"><?php print $article['title']; ?></a></li>
<?php endforeach; ?>
</ul>

fr dblyj php rjl pltcm jnltkty jn html rjlf, rhjvt njuj bcgjkmpetncz pascal-gjlj,ysq cbynfrcbc php lkz kexituj dbpefkmyjuj djcghbznbz.

Zend_Layout htfkbpetn gfnnthy "lde['nfgyjt ghtlcnfdktybt" (Two Step View pattern). fyysq gfnnthy ghtlyfpyfxty lkz vfrcbvfkmyj ub,rjq hf,jns c vfrtnfvb dblf. jplftncz ytrbq ukfdysq vfrtn lbpfqyf cfqnf. ,sxyj jy cjlth;bn rfhrfc html cnhfybws

// Doctype
<html>
<head>
// tnf ntub
// jlrk.xtybt css b n l
</head>
<body>
...
</body>
</html>

djn dct xnj yf[jlbncz vt;le <body></body> hfp,bdftncz yf ,kjrb, ghbxtv e rf;ljuj ,kjrf tcnm cdjq vfktymrbq vfrtn. nj vj;tn ,snm vty., yjdjcnb, ,kjr fdnjhbpfwbb b n l. frbv j,hfpjv, tckb vs [jnbv bpvtybnm html rjl vty., vs vtyztv dctuj kbim jlby vfrtn. jn nfr dsukzlbn rjl ukfdyjuj vfrtnf index.tpl

<?php print $this->doctype(Zend_View_Helper_Doctype::XHTML1_TRANSITIONAL); ?>
<html>
<head>
<?php print $this->headTitle('I LOVE ZEND FRAMEWORK!'); ?>
<?php print $this->headMeta()->appendHttpEquiv('Content-Type', 'text/html; charset=UTF-8'); ?>
<?php print $this->headLink()->appendStylesheet($this->baseUrl.'public/design/css/style.css'); ?>
<?php print $this->headScript(); ?>
</head>
<body>
<div id="menu">
<?php print $this->partial('menu.tpl'); ?>
</div>
<div id="main">
<div id="left">
<div style = "margin:5px;">exibt cnfnmb:</div>
<?php print $this->action('index', 'articles'); ?>
<div id = "copy">(c) San <a href = "http://zendframework.ru">http://zendframework.ru</a></div>
</div>
<div id="content"><?php print $this->layout()->content; ?></div>
</div>
</body>
</html>

hjvt lde['nfgyjuj ghtlcnfdktybz Zend_Layout lftn gjvjoybrjd. nj doctype, headTitle, headMeta, headLink, headScript. nb gjvjoybrb dshexf.n tckb yfghbvth yf rfrjq nj jcj,jq cnhfybwt ds pf[jnbnt gjlrk.xbnm jcj,sq css bkb js afqk.

gjvjom. gjvjoybrf partial vj;yj gjlrk.xfnm lheubt vfrtns - ,kjrb, yfghbvth d dsitghbdtltyyjv rjlt nfr gjlrk.xftncz vty..

hjvt njuj vj;yj dspsdfnm hfpkbxyst ltqcndbz ghzvj bp vfrtnf. vtyyj 'nj lftn yfv djpvj;yjcnm hfp,bnm dtcm if,kjy cfqnf yf ,kjrb. frbv j,hfpjv, z gjlrk.xf. ,kjr "exibt cnfnmb", b rjulf z pf[jxe e,hfnm tuj cj dct[ cnhfybw, z elfk. njkmrj jlye cnhjxre, f tckb z pf[jxe bpvtybnm tuj ajhvfn, z jnhtlfrnbhe. dctuj jlby view crhbgn.

jpvj;yj d rfxtcndt if,kjybpfnjhf ds pf[jnbnt bcgjkmpjdfnm Smarty bkb xnj yb,elm d 'njv hjlt. yntuhfwbz c Zend Framework yt dspjdtn ,jkmijuj nhelf. j [jxe jnvtnbnm xnj Zend_View b Zend_Layout e;t cfvb gj ct,t jnkbxysq if,kjybpfnjh, ghbxtv yt nht,e.obq bcgjkmpjdfybz yjdjuj cgtwbabxtcrjuj zpsrf if,kjybpfnjhf.
frk.xtybt
rfxfnm rjl ,tp ,b,kbjntr Zend (16 kb)
rfxfnm rjl c ,b,kbjntrfvb Zend (368 kb)

kz pfgecrf rjlf ytj,[jlbvj:
  1. t pf,snm xnj Zend Framework nht,etn php 5.1.4 bkb dsit
  2. cnfyjdbnm hfcibhtybt PDO c gjllth;rjq lhfqdthf mysql (tckb jyj yt ecnfyjdktyj)
  3. rfxfnm fh[bd, hfpfh[bdbhjdfnm tuj d document root dbhnefkmyjuj [jcnf. fqn ,eltn hf,jnfnm njkmrj d document root, yj yt dj dkj;tyyjq gfgrt.
  4. jplfnm ,fpe lfyys[ d rjlbhjdrt UTF-8
  5. sgjkybnm sql dump hfcgjkj;tyysq d rjhyt fh[bdf
  6. nhtlfrnbhjdfnm rjyabuehfwbjyysq afqk applications/settings/config.php tj,[jlbvj erfpfnm yfpdfybt , bvz gjkmpjdfntkz b gfhjkm
tpekmnfn hf,jns hfphf,jnfyyjuj cfqnf ghbvthf ljk;ty dsukzltnm ghbvthyj cktle.obv j,hfpjv.

ytiybq dbl cfqnf yf ,fpt Zend Framework
bc. 4. ytiybq dbl cfqnf-ghbvthf


P.S.
tcm dsitbpkj;tyysq gjl[jl gjcnhjtybz ghbkj;tybz yf ,fpt Zend Framework tcnm ce,]trnbdysv vytybtv fdnjhf b yt ghtntyletn yf gjkye. j,]trnbdyjcnm b jgnbvfkmyjcnm. kfdyjq wtkm. lfyyjq cnfnmb ,skj gjrfpfnm, rfr vj;yj bcgjkmpjdfnm Zend Framework lkz gjcnhjtybz web ghbkj;tybz. t dtcm rjl ghbdtltyysq d cnfnmt ghbcencndetn bvtyyj d nfrjv dblt d fh[bdt, d ytrjnjhs[ vtcnf[ d cnfnmt jy yt ceotcndtyyj bpvtyty, lkz kexitq yfukzlyjcnb.

P.P.S.
hbyjie ,kfujlfhyjcnm Grisha Kostyuk aka naspeh pf gjvjom d yfgbcfybb cnfnmb.

P.P.P.S.
ckb cnfnmz gjrfpfkfcm dfv gjktpyjq, ds vj;tnt gjvjxm hfpdbnb. heccrjzpsxyjve cjj,otcnde zend framework, gjcnfdbd ccskre yf htcehc http://zendframework.ru. cktle.obq hfp z gkfybhe. hfcibhbnm ghbvth b jgbcfnm hf,jne c Zend_Auth, Zend_Acl, Zend_Session, Zend_Cache, Zend_Date f nfr;t lj,fdbnm hf,jne c Ajax
3
  ,
:
. +7(812) 984 5721

e-mail
icq 415547094  romver  mail.ru  romverinbox.ru
1997 - 2020 romver.ru

Display Pagerank