在CakePHP中使用swfObject

CakePHP当前版本:1.2.0.7962 Final
swfObject当前版本:2.1
具体步骤如下:

  • 下载swfObject,把swfobject.js放到 /webroot/js 目录下
  • 将下面代码保存到 /views/helpers/flash.php
    /**
     * @author Alexander Morland
     * @license MIT
     * @version 1.2
     * @modified 19. nov. 2008
     */
    class FlashHelper extends AppHelper {	
    	var $helpers = array('Javascript');
    	/**
    	 * Used for remembering options from init() to each renderSwf
    	 *
    	 * @var array
    	 */
    	var $options = array(
    		'width' => 500,
    		'height' => 500
    	);
     
    	/**
    	 * Used by renderSwf to set a flash version requirement
    	 *
    	 * @var string
    	 */
    	var $defaultVersionRequirement = '9.0.0';
     
    	/**
    	 * Used by renderSwf to only call init if it hasnt been done, either
    	 * manually or automatically by a former renderSwf()
    	 *
    	 * @var boolean
    	 */
    	var $initialized = false;
     
    	/**
    	 * Optional initializing for setting default parameters and also includes the
    	 * swf library. Should be called once, but if using several groups of flashes,
    	 * MAY be called several times, once before each group.
    	 *
    	 * @example echo $flash->init();
    	 * @example $flash->init(array('width'=>200,'height'=>100);
    	 * @return mixed String if it was not able to add the script to the view, true if it was
    	 */
    	function init($options = array()) {
    		if (!empty($options)) {
    			$this->options = am($this->options, $options);
    		}
    		$this->initialized = true;
    		$view =& ClassRegistry::getObject('view'); 
    		if (is_object($view)) { 
    			$view->addScript($this->Javascript->link('swfobject')); 
    			return true;
    		} else {
    			return $this->Javascript->link('swfobject');
    		}
    	}
     
    	/**
    	 * Wrapper for the SwfObject::embedSWF method in the vendor. This method will write a javascript code
    	 * block that calls that javascript method. If given a dom id as fourth parameter the flash will 
    	 * replace that dom object. If false is given, a div will be placed at the point in the 
    	 * page that this method is echo'ed. The last parameter is mainly used for sending in extra settings to
    	 * the embedding code, like parameters and attributes. It may also send in flashvars to the flash. 
    	 * 
    	 * For doucumentation on what options can be sent, look here:
    	 * http://code.google.com/p/swfobject/wiki/documentation
    	 *
    	 * @example echo $flash->renderSwf('counter.swf'); // size set with init();
    	 * @example echo $flash->renderSwf('flash/ad.swf',100,20);
    	 * @example echo $flash->renderSwf('swf/banner.swf',800,200,'banner_ad',array('params'=>array('wmode'=>'opaque')));
    	 * @param string $swfFile Filename (with paths relative to webroot)
    	 * @param int $width if null, will use width set by FlashHelper::init()
    	 * @param int $height if null, will use height set by FlashHelper::init()
    	 * @param mixed $divDomId false or string : dom id
    	 * @param array $options array('flashvars'=>array(),'params'=>array('wmode'=>'opaque'),'attributes'=>array());
    	 *		 See SwfObject documentation for valid options
    	 * @return string
    	 */
    	function renderSwf($swfFile, $width = null, $height = null, $divDomId = false, $options = array()) {
    		$options = am ($this->options, $options);		
    		if (is_null($width)) {
    			$width = $options['width'];
    		}
    		if (is_null($height)) {
    			$height = $options['height'];
    		}
    		$ret = '';
    		if (!$this->initialized) {
    			$init = $this->init($options);
    			if (is_string($init)) {
    				$ret = $init;
    			}
    			$this->initialized = TRUE;
    		}		
    		$flashvars = '{}';
    		$params =  '{wmode : "opaque"}';
    		$attributes = '{}';
    		if (isset($options['flashvars'])) {
    			$flashvars = $this->Javascript->object($options['flashvars']);
    		}
    		if (isset($options['params'])) {
    			$params = $this->Javascript->object($options['params']);
    		}
    		if (isset($options['attributes'])) {
    			$attributes = $this->Javascript->object($options['attributes']);
    		}
     
    		if ($divDomId === false) {
    			$divDomId = uniqid('c_');
    			$ret .= '<div id="'.$divDomId.'"></div>';
    		}
    		if (isset($options['version'])) {
    			$version = $options['version'];
    		} else {
    			$version = $this->defaultVersionRequirement;			
    		}
    		if (isset($options['install'])) {
    			$install = $options['install'];
    		} else {
    			$install =  '';			
    		}
     
    		$swfLocation = $this->webroot.$swfFile;
    		$ret .= $this->Javascript->codeBlock(
    			'swfobject.embedSWF("'.$swfLocation.'", "'.$divDomId.'", "'.$width.'", "'.$height.'", "'.$version.'","'.$install.'", '.$flashvars.', '.$params.', '.$attributes.');');
     
    		return $ret;
    	}

使用方法:

  • 例一:
    echo $flash->renderSwf('test.swf',400,200,'flashy');
    echo '<div id="flashy">没有安装flash插件</div>';
  • 例二:
    $flash->init(array('width'=>200,'height'=>100));<br />
    echo $flash->renderSwf('test1.swf');<br />
    echo $flash->renderSwf('test2swf');
  • 例三:
    echo $flash->renderSwf('flashfiles/fl_countdown_v3_3.swf?mo=1&da=24&snd=off&co=AA3333',800,250,false,array('params' => array('movie'=>'?mo=1&da=24&snd=off&co=AA3333')));

参考:FlashHelper – a wrapper for the SwfObject js class

你可能还对下面的日志感兴趣:

相关标签: Development & Design and tagged , , , .

2 Responses to 在CakePHP中使用swfObject

  1. snbzcy says:

    关于cakephp1.2中的分页,请大侠指教
    在XP下安装apache+php后,也按照cakephp教程里的要求成功加载mod_rewrite,
    但访问时不能以 http://www.example.com/controllerName/actionName的格式访问,必须使用
    http://www.example.com/index.php/controllerName/actionName 格式才行,为什么?

    另外,使用1.2中的分页时,页面首次出现如下面: http://www.example.com/controllerName/actionName/page:1

    其他分页链接地址就可能是这样: http://www.example.com/controllerName/actionName/controllerName/actionName/page:2

    或这样: http://www.example.com/controllerName/controllerName/actionName/page:2

    请大侠指明原因,万分感谢!

    • 唯枫 says:

      我也是最近才用cakephp,你这些问题我还没有遇到过,给你个建议:

      cakephp有三种安装方式,你可以参考一下这里:http://book.cakephp.org/cn/view/32

      推荐你用高级安装模式,我用的是这个,如果还解决不了,就把你的apache配置文件还有cakephp的config配置目录的文件发到我邮箱里我帮你看下。
      我的邮箱在首页右边。

      另外分页的问题可以发下你的分页代码我看下。

发表评论

电子邮件地址不会被公开。 必填项已被标记为 *

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>