📜 ⬆️ ⬇️

Yii framework code parsing

Analysis of application code written in php using Yii Framework (source code).

From the article you can make interesting pieces of code in php, as well as the subtleties and designs of the Yii Framework, it is designed to support php programmers and users of the Yii Framework.

Behavior Behavior


An example of behavior for working with images (as we know the behaviors are attached to the controller and become part of its functionality):
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(); } 

Let's take a closer look at the details; a part of the functionality of the Yii + model has been added to the behavior; general methods for working with images have been added. Suppose we have 5 controllers in which images are used, in order not to duplicate the code, we simply put the functionality into behavior. Also in the behavior, you can override some standard class functions.

Action Actions


For convenient use of code in Yii, there are Actions that are attached to the controllers.
During code generation using the gii module, many almost identical controllers are created, the functionality of which can be brought into a separate Action class.
A CRUDAction is created which is inherited from CAction, 2 attributes are defined: the name of the model and the redirect page to which the user gets after the CRUD (Create Read Update Delete) actions is completed, the model is also loaded:
 <?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 is inherited from CRUDAction, the model is dynamically created and the name of the script is set, the run method describes standard actions:
 <?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, ]); } } 

Widget

A large number of the same functionality - you can pack it into a widget and call it where necessary.
Widget class:
 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, ]); } } 

Representation:
 <?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> 

As you can see, working with such a mixture of php and html is inconvenient, and this is how the widget's call looks like:
  <?php $this->widget('Citywidget',[ 'name' => 'Biztrade[city_id]', 'city_id' => $model->city_id, 'dialogId' => 'dialogId' ]); ?> 

Probably all of the interesting, I put the source code - in it you can find more standard things. Thank you.

')

Source: https://habr.com/ru/post/280382/


All Articles