Yiiフレームワークのコード解析

Yiiフレームワークを使用してphpで記述されたアプリケーションコードの分析(ソース)。



この記事から、PHPの興味深いコード、およびYiiフレームワークの微妙さとデザインを取得できます。これは、PHPプログラマーとYiiフレームワークのユーザーをサポートするように設計されています。



行動行動



イメージを操作するための動作の例(私たちが知っているように、動作はコントローラーに接続され、その機能の一部になります):

class ImageBehavior extends CBehavior{ public $modelName = ''; const MAX_WIDTH = 450; const MAX_HEIGHT = 450; public function resize($path){ $mimeType = mime_content_type($path); $type = str_replace('image/','',$mimeType); list($width, $height) = getimagesize($path); list($newWidth, $newHeight) = $this->getNewSize($width,$height); $thumb = imagecreatetruecolor($newWidth, $newHeight); $createFunction = 'imagecreatefrom' . $type; $source = $createFunction($path); imagecopyresized($thumb, $source, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); $imageFunction = 'image' . $type; ob_start(); header('Content-Type: ' . $mimeType); $imageFunction($thumb,null,100); $img = ob_get_contents(); @unlink($path); @file_put_contents($path,$img); } public function getNewSize($width,$height){ $wK = $width/$height; $hK = $height/$width; $newWidth = $width; $newHeight = $height; if($width > self::MAX_WIDTH && ($width > $height)){ $newWidth = self::MAX_WIDTH; $newHeight = $newWidth/$wK; } if($height > self::MAX_HEIGHT && ($height > $width)){ $newHeight = self::MAX_HEIGHT; $newWidth = $newHeight/$hK; } return array($newWidth,$newHeight); } public function columnExists($column){ $modelName = $this->modelName; $model = $modelName::model(); $table = Yii::app()->getDb()->getSchema()->getTable($model->tableName()); $columnNames = $table->getColumnNames(); return in_array($column,$columnNames); } public function deleteImage(){ if($this->columnExists('image_path')){ $path = $this->image_path; $fullPath = Yii::getPathOfAlias('webroot') . $path; if(file_exists($fullPath)) @unlink($fullPath); } } public function getModelName(){ return get_called_class(); }
      
      





よく見てみましょう、Yii +モデルの機能の一部が動作に追加され、画像を操作するための一般的な方法が追加されます。 コードを複製しないために、機能を動作させるために、イメージが使用される5つのコントローラーがあるとします。 また、動作では、クラスの標準関数の一部をオーバーライドできます。



アクションアクション



Yiiでコードを便利に再利用するために、コントローラーにアタッチされるアクションがあります。

giiモジュールを使用したコード生成中に、ほとんど同じコントローラーが多数作成され、それらの機能は別のActionクラスに移動できます。

CActionを継承するCRUDActionが作成され、2つの属性が定義されます。CRUDアクション(作成、更新、削除、削除)の完了後にユーザーが着陸するモデル名とリダイレクトページです。モデルもロードされます。

 <?php class CRUDAction extends CAction{ public $modelName; public $redirectTo = ['index']; public function loadModel($id){ $m = $this->modelName; $model = $m::model()->findByPk($id); if($model===null) throw new CHttpException(404,'The requested page does not exist.'); return $model; } }
      
      





CreateAction-CRUDActionから継承され、モデルが動的に作成され、スクリプトの名前が設定されます。標準メソッドはrunメソッドで説明されます。

 <?php Yii::import('application.components.actions.CRUDAction'); class CreateAction extends CRUDAction{ public function run(){ $model = new $this->modelName; $model->scenario = 'create'; if(isset($_POST[$this->modelName])){ $model->attributes = $_POST[$this->modelName]; if($model->save()) $this->getController()->redirect($this->redirectTo); } $renderPath = strtolower("create"); $this->getController()->render($renderPath,[ 'model' => $model, ]); } }
      
      





ウィジェット


多数の同じ機能-ウィジェットにパックして、必要な場所で呼び出すことができます。

ウィジェットクラス:

 class Citywidget extends CWidget{ public $cols = 10; public $name; public $dialogId; public $city_id; public $defaultName = ''; public function init(){ parent::init(); } public function run(){ $this->render('citywidget',[ 'name' => $this->name, 'dialogId' => $this->dialogId, 'city_id' => $this->city_id, 'cols' => $this->cols, 'defaultName' => $this->defaultName, ]); } }
      
      





性能:

 <?php $this->beginWidget('zii.widgets.jui.CJuiDialog',array( 'id' => $dialogId, // additional javascript options for the dialog plugin 'options'=> [ 'title' => '', 'autoOpen'=>false, 'width' => '800px' ], 'htmlOptions' => [ 'width' => '800px' ] )); ?> <?php $cities = Cities::getCities(); $count = count($cities); $perCol = round($count/$cols); if($city_id == 0){ $city_id = CityChooser::$cityId; } $cityName = Cities::getCityNameByPk($city_id,$defaultName); ?> <style type="text/css"> #<?=$dialogId?>{ display:none; } #<?=$dialogId?> ul { float:left; clear:right; padding:5px; } #<?=$dialogId?> li { float:left; clear:left; } </style> <script type="text/javascript"> function changeCity<?=$dialogId?>(id,cityName,dialogId,inName){ $("#href-"+dialogId+"").text(cityName); $("input[name*='"+inName+"']").val(id); $("#"+dialogId).dialog("close"); if(dialogId == 'chooserDialogId'){ $.get( "/api/setcity/id/"+id, function( data ) { }); } //$("#"+dialogId).dialog("close"); } </script> <?='<ul>'?> <?php $i=0; ?> <?php foreach ($cities as $city):?> <?php if($i>=$perCol):?> <?='</ul><ul>'?> <?php $i=0; ?> <?php endif;?> <li style="margin-top:4px;"> <a href="#" style="margin:8px;color: #428bca;" onClick="changeCity<?=$dialogId?>('<?=$city->id?>','<?=$city->name?>','<?=$dialogId?>','<?=$name?>')"> <?php if((int)$city->sort !== 0):?> <b><?=$city->name?></b> <?php else:?> <?=$city->name?> <?php endif;?> </a> </li> <?php if($i>=$perCol):?> <?='</ul>'?> <?php endif;?> <?php $i++; ?> <?php endforeach;?> <?='</ul>'?> <?php $this->endWidget('zii.widgets.jui.CJuiDialog');?> <input type="hidden" name="<?=$name?>" value="<?=$city_id?>"> <a href="#" id="href-<?=$dialogId?>" onclick='$("#<?=$dialogId?>").dialog("open"); return false;'><?= $cityName?></a>
      
      





ご覧のように、このようなphpとhtmlの混合で作業するのは不便ですが、これはウィジェット呼び出しの外観です:

  <?php $this->widget('Citywidget',[ 'name' => 'Biztrade[city_id]', 'city_id' => $model->city_id, 'dialogId' => 'dialogId' ]); ?>
      
      





おそらくすべてがおもしろいので、 ソースコードを添付しましたが、その中にもっと標準的なものがあります。



All Articles