zf實戰–(一)

來源:互聯網
上載者:User

練慣用zf+smarty開發web項目。今天是第一顆。

再次應該對zf的一些常用對象有所瞭解,還有smarty。

 

一、首先構建環境

二、然後構建項目架構

三、建立設定檔

config.ini

 

[general]
db.adapter=PDO_MYSQL
db.config.host=localhost
db.config.username=root
db.config.password=123456
db.config.dbname=NetVideo

[smarty]
left_delimiter="<{"
right_delimiter="}>"
caching=0

[Format]
dateFormat=Y-m-d H:i:s

[cache]
cache.frontend.lifetime=15
cache.frontend.name=Core
cache.backend.name=File
cache.backend.cache_dir=D:/php/www/cache/
cache.frontend.automatic_serialization=true

 

resource.ini

 

[default]
index = default:index

 

IndexController.php

<?php
require_once 'Zend/Session/Namespace.php';
class IndexController extends Zend_Controller_Action

  
 public function init()
 { 
  $this->smarty=Zend_Registry::get('smarty');
  $this->config=Zend_Registry::get('config');
  $this->smarty->assign('baseUrl',$this->_request->getBaseUrl());
  
 }
 
 public function indexAction()
 {
  echo "中國您好!";
  $this->smarty->display('header.phtml'); 
  $this->smarty->display('index/index.phtml');
  $this->smarty->display('footer.phtml');
 }
 
 
}

 

模板檔案略。

 

項目入口檔案

index.php

 

<?php
 @header('Content-type:text/html;charset=UTF-8');
 //session_cache_limiter('private, must-revalidate');
 error_reporting(E_ALL|E_STRICT);
 date_default_timezone_set('Asia/Shanghai');
 define('ROOT_DIR', dirname(dirname(__FILE__))); //D:/php/www
 define('WEB_ROOT', 'http://localhost:8080/blog/');
 define('CACHE_DIR', 'D:/php/www/blog/cache/');
 define('TABLE_PRE','oophp_');
  
 set_include_path('.'.PATH_SEPARATOR.'./library'.PATH_SEPARATOR.'./application/models/'.PATH_SEPARATOR.get_include_path());
 
 //Register the autoloader(設定Zend Framework 自動載入類檔案)
 require_once 'Zend/Loader.php';
 Zend_Loader::registerAutoload();
 Zend_Session::start();
 //設定資料庫參數,並串連資料庫
 $config=new Zend_Config_Ini('./application/config/config.ini',null, true);
 Zend_Registry::set('config',$config);
 $dbAdapter=Zend_Db::factory($config->general->db->adapter,$config->general->db->config->toArray());
 $dbAdapter->query('SET NAMES UTF8');
 Zend_Db_Table::setDefaultAdapter($dbAdapter);
 Zend_Registry::set('dbAdapter',$dbAdapter);
 //config smarty
 //$smartyParams=new Zend_Config_Ini('./application/config/config.ini','smarty');
 $smarty=new Custom_View_Smarty($config->smarty->toarray());
 Zend_Registry::set('smarty',$smarty);
 //zend cache
 require_once 'Zend/Cache.php';
 /*建立cache對象*/
 $frontendOptions = $config->cache->cache->frontend->toArray();
 $backendOptions = $config->cache->cache->backend->toArray();
 $frontendName = $frontendOptions['name'];
 unset($frontendOptions['name']);
 $backendName = $backendOptions['name'];      
 unset($backendOptions['name']);
 if (empty($backendOptions['cache_dir']))
 {
  $backendOptions['cache_dir'] = CACHE_DIR;
 }  
 $cache = Zend_Cache::factory($frontendName, $backendName, $frontendOptions, $backendOptions);
 Zend_Registry::set('cache',$cache);
 
 /*
 $view = new Zend_View();
 Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')
 ->setView($view);*/
 //create auth object
 $auth=Zend_Auth::getInstance();
 //create acl object
 $acl=new Custom_KenAcl();

 $frontController = Zend_Controller_Front::getInstance();
 $frontController->addModuleDirectory('./application/modules')
                //->setRouter($router)
                ->registerPlugin(new Custom_Controller_Plugin_Smarty())
                ->setBaseUrl('/blog')
                ->setParam('useDefaultControllerAlways', true)
                ->setParam('noViewRenderer', true)
                ->setParam('noErrorHandler', true)
                ->setParam('useDefaultControllerAlways', true)
             ->registerPlugin(new Custom_Controller_Plugin_KenAuth($auth, $acl));
 //Zend_Debug::dump($frontController->getParams(), $label=null, $echo=true);
 $frontController->throwExceptions(true);
 $frontController->dispatch();

 

ok,項目可以顯示了。

然後就可以做更多的練習。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.