ããŒã¿çã«ã¯ãã¢ã«ãã¡ãªãªãŒã¹ããã®äœçŸãã®é©æ°ãå€æŽãä¿®æ£ãå«ãŸããŠããŸãã
次ã«ãæãéèŠãªãã®ã確èªããŸãããæåã«ãããã質åã«çããŸãã
ãããã質å
- ããŒã¿çãšã¯ã©ãããæå³ã§ããïŒ ããŒã¿çãšã¯ãå®å®ããäžé£ã®æ©èœãšã¢ãŒããã¯ãã£ãæå³ããŸãã ããŒã¿çããGAïŒäžè¬æäŸãå®å®çãªãªãŒã¹ïŒãŸã§ãäž»ã«ãã°ã®ä¿®æ£ãšããã¥ã¡ã³ããæ±ããŸãã ãã¬ãŒã ã¯ãŒã¯ã®ã¢ãŒããã¯ãã£ãå€æŽããããæ°ããæ©èœãè¿œå ãããããã€ããã¯ãããŸããã äžäœäºææ§ã®å èš³ã¯åŒãç¶ãçºçããå¯èœæ§ãããããšã«æ³šæããå¿ èŠããããŸãããã§ããéãå°ãããã詳现ã詳现ã«èª¬æããããã«ããŸãã
- å®å®çãªãªãŒã¹ã¯ãã€ã§ããïŒ ä»ã®ãšãããæ£ç¢ºãªæ¥ä»ã瀺ãããšã¯ã§ããŸããã 次ã®ã¹ãããã¯äž»ã«ãšã©ãŒã®ä¿®æ£ãšããã¥ã¡ã³ãã®å®æã§ãããããããã»ã©æéã¯ããããªããšèããŠããŸãã
- ãããžã§ã¯ãã§ããŒã¿çã䜿çšã§ããŸããïŒ ç· ãåããå³ãããããžã§ã¯ãããYii 2.0ã«æ £ããŠããªãå Žåã¯ãããŒã¿çã䜿çšããªãã§ãã ããã ãããããªãã«é¢ä¿ãªãããã¬ãŒã ã¯ãŒã¯ã®å€æŽãæããŠããªãå Žåã¯ãè©ŠããŠã¿ãŠãã ããã ç§ãã¡ã¯ãããªãå€ãã®ãããžã§ã¯ãããã§ã«ç«ã¡äžãããã `master`ãã©ã³ãã§äœæ¥ããŠãããšèããŸããã ãŸãããã¬ãŒã ã¯ãŒã¯ãæ©èœããããã®æå°PHPããŒãžã§ã³ã¯5.4ã§ããããšãå¿ããªãã§ãã ããã
- 2.0ã®ããã¥ã¡ã³ãã¯ãããŸããïŒ ã¯ãã 決å®çã¬ã€ããšAPIããã¥ã¡ã³ãããããŸã ã ã¬ã€ãã¯åžžã«æŽæ°ãããŠããŸãã ãã·ã¢èªãžã®ç¿»èš³ã¯ããªãªãžãã«ãã»ãŒå®æãããšããã«è¡ãããŸãã
- ã¢ããªã±ãŒã·ã§ã³ã1.1ãã2.0ã«ã¢ããã°ã¬ãŒãããã«ã¯ã©ãããã°ããã§ããïŒ ãã®ãããã¯ã«ã€ããŠã¯ãå¥ã®ã»ã¯ã·ã§ã³ãäœæãããŠããŸãã
Yii 1.1ããã®ã¢ããã°ã¬ãŒã ã 2.0ã¯å®å šã«æžãçŽãããŠãããããæŽæ°ã¯ç°¡åãªäœæ¥ã§ã¯ãªãããšã«æ³šæããŠãã ããã ããã§ãã1.1ã䜿çšããå Žåã1.1ãš2.0ã«ã¯å€ãã®é¡äŒŒç¹ããããããã¯ããã«ç°¡åã«ãªããŸãã - alpha 2.0ããã¢ããã°ã¬ãŒãããæ¹æ³ã¯ïŒ Composerã䜿çšããŠã¢ã«ãã¡ãæŽæ°ããå Žåã
.gitignore
以å€ã®ãã¹ãŠãvendor
ãã£ã¬ã¯ããªããåé€ããŠãcomposerãå床å®è¡ããå¿ èŠããããŸãã ããã¯äžåºŠã ãè¡ãå¿ èŠããããæ°ãããªãªãŒã¹ããšã«ãããè¡ãå¿ èŠã¯ãããŸããã åŸæ¹äºææ§ãå£ããã®ãå«ãå€æŽã®å®å šãªãªã¹ãã¯ã CHANGELOGãã¡ã€ã«ã«ãããŸãã - éçº2.0ããã©ããŒããæ¹æ³ã¯ïŒ ãã¹ãŠã®éçºãšè°è«ã¯GitHubã§è¡ãããŸãã
https://github.com/yiisoft/yii2 æŽæ°ããµãã¹ã¯ã©ã€ãããããã¢ã¹ã¿ãªã¹ã¯ãä»ããŠæŽæ°éç¥ãåãåãããšãã§ããŸãã twitter https://twitter.com/yiiframeworkããã©ããŒããããšãã§ããŸã ã
ã¢ã«ãã¡ã«ãã倧ããªå€å
å€æŽã®å®å šãªãªã¹ãã¯CHANGELOGã«ãããŸãã以äžã§æãéèŠãªãã®ã«ã€ããŠèª¬æããŸãã
æ§é
Yii 2ã¯PSR-4ã®äœ¿çšãéå§ããŸãã
ã¯ã©ã¹ãããŒãããŸãã ããã¯ïŒ
- ãã¬ãŒã ã¯ãŒã¯ã®ãã£ã¬ã¯ããªæ§é ãããã·ã³ãã«ã«ãªããŸããã
- æ¡åŒµãã£ã¬ã¯ããªæ§é ãããã·ã³ãã«ã«ãªããŸããã
- ãã¬ãŒã ã¯ãŒã¯ã¯ãPEARã¹ã¿ã€ã«ã§æå®ãããã¯ã©ã¹ãããŒãããªããªããŸããã ã¯ã©ã¹ã®èªã¿èŸŒã¿ã«ããããããããç°¡åãã€é«éã«ãªããŸããã
Module::controllerNamespace
ã䜿çšããªãéããã³ã³ãããŒã©ãŒã¯ã©ã¹ã¯
Module::controllerNamespace
ã§æå®ãããåå空éã«é 眮ããå¿ èŠããããŸãã
ãŸãã1.1ã«ãã£ããµããã£ã¬ã¯ããªå ã®ã³ã³ãããŒã©ãŒã®ã°ã«ãŒãåã®ãµããŒããè¿ããŸããã
䜿ãããã
䜿ããããã¯ãåœç€ŸããŒã ã®æåªå äºé ã®1ã€ã§ãã ãã®ãããåœåã«å€ãã®æ³šæãæããããŸããŸãªIDEã§ã³ãŒããæ£åžžã«æ©èœããããã«ããŸãã ããã«ãããæ¯æ¥ã®éçºããã楜ããããããšãã§ããŸãã
PSR-1ããã³PSR-2ã³ãŒãã¹ã¿ã€ã«ã«åãæ¿ããå€ãã®IDEã èªåã³ãŒããã§ã㯠ã
èªåãã©ãŒããã ã
æ§èœ
æãéèŠãªå€æŽã®1ã€ã¯ãæ¬åœã«å¿ èŠã«ãªããŸã§ã»ãã·ã§ã³ãéå§ãããªãããšã§ãã ããã«ãããã¢ããªã±ãŒã·ã§ã³ã¯äœåãªãªãœãŒã¹ã浪費ããªããªããŸãã
ãããžã§ã¯ãã§Markdownã䜿çšãããšãHTMLãžã®å€æé床ãå€§å¹ ã«åäžããŠããããšãããããŸãã ããã¯ãæ¢åã®ãœãªã¥ãŒã·ã§ã³ãã¬ãã¥ãŒããåŸãCarsten BrandtïŒcebeïŒããããã®ç®çã®ããã«ã©ã€ãã©ãªããŒãããäœæããåŸã«å¯èœã«ãªããŸããã ããã©ãŒãã³ã¹ã«å ããŠãæ¡åŒµæ§ãåäžããGitHubã§äœ¿çšãããããŒã¯ããŠã³ãªãã·ã§ã³ããµããŒãããŸãã
å®å šæ§
Yiiã¯*ãã¹ã¯* CSRFããŒã¯ã³ã䜿çšããŠã BREACHæ»æãé²ããŸãã
RBACãæžãçŽãããç°¡åã«ãªããŸããã çŸåšãããžãã¹ã«ãŒã«ã¯
eval
ãªãã¯ã©ã¹ã®åœ¢åŒã§çºè¡ãããŠããŸãã
RESTful APIãã¬ãŒã ã¯ãŒã¯
ããŒã¿çã§ç»å ŽããåŸ æã®æ©èœã¯ãREST APIãäœæããããã®çµã¿èŸŒã¿ãã¬ãŒã ã¯ãŒã¯ã§ãã ãã®ã¬ãã¥ãŒã§ã¯ã詳现ã«ã€ããŠã¯èª¬æãããäž»ãªæ©èœã®ã¿ããªã¹ãããŸãã 詳ãã説æã¯å¯èœã§ãDefinitive Guideã®ã¬ãã¥ãŒã® ã
- ActiveRecordã«ããé »ç¹ãªæäœã®è¿ éãªãããã¿ã€ãã³ã°ã
- å¿ç圢åŒã®ããŽã·ãšãŒã·ã§ã³ïŒJSONããã³XMLã¯ããã©ã«ãã§ãµããŒããããŠããŸãïŒ;
- ãªããžã§ã¯ãã®ã«ã¹ã¿ãã€ãºå¯èœãªã·ãªã¢ã«åã ã·ãªã¢ã«åå¯èœãªãã£ãŒã«ããéžæã§ããŸãã
- ããŒã¿åéãšæ€èšŒãšã©ãŒã®é©åãªãã©ãŒãããã
- å®å šãªHTTPãµããŒãã«ããå¹æçãªã«ãŒãã£ã³ã°ã
-
OPTIONS
ããã³HEAD
ãµããŒãã - èªèšŒ
- èªå¯
- HATEOASãµããŒã;
- HTTPãã£ãã·ã³ã°
- ãªã¯ãšã¹ãã®æ°ãå¶éããŸãã
äŸåæ§æ³šå ¥ãšãµãŒãã¹ãã±ãŒã¿ãŒ
å€ãã®äººãYiiã«äŸåæ§æ³šå ¥ïŒDIïŒã³ã³ãããŒããªãçç±ãå°ããŠããŸããã å®éãYiiã¯åžžã«Yiiã¢ããªã±ãŒã·ã§ã³ã®ã€ã³ã¹ã¿ã³ã¹ã§ããService Locatorã®åœ¢åŒã§åæ§ã®æ©èœãæäŸããŠããŸããã ããã§ããµãŒãã¹ãã±ãŒã¿ãŒãå¥ã®ã³ã³ããŒãã³ã
yii\di\ServiceLocator
ãšããŠå²ãåœãŠãŸããã åãšåæ§ã«ãã¢ããªã±ãŒã·ã§ã³ãšã¢ãžã¥ãŒã«ã®äž¡æ¹ããµãŒãã¹ãã±ãŒã¿ãŒã§ãã
Yii::$app->get('something')
ã䜿çšããŠããµãŒãã¹ïŒ1.1ã§ã¯ã¢ããªã±ãŒã·ã§ã³ã³ã³ããŒãã³ããšåŒã°ããŠããŸããïŒã
Yii::$app->get('something')
ãŸãã
Service Locatorã«å ããŠã
yii\di\Container
ã³ã³ãã
yii\di\Container
ãå®è£ ããŸããã é¢é£æ§ã®äœãã³ãŒããäœæããã®ã«åœ¹ç«ã¡ãŸãã äºåããŒã¿ã«ãããšããã®ã³ã³ããã¯PHPã§æãé«éãªDIã³ã³ããã®1ã€ã§ãã
Yii::$container->set()
ã䜿çšããŠãã¯ã©ã¹ã®åæå€ãèšå®ã§ããŸãã å€ã
Yii::$objectConfig
åé€ãããŸããã
ãã¹ãäž
Yiiã¯Codeceptionãšçµ±åãããŠããŸã ã ããã«ããããŠãŒã¶ãŒã¢ã¯ã·ã§ã³ããšãã¥ã¬ãŒãããçââæãããåçã®æ£ç¢ºæ§ã確èªããããšã«ãããã¢ããªã±ãŒã·ã§ã³å šäœããã¹ãã§ããŸãã PHPUnitã§ã®ã»ã¬ã³ã®ãµããŒããšã¯ç°ãªããCodeceptionã¯ãã©ãŠã¶ãŒãå¿ èŠãšãããCIãµãŒããŒã«é 眮ããã®ãç°¡åã§ãã¯ããã«é«éã«åäœããŸãã
Yiiã¯ãŸãã ãã£ã¯ã¹ãã£ãµããŒããè¿œå ããŸãããããã¯ããã¹ãããŒã¿ãåæåããåé¡ã解決ããŸããããã¯ããã¹ããèšè¿°ããéã®æãé·ãæãæ¥åžžçãªã¿ã¹ã¯ã®1ã€ã§ãã ãã£ã¯ã¹ãã£ã®ãµããŒãã¯ãäœæãšã¢ããªã±ãŒã·ã§ã³ã®äž¡æ¹ãã«ããŒããŸãã ãã§ã€ã«ãŒæ¡åŒµæ©èœãå®è£ ãããå®éã®ããŒã¿ã»ããã«è¿ããã®ãäœæã§ããŸãã
ãåºæ¬ããšãé«åºŠãã®äž¡æ¹ã®ã¢ããªã±ãŒã·ã§ã³ãã³ãã¬ãŒãã¯ããã¹ããã¢ãžã¥ãŒã«ãæ©èœãããã³åãå ¥ãã®äž¡æ¹ã§æäŸãããŸãã ããããã¹ããéããŠéçºãé©çšããè¯ãã€ã³ã»ã³ãã£ãã«ãªãããšãé¡ã£ãŠããŸãã
ã¢ãã«æ€èšŒ
ã¢ãã«æ€èšŒã§ã¯ãããªãå€ãã®èå³æ·±ãæ¹åãè¡ãããŸããã
UniqueValidator
ããã³
ExistValidator
ã¯ãè€æ°åã®æ€èšŒããµããŒãããããã«ãªã
ExistValidator
ã 以äžã¯ã
unique
ããã€ãã®äŸã§ãã
// a1 ['a1', 'unique'] // a1 , a2 ['a1', 'unique', 'targetAttribute' => 'a2'] // a1 a2 [['a1', 'a2'], 'unique', 'targetAttribute' => ['a1', 'a2']] // a1 a2 , a1 ['a1', 'unique', 'targetAttribute' => ['a1', 'a2']] // a1 a2, a3 ['a1', 'unique', 'targetAttribute' => ['a2', 'a1' => 'a3']]
æ€èšŒã¯ã
when
ããã³
whenClient
æå®ãããæ¡ä»¶äžã§å®è¡ã§ã
whenClient
ã 以äžã¯ãéžæããåœãç±³åœã®å Žåã«ã®ã¿å¿ é ã®å·ãã£ãŒã«ãã®äŸã§ãã
['state', 'required', 'when' => function ($model) { return $model->country == Country::USA; }, 'whenClient' => "function (attribute, value) { return $('#country').value == 'USA'; }", ]
ã¢ãã«ã«é¢é£ä»ããããŠããªãããŒã¿ã確èªããå¿ èŠãããå ŽåããããŸãã ããã¯ãæ°ãã
yii\base\DynamicModel
ã䜿çšããŠå®è¡ã§ããŸãã
public function actionSearch($name, $email) { $model = DynamicModel::validateData(compact('name', 'email'), [ [['name', 'email'], 'string', 'max' => 128], ['email', 'email'], ]); if ($model->hasErrors()) { // } else { // } }
ããŒã¿ããŒã¹ãšã¢ã¯ãã£ãã¬ã³ãŒã
ããŒã¿ããŒã¹ã«é¢é£ãããã¹ãŠãYiiã®é·æã§ãã ãã®æ©èœã»ããã¯ã¢ã«ãã¡çã§ã¯ãã§ã«éåžžã«èå³æ·±ããã®ã§ããããããŒã¿çã§ã¯æ¹è¯ç¹ãšæ°æ©èœãè¿œå ãããŸããã Active Recordã®å®è£ ã«ã¯elasticsearchãredisãããã³Sphinxæ€çŽ¢ãå«ãŸããŸãã ããŒã¿çã§ã¯ã mongodbãµããŒããç»å ŽããŸãã ã
ãã¹ãããããã©ã³ã¶ã¯ã·ã§ã³ã®ãµããŒã
Yiiã¯ãã¹ãããããã©ã³ã¶ã¯ã·ã§ã³ããµããŒãããããã«ãªããŸããã å€éšãã©ã³ã¶ã¯ã·ã§ã³ãéå§ããããã©ãããå¿é ããããšãªãããã©ã³ã¶ã¯ã·ã§ã³ãéå§ã§ããŸãã
åå ãã
ActiveQuery::joinWith()
ãè¿œå ããŠããã§ã«å®£èšãããŠããARé¢ä¿ã䜿çšããJOINã䜿çšããSQLã¯ãšãªã®äœæããµããŒãããŸãã ããã¯ãé¢é£ããããŒãã«ã®ãã£ãŒã«ãã§ããŒã¿ããã£ã«ã¿ãªã³ã°ãŸãã¯ãœãŒãããå Žåã«ç¹ã«äŸ¿å©ã§ãã äŸïŒ
// id id . "customer" $orders = Order::find()->joinWith('customer')->orderBy('customer.id, order.id')->all(); // , . "books" $orders = Order::find()->innerJoinWith('books')->all();
ããã¯ãGridViewã§é¢é£ããåãæäœãããšãã«ç¹ã«åœ¹ç«ã¡ãŸãã ä»ã䜿çšããŠ
joinWith()
ãœãŒãããã£ã«ã¿ã«åœŒã®ããã«è¡ãããšã¯éåžžã«ç°¡åã«ãã
ããŒã¿åå€æ
ActiveRecordã¯ãããŒã¿ããŒã¹ããåä¿¡ããããŒã¿ã察å¿ããã¿ã€ãã«å€æããããã«ãªããŸããã ããšãã°ãããŒã¿ããŒã¹ã«
integer
type
åãããå Žåã察å¿ããActiveRecordã€ã³ã¹ã¿ã³ã¹ãåãåã£ãåŸã
type
ã¯PHPã®
integer
type
ã«ãªããŸãã
æ€çŽ¢ãã
æ€çŽ¢ã¿ã¹ã¯ãç°¡çŽ åããããã«ã空ã®å€ãèªåçã«åé€ãã
Query::filterWhere()
ã¡ãœãããè¿œå ããŸããã ããšãã°ã
name
ãš
email
ãã£ãŒã«ãã«ãããã£ã«ã¿ãŒãåããæ€çŽ¢ãã©ãŒã ãããå Žåãäžèšã®ã³ãŒãã䜿çšããŠæ€çŽ¢ã¯ãšãªãäœæã§ããŸãã ãã®æ¹æ³ããªããã°ããŠãŒã¶ãŒããã£ãŒã«ãã«äœããå ¥åãããã©ããã確èªãããã®å Žåã®ã¿æ€çŽ¢èªãè¿œå ããå¿ èŠããããŸãã
filterWhere()
ã¯ãã®ãã§ãã¯ãè¡ããŸãã
$query = User::find()->filterWhere([ 'name' => Yii::$app->request->get('name'), 'email' => Yii::$app->request->get('email'), ]);
ããããªã¯ãšã¹ã
倧éã®ããŒã¿ãåŠçããããã®ãããèŠæ±ã®ãµããŒããè¿œå ããŸããã ãã®æ©èœã䜿çšãããšãããã«ã§ã¯ãªããããã§ããŒã¿ãåãåããŸãã ããã«ãããã¡ã¢ãªãå€§å¹ ã«ç¯çŽã§ããŸãã äŸïŒ
use yii\db\Query; $query = (new Query()) ->from('user') ->orderBy('id'); foreach ($query->batch() as $users) { // $users â 100 user } // , foreach ($query->each() as $user) { // $user â user }
ActiveRecordã§ãããã¯ãšãªã䜿çšã§ããŸãã
// 10 foreach (Customer::find()->batch(10) as $customers) { // $customers â 10 Customer } // 10 , foreach (Customer::find()->each(10) as $customer) { // $customer â Customer } // foreach (Customer::find()->with('orders')->each() as $customer) { }
ãµãã¯ãšãªã®ãµããŒã
ãã¹ããããã¯ãšãªã®ãµããŒããã¯ãšãªãã«ãã«è¿œå ãããŸããã ãµãã¯ãšãªãéåžžã®
Query
ãªããžã§ã¯ããšããŠäœæããå¥ã®
Query
䜿çšã§ããŸãã
$subQuery = (new Query())->select('id')->from('user')->where('status=1'); $query->select('*')->from(['u' => $subQuery]);
ãã£ãŒãããã¯
å€ãã®å Žåããªã³ã¯ã¯ãã¢ã«ã§ããŸãã ããšãã°ã
Customer
ã¯
orders
é¢ä¿ãå«ãŸããŠããå Žåãããã
Customer
ã¯
customer
é¢ä¿ãå«ãŸããŠ
Order
å Žåããã
customer
ã 次ã®äŸ
customer
泚æã®
customer
å ã®ãªããžã§ã¯ããšåããªããžã§ã¯ãã§ã¯ãªãããšãããããŸãã
ã©ã®æ³šæãæ±ããããŠããŸãã ã³ãŒã«
customer->orders
åŒã³æ¹ã ãã§ãªããæ°ããSQLã¯ãšãªã«ã€ãªãã
customer
ãªãŒããŒãïŒ
// SELECT * FROM customer WHERE id=1 $customer = Customer::findOne(1); // echoes "not equal" // SELECT * FROM order WHERE customer_id=1 // SELECT * FROM customer WHERE id=1 if ($customer->orders[0]->customer === $customer) { echo 'equal'; } else { echo 'not equal'; }
äžèŠãªSQLã¯ãšãªãåé¿ããããã«ã
customer
ãš
orders
ã«å¯Ÿãããã£ãŒãããã¯ã宣èšã§ããŸãã ããã¯ã
inverseOf()
ã¡ãœããã䜿çšããŠè¡ãããŸãã
class Customer extends ActiveRecord { // ... public function getOrders() { return $this->hasMany(Order::className(), ['customer_id' => 'id'])->inverseOf('customer'); } }
ããã§ãäžèšã®ã³ãŒããå床å®è¡ãããšã次ã®çµæãåŸãããŸãã
// SELECT * FROM customer WHERE id=1 $customer = Customer::findOne(1); // echoes "equal" // SELECT * FROM order WHERE customer_id=1 if ($customer->orders[0]->customer === $customer) { echo 'equal'; } else { echo 'not equal'; }
ãªã¬ãŒã·ã§ãã«ã¯ãšãªçšã®çµ±äžAPI
ActiveRecordã®alpha 2.0ã§ã¯ãäž¡æ¹ã®ãªã¬ãŒã·ã§ãã«ïŒMySQLãªã©ïŒã®ãµããŒããå®è£ ããããã
noSQLïŒredisãelasticsearchãMongoDBãªã©ïŒããŒã¿ã¹ãã¢ã ããŒã¿çã§ã¯ããã®ã³ãŒãããªãã¡ã¯ã¿ãªã³ã°ããã€ã³ã¿ãŒãã§ã€ã¹ã®åäžæ§ãé«ããŸããã ã€ãŸãã
ActiveRelation
ã¯åé€ããã
ActiveQuery
ã¯é¢ä¿ã«é¢ããã¯ãšãªãå®è¡ããé¢ä¿ãèšè¿°ããããã®åäžã®ãšã³ããªãã€ã³ãã«ãªããŸããã
ActiveRecord::findOne()
ããã³
findAll()
ã¡ãœãããè¿œå ããŸããã
ãããã®ã¡ãœããã䜿çšãããšãçãæ§æã䜿çšããŠããŒãŸãã¯åã®å€ãç §äŒã§ããŸãã 以åã¯ã
ActiveRecord::find()
ã¡ãœããã®ãã©ã¡ãŒã¿ãŒã䜿çšããŠãããè¡ãããšãã§ããŸãããããã«ãããã¡ãœãããç°ãªãããŒã¿åãè¿ãããã«æ··ä¹±ãçããŸããã
AJAXãµããŒã
åªããPjaxã©ã€ãã©ãªãŒã䜿çšããããšã«æ±ºãã
yii\widgets\Pjax
ãäœæããŸããã ããã¯ãã©ãããããŠãããã¹ãŠã«AJAXãè¿œå ã§ããäžè¬çãªãŠã£ãžã§ããã§ãã ããšãã°ããã®äžã«
GridView
ãã©ããããããšãã§ããŸã
ããŒãžããªããŒãããã«ããŒãžããšã®ããã²ãŒã·ã§ã³ãšãœãŒããååŸããã«ã¯ïŒ
use yii\widgets\Pjax; use yii\grid\GridView; Pjax::begin(); echo GridView::widget([ /*...*/ ]); Pjax::end();
ãªã¯ãšã¹ããšã¬ã¹ãã³ã¹
å€ãã®å éšä¿®æ£ãšæ¹åã«å ããŠããªã¯ãšã¹ããšã¬ã¹ãã³ã¹ã®ã³ã³ããŒãã³ãã«ã¯å€ãã®å€æŽãå ããããŸããã æã泚ç®ãã¹ãããšã¯ããªã¯ãšã¹ãã®æäœã次ã®ããã«ãªã£ãããšã§ãã
// GET request, , 1 $page = Yii::$app->request->get('page', 1); // POST request, , null $name = Yii::$app->request->post('name');
ãã1ã€ã®åºæ¬çãªå€æŽç¹ã¯ãã¢ããªã±ãŒã·ã§ã³ã®æåŸã«å¿çãéä¿¡ãããããã«ãªã£ãããšã§ããããã«ãããå¿çã®ããããŒãšæ¬æãå¿ èŠã«å¿ããŠä»»æã®å Žæã«å€æŽã§ããŸãã
èŠæ±ã¯ã©ã¹ã¯ãJSONãªã©ã®ããŸããŸãªåœ¢åŒã§èŠæ±æ¬æã解æã§ããããã«ãªããŸããã
ãã£ã«ã¿ãŒ
ãã£ã«ã¿ãªã³ã°ã¡ã«ããºã å šäœãåèãããŸããã ããã§ãã³ã³ãããŒã©ã¬ãã«ãšã¢ããªã±ãŒã·ã§ã³ã¬ãã«ãŸãã¯ã¢ãžã¥ãŒã«ã¬ãã«ã®äž¡æ¹ã§ã¢ã¯ã·ã§ã³ã®ãã£ã«ã¿ãªã³ã°ãæå¹ã«ã§ããŸãã ããã«ãããéå±€ãã£ã«ã¿ãŒãæŽçã§ããŸãã ããšãã°ãã¢ãžã¥ãŒã«ã«ãã£ã«ã¿ãŒãèšå®ãããšããã¹ãŠã®ã¢ã¯ã·ã§ã³ã«é©çšãããŸãã ããã«ãäžéšã®ã¢ãžã¥ãŒã«ã³ã³ãããŒã©ãŒã«å¥ã®ãã£ã«ã¿ãŒãã€ã³ã¹ããŒã«ã§ããŸãã ãã®ãã£ã«ã¿ãŒã¯ããããã®ã³ã³ãããŒã©ãŒã®ã¢ã¯ã·ã§ã³ã«ã®ã¿é©çšãããŸãã
ã³ãŒããåç·šæããããã£ã«ã¿ãŒã¯
yii\filters
åå空éã«ãªããŸããã ããšãã°ã
yii\filters\HttpBasicAtuh
ãã£ã«ã¿ãŒã䜿çšããŠãHTTP Basic AuthèªèšŒãæå¹ã«ã§ããŸãã ãããè¡ãã«ã¯ãã³ã³ãããŒã©ãŒãŸãã¯ã¢ãžã¥ãŒã«ã§èª¬æãããŠããŸãã
public function behaviors() { return [ 'basicAuth' => [ 'class' => \yii\filters\auth\HttpBasicAuth::className(), 'exclude'=> ['error'], // action "error" ], ]; }
ã³ã³ããŒãã³ãã®åæå
ã¢ããªã±ãŒã·ã§ã³ã®ã©ã€ããµã€ã¯ã«ã«éèŠãªåæåæé ïŒãããŒãã¹ãã©ãããïŒãå°å ¥ããŸããã æ¡åŒµæ©èœã¯ã
composer.json
宣èšããããšã§ããŒãã¹ãã©ããã¯ã©ã¹ãç»é²ã§ããŸãã
Application::$bootstrap
宣èšãããŠããå Žåãéåžžã®ã³ã³ããŒãã³ããããŒãã¹ãã©ããã«ç»é²ããããšãã§ããŸãã
ããŒãã¹ãã©ããã³ã³ããŒãã³ãã¯ãã¢ããªã±ãŒã·ã§ã³ããªã¯ãšã¹ãã®åŠçãéå§ããåã«ã€ã³ã¹ã¿ã³ã¹åãããŸãã ããã«ãããã³ã³ããŒãã³ãã¯ã€ãã³ããã³ãã©ãŒãç»é²ããã¢ããªã±ãŒã·ã§ã³ã®ã©ã€ããµã€ã¯ã«ã«åå ã§ããŸãã
URLãæäœãã
éçºè ã¯URLã§å€ãã®äœæ¥ãè¡ãããã
Url
ãã«ããŒã§URLé¢é£ã®ã¡ãœããã®å€§éšåãåŒãåºããããå¿«é©ãªAPIãååŸããŸããã
use yii\helpers\Url; // // : /index.php?r=management/default/users echo Url::to(''); // , action // : /index.php?r=management/default/page&id=contact echo Url::toRoute(['page', 'id' => 'contact']); // , action // : /index.php?r=management/post/index echo Url::toRoute('post/index'); // , // : /index.php?r=site/index echo Url::toRoute('/site/index'); // url `actionHiTech` // : /index.php?r=management/default/hi-tech echo Url::toRoute('hi-tech'); // url action , `DateTimeController::actionFastForward` // : /index.php?r=date-time/fast-forward&id=105 echo Url::toRoute(['/date-time/fast-forward', 'id' => 105]); // URL alias Yii::setAlias('@google', 'http://google.com/'); echo Url::to('@google/?q=yii'); // URL // : /index.php?r=management/default/users echo Url::canonical(); // URL // : /index.php?r=site/index echo Url::home(); Url::remember(); // URL Url::previous(); // URL
URLã«ãŒã«ãæ¹åãããŠããŸãã æ°ãã
yii\web\GroupUrlRule
ã¯ã©ã¹ã䜿çšããŠãã«ãŒã«ãã°ã«ãŒãåã§ããŸãã ã°ã«ãŒãããšã«ãã°ã«ãŒãå ã®ãã¹ãŠã®URLã«é©çšãããå ±éã®ãã¬ãã£ãã¯ã¹ãæå®ã§ããŸãã
new GroupUrlRule([ 'prefix' => 'admin', 'rules' => [ 'login' => 'user/login', 'logout' => 'user/logout', 'dashboard' => 'default/dashboard', ], ]); // : [ 'admin/login' => 'admin/user/login', 'admin/logout' => 'admin/user/logout', 'admin/dashboard' => 'admin/default/dashboard', ]
圹å²ããŒã¹ã®ã¢ã¯ã»ã¹å¶åŸ¡ïŒRBACïŒ
RBACã®å®è£ ãããçŽããŸããã çŸåšã¯ãå ã®RBAC NISTã¢ãã«ã«åºã¥ããŠããŸããã€ãŸãã æäœãšã¿ã¹ã¯ã®æŠå¿µãæšãŠãŸããã 代ããã«ãNISTãšããçšèªã«å¯Ÿå¿ããã¢ã¯ã»ã¹æš©ããããŸãã
äžèšã®ããã«ãåèšèšãç解ãããããžãã¹ã«ãŒã«ã決ãŸããŸãã çŸåšãã«ãŒã«ã¯åå¥ã®ã¯ã©ã¹ã§èª¬æãããŠããŸãã
翻蚳
ãŸãããã¬ãŒã ã¯ãŒã¯ã¡ãã»ãŒãžã®ç¿»èš³ã«åå ããŠããããã¹ãŠã®ã³ãã¥ããã£ã¡ã³ããŒã«æè¬ããŸãã çŸåšããããã¯26ã®èšèªã«ç¿»èš³ãããŠããŸãã ããã§ãã
ã¡ãã»ãŒãžå€æã³ã³ããŒãã³ãã¯ã䜿çšãããŠãããœãŒã¹ã«ç®çã®ã¡ãã»ãŒãžãå«ãŸããŠããªãå Žåã代æ¿ãœãŒã¹ããã¡ãã»ãŒãžãååŸããããšããŸãã ããšãã°ãã¢ããªã±ãŒã·ã§ã³ãèšèªãšããŠ
fr-CA
ã䜿çšãã翻蚳ã
fr
ã®ã¿ã®å ŽåãYiiã¯æåã«
fr-CA
翻蚳ãæ¢ããããã§èŠã€ãããªãå Žåãæ€çŽ¢ã¯
fr
ç¶è¡ããŸãã
Giiã³ãŒããžã§ãã¬ãŒã¿ãŒã«æ°ãããªãã·ã§ã³ãè¿œå ããã
Yii::t()
ã©ãããããã¡ãã»ãŒãžã§ã³ãŒããçæã§ããããã«ãªããŸããã
翻蚳æ€çŽ¢ããŒã ã¯ã
.po
ããã³ããŒã¿ããŒã¹ã«æååãèšè¿°ããæ¹æ³ãåŠã³ãŸããã
æ¡åŒµæ©èœãšããŒã«
yii2-apidoc
ããã¥ã¡ã³ããžã§ãã¬ãŒã¿ãŒæ¡åŒµæ©èœãå®è£ ããŸãããããã¯ãAPIããã¥ã¡ã³ããš
yii2-apidoc
ããã®ã¡ã€ã³ããã¥ã¡ã³ãã®äœæã«äœ¿çšã§ããŸãã ãžã§ãã¬ãŒã¿ã¯ç°¡åã«ã«ã¹ã¿ãã€ãºã§ããããŒãºã«åãããŠæ¡åŒµã§ããŸãã å ¬åŒã®ããã¥ã¡ã³ããšAPIãçæããããã«äœ¿çšããŸãã ããã§çµæãèŠãããšãã§ããŸãïŒ http : //www.yiiframework.com/doc-2.0/
ãããã¬ãŒã¯ãå€ãã®ãã€ããŒãªæ¹åãšã¡ãŒã«ãããã°ããã«ãåãåããŸããã ãŸããããŒã¿ããŒã¹ãžã®ã¯ãšãªã®æ°ãšéä¿¡ãããæåã®æ°ãããŒã«ããŒã§è¡šç€ºããããšãåŠã³ãŸããã
äžèšã®ç¿»èš³ã«é¢ããæ¹åã«å ããŠãæ°ããæ¡åŒµãžã§ãã¬ãŒã¿ãŒãGiiã«è¿œå ãããŸããã ããã«ãçæãããã³ãŒãã®ãã¬ãã¥ãŒãæ¹åãããå ŽåããããŸãã ããŒããŒããããå«ãããã¡ã€ã«ããã°ããåãæ¿ããŠå·®åãæŽæ°ã§ããŸãã ãŸããçŸåšãdiffããã®ã³ããŒãšè²Œãä»ãã«ãããäœåãªãã®ã¯ãããã¡ã«å°éããŸããã
è©ŠããŠã¿ãŠãã ããïŒ
ãããããé¡ãããŸãïŒ
Yii 2.0ã®ããŒã¿çã¯ã倧ããªå ±ååªåã«ããéæãããäž»èŠãªãã€ã«ã¹ããŒã³ã§ãã ç§ãã¡ã¯çŽ æŽãããã³ãã¥ããã£ãã倧éã®è²Žéãªã³ãŒããåãåããªããã°æåããªãã£ãã§ãããã
ãªãªãŒã¹ããŠããã ãããããšãããããŸãã