Yii中单独为module加载Bootstrap或其他组件的4种方法

 更新时间:2016年11月25日 17:28  点击:1411
Bootstrap中包含了丰富的Web组件,根据这些组件,可以快速的搭建一个漂亮、功能完备的网站。 但是有时候我们网站前台并不需要Bootstrap,只要管理后台使用Bootstrap,那么该如何单独为一个module加载Bootstrap呢

这里有4中方法来实现这个:
1.在应用的配置文件中添加如下内容 (protected/config/main.php):

PHP

 代码如下 复制代码
    'modules'=>array(
        'admin'=>array(
            'preload'=>array('<span class='wp_keywordlink_affiliate'><a href="http://lxy.me/tag/bootstrap" title="查看 bootstrap 中的全部文章" target="_blank">bootstrap</a></span>'),
            'components'=>array(
                '<span class='wp_keywordlink_affiliate'><a href="http://lxy.me/tag/bootstrap" title="查看 bootstrap 中的全部文章" target="_blank">bootstrap</a></span>'=>array(
                    'class'=>'ext.bootstrap.components.Bootstrap'
            )
        ),
    // ...其他模块...
    )   

   

2.在模块初始化时加载:

 代码如下 复制代码
    public function init()
    {
        // import the module-level models and components
        $this->setImport(array(
            'admin.models.*',
            'admin.components.*',
            // 'ext.bootstrap.components.Bootstrap', // this will go to app config for components
        ));
        Yii::app()->getComponent('bootstrap');// this does the loading
    }


3.模块初始化加载的另一种方法:

 代码如下 复制代码

PHP
    public function init()
    {
        // import the module-level models and components
        $this->setImport(array(
            'admin.models.*',
            'admin.components.*',
        ));

        $this->configure(array(
                'components'=>array(
                    'bootstrap'=>array(
                        'class'=>'ext.bootstrap.components.Bootstrap'
                    )
                )
        ));
        $this->getComponent('bootstrap');
    }


4.模块加载时的另一种方法:

 代码如下 复制代码

PHP
    public function init()
    {
        // import the module-level models and components
        $this->setImport(array(
            'admin.models.*',
            'admin.components.*',
        ));

        $this->configure(array(
                'preload'=>array('bootstrap'),
                'components'=>array(
                    'bootstrap'=>array(
                        'class'=>'ext.bootstrap.components.Bootstrap'
                    )
                )
        ));
        $this->preloadComponents();
    }

本文章从我一个ajax实例中提供了段关于通过图片地地址把图片保存到本址,这里我们直接通过readfile读取然后通过fopen保存即可

实例

 代码如下 复制代码

<?php
/**
 * 通过图片的远程url,下载到本地
 * @param: $url为图片远程链接
 * @param: $filename为下载图片后保存的文件名
 */
function GrabImage($url,$filename) {
 if($url==""):return false;endif;
 
 ob_start();
 readfile($url);
 $img = ob_get_contents();
 ob_end_clean();
 $size = strlen($img);
 
 //"../../images/books/"为存储目录,$filename为文件名
 $fp2=@fopen("../../images/books/".$filename, "a");
 fwrite($fp2,$img);
 fclose($fp2);
 
 return $filename;
 }
?>

函数返回图片下载完后保存的名字,那么只需在数据库中存储 图片目录+图片名即可。

本文章来给各位使用Magento的朋友介绍怎么来获取购物车产品数量和价格并显示出来,下面我来给各位同学详细介绍。

得到购物车所有的产品信息

 代码如下 复制代码

// $items = Mage::getModel('checkout/cart')->getQuote()->getAllItems();
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();foreach($items as $item) {
    echo 'ID: '.$item->getProductId().'<br>';
    echo 'Name: '.$item->getName().'<br>';
    echo 'Sku: '.$item->getSku().'<br>';
    echo 'Quantity: '.$item->getQty().'<br>';
    echo 'Price: '.$item->getPrice().'<br>';
    echo "<br>";
}

当然,也可以快速得到当前用户的所有的item的个数和产品的所有个数!下面是magento的一个快速函数!

 代码如下 复制代码

$totalItems = Mage::getModel('checkout/cart')->getQuote()->getItemsCount();
$totalQuantity = Mage::getModel('checkout/cart')->getQuote()->getItemsQty();

下面是得到subtotal price 和grand price的方法

 代码如下 复制代码

$subTotal = Mage::getModel('checkout/cart')->getQuote()->getSubtotal();
$grandTotal = Mage::getModel('checkout/cart')->getQuote()->getGrandTotal()

这样就可以快速的得到当前购物车的详细信息,方便操作数据,显示购物车的信息!!

一般用于右上角的购物车信息居多,或者ajax购物车!

本文章来给各位朋友介绍Magento 修正来自首页的产品页面包屑导航实现方法,如果产品是从Category产品列表中进入Product详细页面,则面包屑导航中含有Category Path; 否则,当从首页,或搜索结果中,或者其他什么地方进入,则缺少之。我想,可能是Magento支持一个产品放入多个Category的缘故吧。不管怎么样,产品页中缺少了Category Path,用户体验不大好。

修正的方法,找到文件app/code/core/Mage/Catalog/Helper/Data.php

复制一份到local代码池

app/code/local/Mage/Catalog/Helper/Data.php
在函数getBreadcrumbPath的开始部分,加上如下的代码逻辑:

 

 代码如下 复制代码
getBreadcrumbPath()
    {
        if (!$this->_categoryPath) {
            $path = array();
            //add by date 2013-04-07 产品页面包屑导航修正
            if ($this->getProduct() && !$this->getCategory()) {
                $_categoryIds = $this->getProduct()->getCategoryIds();
                rsort($_categoryIds);
                if ($_categoryId = $_categoryIds[0]) {
                    $_category = Mage::getModel('catalog/category')->load($_categoryId);
                    Mage::register('current_category', $_category);
                }
            }
            //end date 2013-04-07
           
            if ($category = $this->getCategory()) {
                $pathInStore = $category->getPathInStore();
                $pathIds = array_reverse(explode(',', $pathInStore));
                $categories = $category->getParentCategories();
                // add category path breadcrumb
                foreach ($pathIds as $categoryId) {
                    if (isset($categories[$categoryId]) && $categories[$categoryId]->getName()) {
                        $path['category'.$categoryId] = array(
                            'label' => $categories[$categoryId]->getName(),
                            'link' => $this->_isCategoryLink($categoryId) ? $categories[$categoryId]->getUrl() : ''
                        );
                    }
                }
            }
            if ($this->getProduct()) {
                $path['product'] = array('label'=>$this->getProduct()->getName());
            }
            $this->_categoryPath = $path;
        }
        return $this->_categoryPath;
    }

首先判断当前是否是产品页,如果是并且没有Category信息,就获取产品所属的Category IDs,Magento中一个产品可以加入多个Category中,现在也不管那么多了,只挑出其中一个幸运的Category作为current_category

php默认时区是欧美国家的所以与我们中国时区相差了整整8小时哦,下面我来给各位介绍php设置时区方法,有需要了解的朋友可进入参考。

在 php.ini 中,默认是 date.timezone = UTC。修改为中国时区,修改为 date.timezone = PRC。如果直接写 GMT 格式的,是 date.timezone = Etc/GMT+8。

另外,也可以在 PHP 页面头中设置。

 代码如下 复制代码

date_default_timezone_set('PRC');


在PHP5中, 有很多方法可以设置或者获取默认的时区设置,例如,使用date_default_timezone_setl函数来设置时区

 代码如下 复制代码

<?php
    date_default_timezone_set("Asia/Shanghain"); //设置时区为上海
?>

或者 设置东京的时区代码为:

<?php
    date_default_timezone_set("Asia/Tokyo");
?>

系统初始化时,加上

ini_set('date.timezone','Asia/Shanghai'); 

date_default_timezone_set("PRC"); 

就将解决时区相差8


还有如果你有php.ini管理权限可直接在php.ini中修改哦


手动修改php.ini设置

打开php找到date.timezone = "PRC" 如有去掉前面的分号,没有的话手动添加!

 

装上PHP5后你会发现这样的问题:

 

 代码如下 复制代码
<?php  
$atime=date("Y-m-d H:i:s");  
echo $atime;  
?> 
<?php
$atime=date("Y-m-d H:i:s");
echo $atime;
?>

你也许会发现,输出的时间和你现在的时间是不相同的。

原因是假如你不在程序或配置文件中设置你的服务器当地时区的话,PHP所取的时间是格林威治标准时间,所以和你当地的时间会有出入。

格林威治标准时间和北京时间大概差8个小时左右 那么我们如何避免时间误差呢?

我们一起来看看解决方法:

在页头使用date_default_timezone_set()设置我的默认时区为北京时间。

 代码如下 复制代码


<?  
date_default_timezone_set('PRC');  
echo date('Y-m-d H:i:s');  
?> 


时间和服务器当前时间一样了。

如果发生插入数据库错误的话,请确保 date('Y-m-d H:i:s') 中的 H 为大写。

[!--infotagslink--]

相关文章