牢骚两句:马上过年了从事Web PHP方向的开发又一年了 到现在为止也小两年了,基本的也都说的过去但是系统的学习还没有进行过。要想晋级必须功底扎实同时有拿得出手的绝活。忘了从哪里听来的了毕业五年的状况基 本上决定了一个人的职业生涯的情况。所以在断断续续的浮躁,冲动,堕落中我该做点事情了,公司里面有自己的框架所以对zendframework的学习只 是在培训的时候粗略的过了一边。居安思危,一定不能松懈,特别是现在跳槽的情况下zend 还是一个高级的必备的一项技能。所以就重新拾起zendframework。
现在网上的配置都是smarty2+ zend1.0x的 教程都不能用了 所以就自己整理了下一个国外的blog的一个配置 这样翻译过来 方便大家使用 水平有限请多见谅
另源码包已经打包上传:里面包括了lib库 和配置文件 昨天自己玩zend 的时候怎么都无法访问其他的action 后来发现是apache 的 allowOveride none 没有更改 这里小提醒下
zend的环境要求 rewrite 模块 http.conf 中 AllowOverride None 要改成AllowOverride All php 的pdo_mysql 要开启 至于怎么开启 这个就不在这里说了 谷歌下就可可以找到很多 刚开始我用百度 全部是一篇文章 蛋疼………………….
原文地址:http://gediminasm.org/article/smarty-3-extension-for-zend-framework#action
译文:
Zend 框架延伸 Smarty 3 使用
Smarty 3 和 zend 的扩展很容易整合,并且完全兼容视图和布局模版支持的所有标准功能,如模块和查看 AJAX,JSON,XML。 与现有的smarty 3 的功能没有冲突
特点:
标准规则的布局和视图渲染
每个模块的静态模版路径
所有的帮助支持,包括ajax,json xml 上下文
第一步:下载相应的库文件 Smarty 3 + zend 1.11.+
初始化zendFramework后的默认目录结构应该是(记住新的zendFramework官方的入口在myapp/public/index.php)
- …
- /myapp
- /application
- /configs
- application.ini
- /modules
- Bootstrap.php
- /library
- /Smarty
- /plugins
- /sysplugins
- /debug.tpl
- /Smarty.class.php
- /public
- …
配置文件的内容如下
- [production]
- phpSettings.display_startup_errors = 0
- phpSettings.display_errors = 0
- bootstrap.path = APPLICATION_PATH “/Bootstrap.php”
- bootstrap.class = “Bootstrap”
- resources.frontController.moduleDirectory = APPLICATION_PATH “/modules”
- resources.frontController.moduleDefault = “default”
- resources.layout.layout = “layout”
- resources.view[] =
- ; — Autoloading Prefixes —
- autoloaderNamespaces.extension[] = “Ext_”
- ; — Smarty —
- smarty.caching = 1
- smarty.cache_lifetime = 14400 ; 4 hours
- smarty.template_dir = APPLICATION_PATH “/templates/”
- smarty.compile_dir = APPLICATION_PATH “/tmp/smarty_compile/”
- smarty.config_dir = “”
- smarty.cache_dir = APPLICATION_PATH “/tmp/smarty_cache/”
- smarty.left_delimiter = “{“
- smarty.right_delimiter = “}”
- [staging : production]
- [testing : production]
- phpSettings.display_startup_errors = 1
- phpSettings.display_errors = 1
- [development : production]
- phpSettings.display_startup_errors = 1
- phpSettings.display_errors = 1
- resources.frontController.params.displayExceptions = 1
- smarty.caching = 0
这时候的目录结构应该是这个样子
- …
- /myapp
- /application
- /configs
- application.ini
- /modules
- /default
- /controllers
- …
- /views
- …
- /tmp
- Bootstrap.php
- /library
- /Smarty
- /plugins
- /sysplugins
- /debug.tpl
- /Smarty.class.php
- /Ext
- /public
- …
最重要的一步 创建smarty.php 载入文件
目录地址myapp/library/Ext/View/Smarty.php
内容如下
- /**
- * Smarty template engine integration into Zend Framework
- * Some ideas borrowed from http://devzone.zend.com/article/120
- */
- class Ext_View_Smarty extends Zend_View_Abstract
- {
- /**
- * Instance of Smarty
- * @var Smarty
- */
- protected $_smarty = null;
- /**
- * Template explicitly set to render in this view
- * @var string
- */
- protected $_customTemplate = ”;
- /**
- * Smarty config
- * @var array
- */
- private $_config = null;
- /**
- * Class definition and constructor
- *
- * Let’s start with the class definition and the constructor part. My class Travello_View_Smarty is extending the Zend_View_Abstract class. In the constructor the parent constructor from Zend_View_Abstract is called first. After that a Smarty object is instantiated, configured and stored in a private attribute.
- * Please note that I use a configuration object from the object store to get the configuration data for Smarty.
- *
- * @param array $smartyConfig
- * @param array $config
- */
- public function __construct($smartyConfig, $config = array())
- {
- $this->_config = $smartyConfig;
- parent::__construct($config);
- $this->_loadSmarty();
- }
- /**
- * Return the template engine object
- *
- * @return Smarty
- */
- public function getEngine()
- {
- return $this->_smarty;
- }
- /**
- * Implement _run() method
- *
- * The method _run() is the only method that needs to be implemented in any subclass of Zend_View_Abstract. It is called automatically within the render() method. My implementation just uses the display() method from Smarty to generate and output the template.
- *
- * @param string $template
- */
- protected function _run()
- {
- $file = func_num_args() > 0 && file_exists(func_get_arg(0)) ? func_get_arg(0) : ”;
- if ($this->_customTemplate || $file) {
- $template = $this->_customTemplate;
- if (!$template) {
- $template = $file;
- }
- $this->_smarty->display($template);
- } else {
- throw new Zend_View_Exception(‘Cannot render view without any template being assigned or file does not exist’);
- }
- }
- /**
- * Overwrite assign() method
- *
- * The next part is an overwrite of the assign() method from Zend_View_Abstract, which works in a similar way. The big difference is that the values are assigned to the Smarty object and not to the $this->_vars variables array of Zend_View_Abstract.
- *
- * @param string|array $var
- * @return Ext_View_Smarty
- */
- public function assign($var, $value = null)
- {
- if (is_string($var)) {
- $this->_smarty->assign($var, $value);
- } elseif (is_array($var)) {
- foreach ($var as $key => $value) {
- $this->assign($key, $value);
- }
- } else {
- throw new Zend_View_Exception(‘assign() expects a string or array, got ‘.gettype($var));
- }
- return $this;
- }
- /**
- * Overwrite escape() method
- *
- * The next part is an overwrite of the escape() method from Zend_View_Abstract. It works both for string and array values and also uses the escape() method from the Zend_View_Abstract. The advantage of this is that I don’t have to care about each value of an array to get properly escaped.
- *
- * @param mixed $var
- * @return mixed
- */
- public function escape($var)
- {
- if (is_string($var)) {
- return parent::escape($var);
- } elseif (is_array($var)) {
- foreach ($var as $key => $val) {
- $var[$key] = $this->escape($val);
- }
- }
- return $var;
- }
- /**
- * Print the output
- *
- * The next method output() is a wrapper on the render() method from Zend_View_Abstract. It just sets some headers before printing the output.
- *
- * @param <type> $name
- */
- public function output($name)
- {
- header(“Expires: Mon, 26 Jul 1997 05:00:00 GMT”);
- header(“Cache-Control: no-cache”);
- header(“Pragma: no-cache”);
- header(“Cache-Control: post-check=0, pre-check=0”, false);
- print parent::render($name);
- }
- /**
- * Use Smarty caching
- *
- * The last two methods were created to simply integrate the Smarty caching mechanism in the View class. With the first one you can check for cached template and with the second one you can set the caching on or of.
- *
- * @param string $template
- * @return bool
- */
- public function isCached($template)
- {
- return $this->_smarty->is_cached($template);
- }
- /**
- * Enable/disable caching
- *
- * @param bool $caching
- * @return Ext_View_Smarty
- */
- public function setCaching($caching)
- {
- $this->_smarty->caching = $caching;
- return $this;
- }
- /**
- * Template getter (return file path)
- * @return string
- */
- public function getTemplate()
- {
- return $this->_customTemplate;
- }
- /**
- * Template filename setter
- * @param string
- * @return Ext_View_Smarty
- */
- public function setTemplate($tpl)
- {
- $this->_customTemplate = $tpl;
- return $this;
- }
- /**
- * Magic setter for Zend_View compatibility. Performs assign()
- *
- * @param string $key
- * @param mixed $val
- */
- public function __set($key, $val)
- {
- $this->assign($key, $val);
- }
- /**
- * Magic getter for Zend_View compatibility. Retrieves template var
- *
- * @param string $key
- * @return mixed
- */
- public function __get($key)
- {
- return $this->_smarty->getTemplateVars($key);
- }
- /**
- * Magic getter for Zend_View compatibility. Removes template var
- *
- * @see View/Zend_View_Abstract::__unset()
- * @param string $key
- */
- public function __unset($key)
- {
- $this->_smarty->clearAssign($key);
- }
- /**
- * Allows testing with empty() and isset() to work
- * Zend_View compatibility. Checks template var for existance
- *
- * @param string $key
- * @return boolean
- */
- public function __isset($key)
- {
- return (null !== $this->_smarty->getTemplateVars($key));
- }
- /**
- * Zend_View compatibility. Retrieves all template vars
- *
- * @see Zend_View_Abstract::getVars()
- * @return array
- */
- public function getVars()
- {
- return $this->_smarty->getTemplateVars();
- }
- /**
- * Updates Smarty’s template_dir field with new value
- *
- * @param string $dir
- * @return Ext_View_Smarty
- */
- public function setTemplateDir($dir)
- {
- $this->_smarty->setTemplateDir($dir);
- return $this;
- }
- /**
- * Adds another Smarty template_dir to scan for templates
- *
- * @param string $dir
- * @return Ext_View_Smarty
- */
- public function addTemplateDir($dir)
- {
- $this->_smarty->addTemplateDir($dir);
- return $this;
- }
- /**
- * Adds another Smarty plugin directory to scan for plugins
- *
- * @param string $dir
- * @return Ext_View_Smarty
- */
- public function addPluginDir($dir)
- {
- $this->_smarty->addPluginsDir($dir);
- return $this;
- }
- /**
- * Zend_View compatibility. Removes all template vars
- *
- * @see View/Zend_View_Abstract::clearVars()
- * @return Ext_View_Smarty
- */
- public function clearVars()
- {
- $this->_smarty->clearAllAssign();
- $this->assign(‘this’, $this);
- return $this;
- }
- /**
- * Zend_View compatibility. Add the templates dir
- *
- * @see View/Zend_View_Abstract::addBasePath()
- * @return Ext_View_Smarty
- */
- public function addBasePath($path, $classPrefix = ‘Zend_View’)
- {
- parent::addBasePath($path, $classPrefix);
- $this->addScriptPath($path . ‘/templates’);
- $this->addTemplateDir($path . ‘/templates/static’);
- return $this;
- }
- /**
- * Zend_View compatibility. Set the templates dir instead of scripts
- *
- * @see View/Zend_View_Abstract::setBasePath()
- * @return Ext_View_Smarty
- */
- public function setBasePath($path, $classPrefix = ‘Zend_View’)
- {
- parent::setBasePath($path, $classPrefix);
- $this->setScriptPath($path . ‘/templates’);
- $this->addTemplateDir($path . ‘/templates/static’);
- return $this;
- }
- /**
- * Magic clone method, on clone create diferent smarty object
- */
- public function __clone() {
- $this->_loadSmarty();
- }
- /**
- * Initializes the smarty and populates config params
- *
- * @throws Zend_View_Exception
- * @return void
- */
- private function _loadSmarty()
- {
- if (!class_exists(‘Smarty’, true)) {
- require_once ‘Smarty/Smarty.class.php’;
- }
- $this->_smarty = new Smarty();
- if ($this->_config === null) {
- throw new Zend_View_Exception(“Could not locate Smarty config – node ‘smarty’ not found”);
- }
- $this->_smarty->caching = $this->_config[‘caching’];
- $this->_smarty->cache_lifetime = $this->_config[‘cache_lifetime’];
- $this->_smarty->template_dir = $this->_config[‘template_dir’];
- $this->_smarty->compile_dir = $this->_config[‘compile_dir’];
- $this->_smarty->config_dir = $this->_config[‘config_dir’];
- $this->_smarty->cache_dir = $this->_config[‘cache_dir’];
- $this->_smarty->left_delimiter = $this->_config[‘left_delimiter’];
- $this->_smarty->right_delimiter = $this->_config[‘right_delimiter’];
- $this->assign(‘this’, $this);
- }
- }
现在目录结构是这个样子
- …
- /myapp
- …
- /library
- /Smarty
- /plugins
- /sysplugins
- /debug.tpl
- /Smarty.class.php
- /Ext
- /View
- Smarty.php
- /public
- …
BootStrap文件的内容如下
myapp/application/Bootstrap.php
- class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
- {
- /**
- * Bootstrap Smarty view
- */
- protected function _initView()
- {
- // initialize smarty view
- $view = new Ext_View_Smarty($this->getOption(‘smarty’));
- // setup viewRenderer with suffix and view
- $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(‘ViewRenderer’);
- $viewRenderer->setViewSuffix(‘tpl’);
- $viewRenderer->setView($view);
- // ensure we have layout bootstraped
- $this->bootstrap(‘layout’);
- // set the tpl suffix to layout also
- $layout = Zend_Layout::getMvcInstance();
- $layout->setViewSuffix(‘tpl’);
- return $view;
- }
- }
现在目录结构
- …
- /myapp
- /application
- /configs
- application.ini
- /modules
- /default
- /controllers
- IndexController.php
- ErrorController.php
- /views
- /templates
- /error
- error.tpl
- /index
- index.tpl
- /static
- header.tpl
- layout.tpl
- Bootstrap.php
- …
indexController的内容
myapp/application/controllers/IndexController.php
- <?php
- class IndexController extends Zend_Controller_Action
- {
- public function indexAction()
- {
- $this->view->hello = ‘Hello Smarty 3’;
- }
- }
创建index/index.tpl
myapp/application/view/templates/index/index.tpl
- <!DOCTYPE p PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
- <html>
- <head>
- <meta http-equiv=”Content-Type” content=”text/html;charset=utf-8;”>
- </head>
- <body>
- <p>{$hello}</p>
- <p>dont forget to visit <a target=”_blank” href=”http://blog.csdn.net/JustBeBetter”>代码顽主BLOG</a>
- </p><p><a href=”{$this->url([‘controller’ => ‘index’, ‘action’ => ‘index’])}”>home</a></p>
- </body>
- </html>
创建error/error.tpl
myapp/application/view/templates/error/error.tpl
- <meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>
- <title>Zend Framework Default Application</title>
- <h1>An error occurred</h1>
- <h2>{$this->message}</h2>
- {if $this->exception}
- <h3>Exception information:</h3>
- <p>
- <b>Message:</b> {$this->exception->getMessage()}
- </p>
- <h3>Stack trace:</h3>
- <pre>{$this->exception->getTraceAsString()}
- </pre>
- <h3>Request Parameters:</h3>
- <pre>{var_export($this->request->getParams(), true)}
- </pre>
- {/if}
创建 头部文件
myapp/application/view/templates/static/header.tpl
- <div id=”header”>
- {include file=”header.tpl”}
- </div>
- {$this->layout()->content}
创建头文件
- header
现在你可以访问你的网站了 对了 入口在publi目录下哦
在上班所以有些粗糙:如果有看不懂的请参考原文
下面是我试验过的例子的源代码 下载后直接使用就可以
I know this if off topic but I’m looking into starting my own weblog and was wondering what all is needed to get set up? I’m assuming having
a blog like yours would cost a pretty penny? I’m not very web smart so I’m not 100% certain.
Any recommendations or advice would be greatly
appreciated. Thank you
Nice post. I was checking continuously this blog and
I’m inspired! Very helpful info particularly the last section :) I care for such info a lot. I was looking for this particular information for a long time. Thanks and best of luck.
I read this article completely about the resemblance of most recent and earlier technologies, it’s remarkable article.
Wow, wonderful weblog format! How lengthy have you been
blogging for? you make blogging glance easy.
The full glance of your web site is fantastic, let alone the content!
Heya I’m for the first time here. I came across this board and I find It really useful & it helped me out much. I hope to give something back and help others like you aided me.
The article provides established useful to us. It’s quite
informative and you are naturally very knowledgeable in this region.
You possess popped our eyes in order to varying thoughts about this topic with interesting and solid articles.
Very nice post. I just stumbled upon your blog and wished to say that I’ve really enjoyed surfing around your blog posts. After all I’ll be subscribing to your
rss feed and I hope you write again very soon!
Tremendous things here. I am very satisfied to look your article.
Thank you a lot and I am looking ahead to contact you. Will
you kindly drop me a mail?
It’s truly very difficult in this full of activity life to listen news on Television, so I only use the web for that purpose, and take the latest news.
感谢博主的分享,清闲的时候看看还是不错的,希望多多更新。
欢迎在此的光临。。。我会一直更新下去的。。。