Hiển thị các bài đăng có nhãn yii. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn yii. Hiển thị tất cả bài đăng

Thứ Bảy, 27 tháng 4, 2013

Yii ~ set baseUrl variable in every view, override render function



應該也有人很懶得打

Yii::app() -> request -> baseUrl;

整個就是又臭又長

所以我在Controller.php裡面放了這段

$this -> baseUrl = Yii::app() -> request -> baseUrl;

這樣要baseUrl的時候只要打$this->baseUrl

可是還是覺得很煩...........

因為我比較想打$baseUrl就好

可是這樣在每個view的檔案裏面就要加這段

$baseUrl =  Yii::app() -> request -> baseUrl;

麻煩死0rz

所以乾脆就複寫他原本的render function

讓他每次把data丟進去的時候順便把$baseUrl給設定進去


原本CController.php的render

public function render($view,$data=null,$return=false)
{
if($this->beforeRender($view))
{
$output=$this->renderPartial($view,$data,true);
if(($layoutFile=$this->getLayoutFile($this->layout))!==false)
$output=$this->renderFile($layoutFile,array('content'=>$output),true);

$this->afterRender($view,$output);

$output=$this->processOutput($output);

if($return)
return $output;
else
echo $output;
}
}


在Controller.php裡面加這段function去override

//override
public function render($view, $data = null, $return = false) {
if ($this -> beforeRender($view)) {

$data['baseUrl'] = Yii::app() -> baseUrl;

$output = $this -> renderPartial($view, $data, true);
if (($layoutFile = $this -> getLayoutFile($this -> layout)) !== false)
$output = $this -> renderFile($layoutFile, array('content' => $output), true);

$this -> afterRender($view, $output);

$output = $this -> processOutput($output);

if ($return)
return $output;
else
echo $output;
}
}


Thứ Hai, 24 tháng 9, 2012

Yii ~ use CKEditor

到這裡下載ckeditor-integration
http://www.yiiframework.com/extension/ckeditor-integration/

解壓縮丟到extensions裡

然後下載ckeditor丟到root

http://ckeditor.com/download

像我就是丟到yii/xxxxx/底下


在View裡面使用ckeditor的sample code

$this -> widget('ext.ckeditor.CKEditorWidget', array("model" => new Event,
"attribute" => 'content',
"defaultValue" => $item['content'],
"config" => array("height" => "400px", "width" => "80%", "toolbar" => "Full")));

Yii ~ use Upload class

想當初做網站處理圖片的部分

我都是包Upload這個class來做圖片的處理

http://www.verot.net/php_class_upload_docs.htm

現在用yii的framework

想不到也可以用Photobucket


這邊下載
http://www.yiiframework.com/extension/upload/

安裝方法就是整包丟進extension

然後這邊是我的sample code

        Yii::import('application.extensions.upload.Upload');
        // receive file from post
        if (isset($_FILES['pic'])) {
            $Upload = new Upload($_FILES['pic']);

            $Upload -> jpeg_quality = 100;
            $Upload -> no_script = false;
            $Upload -> image_resize = true;
            $Upload -> image_x = 700;
            $Upload -> image_y = 500;
            $Upload -> image_ratio = true;

            // some vars
            $destPath = Yii::app() -> getBasePath() . '/../img/origin/' . $this -> id . '/';
            $destName = $item -> id;

            // verify if was uploaded
            //origin img
            if ($Upload -> uploaded) {
                $Upload -> file_new_name_body = $item -> id;
                $Upload -> file_new_name_ext = 'png';
                $Upload -> file_auto_rename = false;
                $Upload -> file_overwrite = true;
                $Upload -> process($destPath);
            }
            //thumb
            $destPath = Yii::app() -> getBasePath() . '/../img/thumb/' . $this -> id . '/';
            if ($Upload -> uploaded) {
                $Upload -> file_new_name_body = $item -> id;
                $Upload -> file_new_name_ext = 'png';
                $Upload -> file_auto_rename = false;
                $Upload -> file_overwrite = true;
                $Upload -> image_resize = true;
                $Upload -> image_x = 120;
                $Upload -> image_y = 180;
                $Upload -> image_ratio = true;
                $Upload -> process($destPath);
            }

        }


Thứ Bảy, 22 tháng 9, 2012

Yii ~ load config

'params'=>array(
// this is used in contact page
'adminEmail'=>'webmaster@example.com',
'myParam'=>'myValue',
),


$param=Yii::app()->params['myParam'];


reference
http://www.yiiframework.com/forum/index.php/topic/10559-how-to-load-configuration-set-in-configmainphp/

Thứ Năm, 6 tháng 9, 2012

Thứ Hai, 3 tháng 9, 2012

yii ~ findAll example

$model=Auction::model()->findAll(array(
'condition'=>'status=:status AND starttime >= :date',
'params'=>array(':status'=>1, ':date'=>$date),
));


reference
http://stackoverflow.com/questions/7314284/yii-findbyattributes-with-greater-than-attribute

Thứ Năm, 30 tháng 8, 2012

yii ~ urlManager rules example

之前做產品頁面我的網址都是透過apache設定成

product/xbox360

或是

product/ps3

作法是

RewriteRule ^product/([a-zA-Z0-9_-]+)/?$ product.php?urlName=$1 [L]


現在用yii

如果網址打product一定會是連到Product這個Controller的actionIndex

就是product/index

如果要加參數就是

product/index/id/12345678

這樣就跟我想像的不一樣.........因為照這規則網址勢必會變類似product/index/id/12345678

這時候就要去config裡改urlManager的rules


我這邊rules是新增這段

'product/<id>'=>'product/index',


ProductController的actionIndex

public function actionIndex() {
$id = Yii::app() -> getRequest() -> getQuery('id');
//do something
$this -> render('index');
}



這樣網址打

product/1234567

她就自動變成product/index/id/12345


reference
http://stackoverflow.com/questions/2760869/yii-framework-controller-action-url-parameters

yii ~ get url parameters

$id = Yii::app()->getRequest()->getQuery('id');


reference
http://stackoverflow.com/questions/2760869/yii-framework-controller-action-url-parameters

Chủ Nhật, 26 tháng 8, 2012

yii ~ model relation get value example

product_internet_recommend

裡面有個productID是外來建為Product的id


models/ProductInternetRecommend.php

public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'product' => array(self::BELONGS_TO, 'Product', 'productID'),
);
}


in controller

$productInternet = ProductInternetRecommend::model() ->with('product')-> findByPk(1);


in view

echo 'asd'.$productInternet->product->price;



reference
http://stackoverflow.com/questions/9857209/yii-relations-error-trying-to-get-property-of-non-object

http://www.yiiframework.com/doc/guide/1.1/en/database.arr