Yiiãã¬ã¼ã ã¯ã¼ã¯ã§ãã£ã¨ç解ãããMVCã®è©±
2011å¹´å ã«æ¸ããããªãã£ããã©ãã¯ããã¯ã§ããããã¾ãã¦ããã§ã¨ããããã¾ããã
PHPã®ã¢ããã³ãã«ã¬ã³ãã¼ã« Ruby on Rails 㨠CakePHP 㨠Django 㨠Symfony2(*1.x ã¨ã¯å¥ç©ãªã®ã§2ã¨æè¨) ã®ç¹é·ããã¾ãã¾ã¨ã¾ã£ã¦ãããã¨ã³ããªãæ¸ããã¦ãã¾ããã
フレームワークで語るMVCの話 : PHP Advent Calendar #19 - basuke の日記
ã§ãYii ããã¿ã«å ãã¦ãåæã«è¿½ã£ããããã¨æãã¾ããYii ãé¡æã«ãã¾ãããã ããã¿ã㪠Yii ã使ãã¨ãã話ã§ã¯ãªããMVCãã«ã¹ã¿ãã¯ãã¬ã¼ã ã¯ã¼ã¯ã¯ Yii ããå¦ã¶ãã¨ããã£ã±ãããã¨ãã話ã§ãã
Yii ã®ç¬¬ä¸å°è±¡ã¯ãããªãã¸ã§ã¯ãæåç㪠CakePHP ã§ãããconfig ãã©ã«ã以ä¸ã®ãã¡ã¤ã«ã« array ã§è¨å®ãæ¸ãã¦ãmodels 㨠controllers 㨠views ã«ãããããã¬ã¼ã ã¯ã¼ã¯ã®åºåºã¯ã©ã¹ããæ´¾çãã php ãæ¸ãããã¥ã¼ã¯phpã³ã¼ãã§æ¸ãã®ããã£ã¨ãæ¸ãããããã¢ããªã±ã¼ã·ã§ã³å ¨ä½ã«å ±éããç¬èªã®ç¹æ§ã¯ããã¼ã¹ã¯ã©ã¹ã§ãã¡ã©å ±éãªæ¯ãèããå®è£ ãã¦ããããå®éã®MãCã«æ´¾çããããã«ããæµåãDBã«ãã¼ãã«ãããã§ã¹ãã£ãã©ã«ããã§ããã
PHPã®éçãªãã¸ã§ã¯ãæåã£ã½ããå°é
ã½ã¼ã¹ããã£ã¨è¦ã¦ãæåã« CakePHP ã¨å¤§ããéã£ã¦è¦ããã®ã¯ã
var $belongsTo = array(...);
ã¨æ¸ãã¨ããã
public function relation() { return array(...); }
ã¿ãããªå½¢ã«ãªã£ã¦ããã®ãã大ããéã£ã¦è¦ããã¨ããã§ãã
ãªããã£ã¼ã«ãã§ã¯ãªãã¡ã½ãããªã®ã
PHPã¯Rubyããã¾ã ããå°ããIDEã§ã®éç解æãã§ããè¨èªã§ãããªã®ã§ããªã¼ãã¼ã©ã¤ãã§ããã°ã³ã¼ãã®è£å®ãå¹ãããããã¾ãããèªç±ãªãã£ã¼ã«ããå®ç¾©ãã¦ã«ã¹ã¿ãã¤ãºããæ¹æ³ã ã¨ãã³ã¼ãè¨è¿°éãã®ãã®ã¯æ¸ãã¾ãããææ³ããã¹é²æ¢ã®å©ãã«ãªããªãã§ããã
ã¾ãYiiã§ã¯ãActiveRecordãçæããã¨ããã£ã¼ã«ãåãã¯ã©ã¹ã®docã³ã¡ã³ãã«ããããã¨æ¸ããã¾ããã©ããããã®ãã__getã__setã使ã£ã¦ããã¨ãæ®éãã£ã¼ã«ãåãè£å®ã§ãã¾ããããããªãã¨ãEclipseã®PDTã¯docã³ã¡ã³ãã«æ¸ããããã£ã¼ã«ãåããã³ã¼ãã£ã³ã°ä¸ã«åè£ã¨ãã¦åºãã¦ããã¾ããNetBeansã¨PHPStormãåããã¨ããã¦ããã¾ãã
/** * @property $hoge */ class Fuga extends CActiveRecord { } $fuga = new Fuga; $fuga->h //ããã§hogeãåè£ã«åºã!
æ°ãããã¦ã¾ããããããããé æ ®ã
ãã¶ãRailsã¯ãRubyãIDEã®è£å©ããããã¦åãã«ããè¨èªã ã¨ãããã¨ã«éãç´ã£ã¦ããã®ä»£ãããã³ã¼ãèªä½ã極端ã«çããªãããã«ããã ã¨æãã¾ãã
ã¨ããããããªè¨èªã®ç¹å¾´ããæ¥ãã³ã³ã»ããã ã¨ãä»ã®ä¼¼ã¦ãªãè¨èªã«å¼ãç¶ããã¨ãã¦ããã¡ãã£ã¨æªãã§ãã¾ãæ°ããããã§ãããããããã対象ã®è¨èªã®ç¹æ§ãéæã«åã£ã¦ããã®è¨èªç¬èªã®çãæ´»ãããã¨ããã®ããè¯ãã©ã¤ãã©ãªAPIã ã¨æãã¾ããJavaã¨Rubyãåèã«ããã®ã¯ãããã©ãããã ãããJavaãRubyã§ãã£ã¦ããã°ãã話ã§ãPHPã ããããã®å³ä»ãã欲ãããã¿ãããªã
Yii ã«ã¯ PHP ã ããããããããã¹ããã¨ããèããæ£æ»æ³ã§æ¼ãé²ãã¦ãæãããã£ã¦ããã®ç¬ç¹ãªèãæ¹ãè¦ããã³ã«ããªããããªã®ãé¡ã£ã¦èããã¨ãããããåèã«ãªãã¾ããã¾ããã©ã®æåååºèº«ã®PHPerããè¦ã¦ãã第ä¸å°è±¡ã¯ã¡ãã£ã¨å¤ãªåã«è¦ãã¦æãã¦ããã§ããã©ãã
ãã¦è§¦ãã¯ãããªæãã§ãä¸çªè¨ãããã£ãã®ã¯ã¤ãã
ã¤ãã³ãé§åã®ã¢ãã«:ããè¯ãMVCã¸ã®èªå°
フレームワークで語るMVCの話 : PHP Advent Calendar #19 - basuke の日記
ãRailsã®ç½ªã
ã³ã³ããã¼ã©ãéè¦è¦ããããã¨ã¨ã¯é¢ä¿ãªãã¯ãã§ããããããã³ã³ããã¼ã©ã«ãã¸ãã¯ãçªã£è¾¼ã¿ãã¡ãªã®ãåå¿è ã«è¦åããããç¹å¾´ã§ããã³ã³ããã¼ã©ã太ãããã¨ã¯ãä¸çªãã£ã¡ããããªããã¨ãããã®çç±ããã¯ããã«ã³ã³ããã¼ã©ãããã§å§ã¾ã£ã¦ãã¾ããã¨ã«èµ·å ãã¦ãã®ã§ã¯ãç½®ãå ´æã«å°ã£ãã³ã¼ããã³ã³ããã¼ã©ã«ããã¦ãã¾ãã¨ãããã¨ã ã¨æãã¾ãã
ãããããã¢ãã«ï¼ActiveRecordã¨ãã誤解ãåºããã®ãRailsã§ããMVCã®ã¢ãã«ã¯ããã¼ã¿ãã¼ã¹ã¢ã¯ã»ã¹ã§å¾ãããã¨ã³ãã£ãã£ã®ç®¡çã ãã§ã¯ããã¾ããããã¸ãã¹ãã¸ãã¯ã¨å¼ã°ãããããããå¦çãªã©ãã¢ãã«ã«å±ãããã®ã§ãããããã¯ããã¼ã¿ãã¼ã¹ã®ãã¼ãã«åä½ããã¯ããã«å¤§ããªæ çµã¿ã§ãè¤æ°ã®ã¨ã³ãã£ãã£ãæ±ãå¿ è¦ãããã®ãæ®éã§ããã¢ãã«ï¼ActiveRecordã¨ããèãã«ã¨ãããã¦ããã¨ã©ã£ãã§ç ´ç¶»ãã¾ãããã¸ãã¯ããã£ã¨ã¢ãã«ã®ä¸é¨ã¨ãã¦èªèãã¦ããããããªã¨æã£ã¦ã¾ãã
ãããã¾ã£ãããã®ã¨ããã ã¨æãã¾ããã
Webã¢ããªã®ãã¬ã¼ã ã¯ã¼ã¯ã£ã¦ã ãããããã¼ã¿ãã¼ã¹ãã¼ãã«ã®CRUDã«å²ããã£ã¦ããã©ã¦ã¶ã§åã MS Access ã¨ãã¦ä½¿ããªã綺éºã«ããããã§ããç°¡æããã°ãã¥ã¼ããªã¢ã«ã ãæ¯ã¹ãã¨å·®ããªãããã«è¦ãã¦ããMã¨Cã®ãã¢ãå´©ãå¿ è¦ãããå®éã®æ¥åã§ãããããããããã®ãã¬ã¼ã ã¯ã¼ã¯ã®è ¹ã®ä¸ãè¦ããã¨æããã§ããããã ãã¶ããããRailsãåºå§ãã®é ã«ã©ããã§èªãã ããã°ã®æå³ãããããããã£ã¦ãã¾ããã
è£è¶³ããã¨ãã³ã³ããã¼ã©ã«ãã¸ãã¯ãæ¸ãã¨ã¾ããã®ã«ã¯çç±ããã£ã¦ãããã¯ãã¢ããªã±ã¼ã·ã§ã³ã®å®è¡ç°å¢ã«ç´æ¥æ¥ãã¦ããã¨ããç¹ã§ãããã©ããããã¨ãã¨ããã¨ãHTTPã®äºæ ãå½±é¿ããããããã¸ãã¯ã ãã®åä½ãã¹ããããã«ããã¨ãããã¨ãã¤ã¾ããxUnitã§ã³ã¼ãã ãã使ã£ã¦åä½ãä¿è¨¼ãããã¨ããããããè¨ãåããé¨åãå¤ã®ç°å¢ããåãé¢ãå¿ è¦ããã£ã¦ããããã£ã¦åãåºãã¦ã¾ã¨ããçµæããã¯ãããã¯ã³ã³ããã¼ã©ã§ã¯ãªãã¢ãã«ã«ãªã£ã¦ãã¾ããã ããã究極çã«ã¯ã¢ãã«ãããã¹ãé§åã§ããªããã ã£ããæåããã³ã³ããã¼ã©ã«ãã¸ãã¹ãã¸ãã¯ãå ¥ããã¹ããããªããä¹±æ´ã«ããã¨ãã£ãããããªæãã
ä¿å®éçºã§ããããã®ãã¢ãã«å´ã®äºæ ã®å¤æ´ããã£ã¼ã«ãåãå¤ãã£ããªã©ããã®ããã§ã³ã³ããã¼ã©ããã£ã±ãæ¸ãæããªãã¨ãããªãã£ã¦ãã¨ãèµ·ããã¨ããã£ãããªãã¸ã§ã¯ãæåã§ãã£ã¦ãã®ã«ãç´äº¤æ§ããããä½ãã£ã¦ãã¨ã§ãããã
ãã¹ããéè¦ãããªããMVCã®ã¢ãã«ããã¼ã¿ãã¼ã¹ãã¼ãã«ã ã¨èª¤è§£ãããããã³ã³ããã¼ã©ã«ãããããæ¸ãã¦ãã¾ããã®ã¯ãå®ã¯ãã¾ãèªå°ãããªãã£ããã¨ã¾ãããããªä»®èª¬ãç«ã¦ãã¨ãã¾ãããã
ãããYiiã¯ã©ããªãã ã¨ããã¨...
ã¾ãç®ã«ä»ãã®ãYiiã®ã¢ãã«ã«ã¯ãã¹ã¦ getAttributeLabel ãªãã¦ã¡ã½ããããããã¨ã§ããã³ã¼ã«ããã¨ããã®å±æ§ã®è¡¨ç¤ºç¨ã®å称ãåºã¦ãã¾ãã"wordPress"ã¨ãããã£ã¼ã«ãåãå ¥ããã¨"WordPress"ã£ã¦ãã表示ç¨ã®ãã«ã¹ãã«ãåºã¦ããã¨ããæããã¢ãã«ã®å®è£ ã³ã¼ãã«ãã£ã¼ã«ãå称ã®ç®¡ç表ãå®è£ ããã¾ããå¤ãã£ã¦ã¾ããã
// ã¢ãã«å®è£ ã®ä¸é¨ public function attributeLabels() { return array( 'id' => 'ID', 'name' => 'Name', 'seq' => 'Sequence', ); }
ã§ãããèãã¦ã¿ãã°åççã§ããã£ã¼ã«ãåãèããã®ã¯ã¢ãã«ã ããããã®ã¨ããå¤æ°åã¨æå³ã®å¯¾å¿ã«ã¤ãã¦ãã§ã«ç¥èããã¾ãããããããã¥ã¼ã®ã»ãã§ãããã£ã¼ã«ãåã¨è¡¨ç¤ºç¨ã®å称ã®ä¸¡æ¹ãç¥ããªããã°ãªããªãã¨ããããã³ã¼ãä¸ã¯ççµåã§ããä»æ§æå³ãå¯çµåã«ãªã£ã¡ããã¾ãã表示ããããã®å称ãHTMLã®ãã¡ãã¡ã«æ£ãã°ãã¨ãDRYã§ããªããªãã¾ãã表示ã ãããã¥ã¼ã£ã¦ããæ§é çãªã¹ãè«ã§ã¯ãªãããã£ã¨ãé½åããã責ååæ ãæ¨å¥¨ããã¨ããã®ã¯ãPHPãç¾å®è§£ããªããªãªæè¡ã ã¨ããã®ã¨ãç¸æ§ãããæ°ããã¾ãã
RailsãCakePHPã¨å¤§ããéãã®ã¯ããã¼ã¿ãã¼ã¹ã使ãã®ãããã©ã«ããªã¢ãã«ã§ããã¦ä½¿ããªããã®ã§ã¯ãªãããã¼ã¿ãã¼ã¹ã使ãã¢ãã«ã¨ä½¿ããªããã©ã¼ã ã¢ãã«ã®ä¸¡æ¹ããã£ã¦ãã©ã£ã¡ã使ããããé¸æããããããªAPIã ã¨ããã®ãç¹å¾´ã§ããmodel ãã©ã«ãã®ä¸ã«CModelæ´¾çã§ãªãã³ã³ãã¼ãã³ããç½®ãã¦ãã¦ããã¼ã対象ã«ãªã£ã¦ãããããã¾ãã
ä»ã®ãã¬ã¼ã ã¯ã¼ã¯ã§ããç¥èãããã°ã§ãããã§ãããYii ã®é¢ç½ãã¨ããã¯ãã¢ããªã±ã¼ã·ã§ã³ã®ã¹ãã£ãã©ã«ãããã§ã«ãããããéCRUDãªMVCã®è¯ããµã³ãã«ã«ãªã£ã¦ããã¨ãããã¨ãYii ã®ã¢ããªã±ã¼ã·ã§ã³ã«ã¯æåããã
- ãã°ã¤ã³ç»é¢
- ãåãåãããã©ã¼ã
- ãã®ãµã¤ãã«ã¤ãã¦ãã¼ã¸
ãä»ãã¦ã¾ãããã 便å©ã ããã£ã¦ã ãã§ã¯ãªãããããã®æ©è½ããã¼ã¿ãã¼ã¹ã¨é¢ä¿ãªãMVCã®ãããµã³ãã«ã«ãªã£ã¦ãããã§ãããæåãããã models/LoginForm.php ã¯ãããªãã¼ã·ã§ã³ã§ãã¾ããsaveã§ããªããã©ã¼ã å ¥åç¨ã®ã¢ãã«ã§ããcontrollers/SiteController.php ã¯ç¹å®ã®ã¢ãã«ã¨ã®é¢ä¿ãªãã§ãããããæ©è½ãæä¾ãã¦ãã¾ããå ã«ããããã®ã«ç®ãéããã¨ãMS Access ã®ç½®ãæãã®ãããªèª¤è§£ãããã«æ¸ãããããªããã¨æãã¾ãã
ã¾ããYiiã®CRUDç³»ã¹ãã£ãã©ã«ãã®çµæã¯ãè¯ãæãã®Mã¨Cã®è²¬ååæ ã®ãµã³ãã«ã«ãªã£ã¦ãã¾ããçæç´å¾ã®CRUDã³ã³ããã¼ã©(ããã¦CRUDã³ã³ããã¼ã©ã¨è¨ãã®ã¯ãéCRUDã³ã³ããã¼ã©ãçæã§ãããã)ã«ã¯ãã»ã¨ãã©ã¢ãã«ã®äºæ ãç¾ãã¾ããããã¨ãã°ãããªæãã
public function actionDelete($id) { if(Yii::app()->request->isPostRequest) { $this->loadModel($id)->delete(); if(!isset($_GET['ajax'])) $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin')); } else throw new CHttpException(400,'Invalid request. Please do not repeat this request again.'); }
$this->loadModel($id) ã¨ããã®ãçµç±ããã¦ããããä½ã¨ããååã®ã¢ãã«ã¯ã©ã¹ã使ãã®ãããé ãã¦ãã¾ãã¨ããå ·åããããã¢ãã«ã®äºæ ã¨ã³ã³ããã¼ã©ããªãã¹ããã¨ãç´è¡ãã¦ã¾ããããã¢ã¯ã·ã§ã³ãåãä»ãã¦ã¢ãã«ã«ã¡ãã»ã¼ã¸ãéãã ãã§å æ ã¯ç¥ããªããç¹å®ã®ã¢ãã«åãç¾ããªãã®ã§ãåãæåã®ã³ã³ããã¼ã©ã¯ãã¢ãã«ãéã£ã¦ãã³ã¼ããã¾ã£ããåãå®è£ ã«ãªãã¾ãããããã¨ãããªãµãã«ãã¢ãã«ã®ã¯ã©ã¹åãããé è½ãã¦Mã¨VCã®éã®å¯çµåãé¿ããããã«ãã¦ãç®æãå¤ãç¾ãã¾ãããã¥ã¼ã§è¡¨ç¤ºãããã£ã¼ã«ããããã¨éæ¥çã«æååã§ååæå®ãã¦æ¾ã£ããã¨ãã
Webã®MVCã®ã³ã³ããã¼ã©ã§çµã¿ãããã¸ãã¯ã£ã¦ãããããHTTPã«é¢ããç¥èããã¨ã«ããå¶å¾¡ã§ããããã¸ãã¹ã®ãã¡ã¤ã³ã¨ã¯é¢ä¿ããªãããã¸ãã¹ãã¸ãã¯ã®äºæ ã¯ç¥ã£ããã£ã¡ããªãããã®ã¾ã¾ãã³ã³ããã¼ã©ã¯ã§ããã ãActiveRecordã®æ¨æºAPIã ãã«ä¾åããããã«ã§ããã°ãã³ã³ããã¼ã©ã¯ã¢ãã«ã®ã¡ã½ããã®ä»æ§å¤æ´ããå½±é¿ãåããªãã¦æ¸ã¿ã¾ãã
ããã§ã¤ãã³ãé§åãªã¢ãã«ãªããã§ããã
ãã¨ãã°ããã¼ã¿ãåé¤ãããã¨ããããæ·»ä»ãã¡ã¤ã«ããã£ãããããåæã«åé¤ãããã§ããããåé¤ã ãã¯ãã£ã¡ããã°ã«æ®ãã¦ããããããªãã¦ãã¨ãããã§ãããããããªã¨ããã³ã³ããã¼ã©ã®ä¸ã§ $model->delete(); ã®ã㨠$model->attachments ã®å ¨ã¦ã«unlinkãã¾ããããããããã¯HTTPã¾ããã®è²¬åãããªããCakePHPã«ãããã¾ãããYiiã§ããã¢ãã«ã« beforeDelete/afterDelete ã¡ã½ãããå®ç¾©ããã ãã§ãåé¤ã¢ã¯ã·ã§ã³ã®åå¾ã§èµ·ãã£ã¦æ¬²ãããã¨ãç°¡åã«å®è£ ã§ãã¾ãã
//Modelå®è£ ã®ä¸ã§ public function beforeDelete() { Yii::log('deleting ' . $this->id); } public function afterDelete() { foreach($this->attachments as $a) { unlink($a); } Yii::log('deleted ' . $this->id); }
ä»ã«ããããããã³ãã©ããã£ã¦ãafterFindãbeforeSaveã¯å©ç¨é »åº¦ã®é«ããã³ãã©ã§ãã
ã¤ãã³ããã³ãã©ãã²ã¨ã¤ãªã®ãå«ã ã¨ãã人㯠attachEventHandler ãªãã¦ã®ãããã¾ãã
Yiiã®ã¢ããªã±ã¼ã·ã§ã³ã§ã¯ããªãããã¤ãã³ãé§åãæ®éã®ä½ãæ¹ã ã¨ããæè¦ã«é¥ãã¾ãããªããªã®ãã¯èªè¦ãã¦ãªãã¦èª¬æãã«ããã®ã§ãããæåããã§ããã ãããããããæ¹ã§æ¸ããããªãã»ãã«èªå°ããã¡ããã
ã§ãããããã¾ãç½ ã«ãªã£ã¦ãã¦ãã¤ãã³ããã³ãã©ãæ¸ãã®ãèªç¶ã«ãããããã«ãªãã¨ãfindããããå¿ ãafterFindãå¼ã°ãã¦ããç¶æ ãæ ä¿ã§ããã¨ããå®å¿æãå¾ããããããããã¨ãåæåã¡ã½ããã®å¼ã³å¿ãã¿ãããªãã¹ã®å¿é ããªããªããCRUDæä½ã¨ãã¸ãã¯ã絶対ã«ã¯ã³ã»ããã«ãªããafterFindãåããå¾ã®ç¶æ ããã¹ãããã ãã§ãããªããããã¾ã§ã®ãã¨ããã³ã³ããã¼ã©ããã®å¼ã³åºãã«é ¼ããªãã¦ããã
ã»ããåç´ãªORMã ã¨findããã¹ããããªãã¦åãã¦å½ããåã§ãã«ãã«ãããã©ãafterFindã¤ãã³ãã®å¯è½æ§ã¾ã§å«ãã ããfindç³»ã®ãã¹ããæ¸ãã¦ãããããªãã¾ãããããã©ã¦ã¶ãªãã§ãåããã¦ã¿ããã§ããã... ã¯ããç½ ã«ãããã¾ãããããã¢ãã«ã£ã¦ãããããã®ã§ããã¨ããæ¦å¿µã¸ã®å°ç·ããã£ããã§ãããã
èªè¨¼ã¨ã»ãã¥ãªãã£ãã£ã«ã¿ã¨OCP
èªåã Yii ã使ã£ã¦ããæ大ã®çç±ãã³ã¬ã§ãã
å人çã«CakePHPã®ACLãã©ãããã¡ã§ãã¢ã¯ã»ã¹å¶å¾¡ã«ã¼ã«ã®å²ãå½ã¦ã¯ãã¡ã¤ã³ã¢ãã«ã¨ã¯é¢ä¿ãªããããã¼ã¿ã®å®ä½ããå è¡ããã¹ããã®ã ã¨æãã®ã§ãããã¼ã¿ãã¼ã¹ãå ã«ãã£ã¦ãããã«ã½ã¼ã¹ã³ã¼ããå¾å±ããã¨ããé¢ä¿ã ã¨èªåã®ä»äºã¨ã®ç¸æ§ããã¾ãã¡è¯ããªãã£ãã
Yiiã®ã¢ã¯ã»ã¹å¶å¾¡ã¯ã³ã³ããã¼ã©ã«ã³ã¼ãã¨ãã¦æ¸ãã¾ãããã£ã¨ãã·ã³ãã«ãªãã¼ã«ãã¼ã¹ã®å¶å¾¡ã¯ãããªã³ã¼ãã
public function accessRules() { return array( array('allow', 'roles'=>array('admin'), ), array('deny', 'users'=>array('*'), ), ); }
ã§ãèªè¨¼ã¯ã©ããããã¨ããã¨ãCUserIdentity ãæ´¾çãããã¯ã©ã¹ãä½ã£ã¦ authenticate ããªã¼ãã¼ã©ã¤ãããããã®ã¨ããã¼ã¿ãã¼ã¹ã®ã¦ã¼ã¶ãã¼ãã«ã使ã£ã¦ããããããã¡ã¤ã«ãèªãã§ç §åãã¦ããããä½ãªããã¹ã¯ã¼ãããã¼ãã³ã¼ãã£ã³ã°ãã¦ãããããªã®ã§ãå ã«å®å ¨ãªèªè¨¼ãä½ããªãã¨ã¢ããªã±ã¼ã·ã§ã³ã«ãã°ã¤ã³ã§ããªãã®ã§ã¯ãªããèªè¨¼ããã£ã¨ä»®å®è£ ã®ã¾ã¾ã«ãã¦ããã¦ãå ã«æ©è½ãä½ã£ã¦ãã¾ããã¨ã ã£ã¦ã§ããã
class UserIdentity extends CUserIdentity { public function authenticate() { // ä»® return 0; } }
ãã¼ã«ã¨ã¦ã¼ã¶ã®å²ãå½ã¦ã¯? ããããªã¼ãã¼ã©ã¤ãã§ã©ãã«ããªãã¾ããèªåã®å ´åã¯ããã¼ã«ã®æ°ãéããã¦ããã®ã§ãããªæãã®ãå®éã«ä½¿ã£ã¦ãã¾ãã
class AuthManager extends CPhpAuthManager { public function checkAccess($itemName, $userId, $params=array()) { $user = User::model()->findByAttributes(array('name'=>$userId)); if($itemName == 'admin') { return !empty($user) && $user->admin; } else { return TRUE; } } }
ã¨ãã£ãå ·åã§ãã¾ãã³ã¼ãããããåºæ¬ã§ãããã¼ã¿ãã¼ã¹ã使ããã©ããã®å¤æã¯ãã³ã¼ãã®å®è£ ã«æ¨©éãããããã³ããæ£æ»æ³ã§ãããããã®æ£æ»æ³ã®ãªã«ãå¬ãããã¨ããã¨ãããä¸ã®æ¨©éãæã¤ã®ããã½ã¼ã¹ã³ã¼ããã®ã»ãã§ãããã¤ã¯ããã¼ã¸ã§ã³ç®¡ç対象ã ãã£ã¦ãã¨ã§ãã
ãã¼ã¿ãã¼ã¹ã¯ã½ã¼ã¹ã³ã¼ãã®ããã«ããã°ã©ãã管çã§ããããã§ã¯ãªããããã°ãç¸æã®ç°å¢ãã®ä¸é¨ã§ããé½åã®ããããã«ãªããªãå ´åãããã§ããããããã§ããã¦ã¼ã¹ã±ã¼ã¹ã«æ²¿ã£ã¦ã¦ã¼ã¶ã®ç¨®é¡ãã¨ã«å½¹å²ãæ£ããå²ãæ¯ãã¨ããä½æ¥ã¯ãã³ã¼ãå®è£ è ã®è²¬åãªãã§ãããã ã£ãããã®ã¨ããã«ããã»ããããã
ããããããªãã¸ã§ã¯ãæåã®æ£æ»æ³ã§èããã°ã§ããªããã¨ã¯ãªããã¨æå¾ ã§ããè¨è¨ã ã¨ãããã¨ããã¨ã¦ã大äºãªãã¨ã ã¨æãã®ã§ãããã¨ä¸æ©ã§ãããããã¨ãã§ãããã£ã¦ç¶æ³ã§ãã³ã¢ã©ã¤ãã©ãªã«ããããå½ã¦ãªãããããªãã®ã¯ãOCP(Open Close Principal)ã«åãã¦ã¾ããããOOPã£ã¦ã®ã¯OCPã®ããã«ããã¨è¨ã£ã¦ããããããããã®æå¾ ã§ããæã¯OOçã«å¤§äºã ã¨æãã¾ãã
ã§ããããªã¨ããã
ã¾ãã§Yiiè¤ãã¡ããã¿ããã§ããããããªYiiã«ãã¡ãã£ã¨ããã¯ã©ããªãã ããã£ã¦ç¹ãããã¾ãã
Yii ã¯ãã¥ã¼ã¾ããã«ããããHTMLã³ã¼ãã¼ã®å ¥ãä½å°ãã»ã¨ãã©ããã¾ããããããããããYii ã®ãã¥ã¼ã¯ãã®ã»ã¨ãã©ãYiiã®APIãã³ã¼ã«ããPHPã¿ã°ã§åã¾ãã¾ãããã¨ãã°...
<?php Yii::app()->clientScript->registerCoreScript('jquery'); Yii::app()->clientScript->registerScriptFile(Yii::app()->request->baseUrl . '/js/jquery.json.js'); ?>
jQueryã®ãã©ã°ã¤ã³ããªã³ã¯ãããã ãã§ãããæ¨æºã®æ¹æ³ã ã¨ãã¾ããã¨ã
ã¾ãã§ãèãã¦ã¿ã¦ãã ãããMVCã«ãããVã¯ããã¥ã¼ãã¸ãã¯ãã§ãããã¤ã¦ã§ããã°ãæç¶ãè¨èªã ãã§ãã£ã¦ããããã§ããããèããã°ãã¾ã HTMLã¬ã³ãã©ãå©ç¨ã§ããã ããã·ã¨ãããã®ããããã¾ãããYiiã§ä½ããã®ã¯ã¢ããªã±ã¼ã·ã§ã³ã§ãã£ã¦ã³ã³ãã³ãã§ã¯ãªããããããã¹ã¿ã³ã¹ãªãã§ããããããã£ã¨ã
ãã¨... ããã¦è¨ãã¨ããã°ããããããªãããã¨ããã®ãã´ã®MSNã¿ãããªæãã¯ã©ãã«ããªããªããã®ãã¨w ããã«ååã£!
ã¨ãæã£ããã¨ããã£ã±ãæ¸ãããèªãã®ãããã¸ãã«ãªã£ã¦ãã¾ãã¾ããã
ã¨ãã«ã¤ãã³ããªããã®æ©è½ã¯ãCakePHPã«ãã£ããã§ãæ¹ãã¦è¨ããã¨ãããªããããããªããããããã©åãæ°ã«ãããã®ã¯ãã®ãåããããªãã®ãã®ãæåã®è¦ãæ¹ãã¨ãå¦ç¿ã®æµãã¥ãããããããä¸æã ã¨ãããã¨ã§ããYiiã¯Railsã¨CakePHPã®è¨è¨ããããåèã«ãã¦ããã¨æãã¾ããããããå
é¨ã®è¨è¨ã使ããæ¹ã®æ³å®ãããã£ã¨ãªãã¸ã§ã¯ãæåçã«çªãè©°ãã¦èãã¦ã¿ãçµæãã£ã¦æãã§ããã
ãã®ä¸æ¹ã§ãæè©®PHPãªã¨ããã¯ãã£ãã妥åãã¦ãããããªãããã«ãã¦ãããã«ãè¦ãã¾ãã
ã¾ã èªåãã¦ã¼ã¶ã¨ãã¦çµé¨ã¯æµ ããã©ããã®ä¸ããããã£ã¦ãããã¨ããããªã«ãã£ãã®ã§ãPHPãã¬ã¼ã ã¯ã¼ã¯è©±ãããã¨ããããå¤ãã¦èããã®ã¯ã¡ãã£ã¨æãã¦ãããããªãããªã¨æãã¾ãããã§ã«å¥ã®ãã¬ã¼ã ã¯ã¼ã¯ã§æè¡ã確ç«ãã¦ã人ã§ããYiiã®APIããã¯ãããããªãã¨ãå¦ã¹ãããããªãã§ããããã