You are on page 1of 33

Installazione di Yii - YII2 - La guida italiana https://milicia4.github.

io/Yii2-Ita/getting-started/sub-installing-Yii/

https://getcomposer.org/download/

curl -sS https://getcomposer.org/in staller | php


mv composer.phar /usr/local/bin /composer

1 di 5 10/07/2019 21.49
Installazione di Yii - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-installing-Yii/

composer

composer
self-update

composer global require "fxp/composer-asset-plugin:^1.3.1"


composer create-project --prefer-dist yiisoft/yii2-app-basic basic

basic

2 di 5 10/07/2019 21.49
Installazione di Yii - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-installing-Yii/

config/web.php
cookieValidationKey

// !!! insert a secret key in the following (if it is empty) - this


is required
by cookie validation

'cookieValidationKey' => 'enter your secret key here',

web

php yii serve

http://localhost:8080/

3 di 5 10/07/2019 21.49
Installazione di Yii - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-installing-Yii/

\requirements.php /web/requirements.php
http://localhost
/requirements.php

cd basic
php requirements.php

4 di 5 10/07/2019 21.49
Installazione di Yii - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-installing-Yii/

http://www.example.com/index.php
http://www.example.com/basic/web/index.php

basic/web
index.php

5 di 5 10/07/2019 21.49
Iniziamo con un "Ciao" - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-saying-hello/

say
message

say SiteController
controllers/SiteControlle.php

<?php

namespace app\controllers;

use yii\web\Controller;

class SiteController extends Controller{

1 di 4 11/07/2019 16.56
Iniziamo con un "Ciao" - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-saying-hello/

// ...existing code...

public function actionSay($message = 'Hello'){

return $this->render('say', ['message' => $message]);

}
}

say
actionSay SiteController action

action

create-comment

action
create-comment
actionCreateComment

say $message

<?php
use yii\helpers\Html;
?>
<?= Html::encode($message) ?>

2 di 4 11/07/2019 16.56
Iniziamo con un "Ciao" - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-saying-hello/

say views/site/say.php

views/ControllerID/ViewsName.php

$message

http://hostname/index.php?r=site%2Fsay&message=Hello+World

3 di 4 11/07/2019 16.56
Iniziamo con un "Ciao" - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-saying-hello/

message actionSay()

say views/layouts/main.php

ControllerID/ActionID
ControllerID

ActionID
site/say
SiteController say
actionSay()

4 di 4 11/07/2019 16.56
Lavoriamo con i moduli - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-working-with-forms/

EntryForm
models/EntryForm.php

<?php

namespace app\models;

use Yii;
use yii\base\Model;

1 di 7 11/07/2019 16.57
Lavoriamo con i moduli - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-working-with-forms/

class EntryForm extends Model{

public $name;
public $email;

public function rules(){

return [
[['name', 'email'], 'required'],
['email', 'email'],
];
}
}

EntryForm name email

rules()

name email

email

EntryForm

<?php
$model = new EntryForm();
$model->name = 'Qiang';
$model->email = 'bad';
if ($model->validate()) {
// Good!
} else {
// Failure!
// Use $model->getErrors()

2 di 7 11/07/2019 16.57
Lavoriamo con i moduli - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-working-with-forms/

}
?>

entry
SiteController

<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\models\EntryForm;

class SiteController extends Controller{

// ...existing code...

public function actionEntry(){

$model = new EntryForm();

if ($model->load(Yii::$app->request->post()) &&
$model->validate()) {
// valid data received in $model

// do something meaningful here about $model ...

return $this->render('entry-confirm', ['model' => $model]);


} else {
// either the page is initially displayed or there is some
validation error
return $this->render('entry', ['model' => $model]);
}
}
}

3 di 7 11/07/2019 16.57
Lavoriamo con i moduli - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-working-with-forms/

EntryForm

entry-confirm

entry

entry-form entry

views/site
/entry-confirm.php

<?php
use yii\helpers\Html;
?>
<p>You have entered the following information:</p>

<ul>

4 di 7 11/07/2019 16.57
Lavoriamo con i moduli - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-working-with-forms/

<li><label>Name</label>: <?= Html::encode($model->name) ?></li>


<li><label>Email</label>: <?= Html::encode($model->email) ?></li>
</ul>

entry

views/site/entry.php

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin(); ?>

<?= $form->field($model, 'name') ?>

<?= $form->field($model, 'email') ?>

<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn
btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>

entry.php
begin() end()

field()

http://hostname/index.php?r=site%2Fentry

5 di 7 11/07/2019 16.57
Lavoriamo con i moduli - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-working-with-forms/

6 di 7 11/07/2019 16.57
Lavoriamo con i moduli - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-working-with-forms/

7 di 7 11/07/2019 16.57
Lavoriamo con i database - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-working-with-database/

country

yii2basic

country

1 di 8 11/07/2019 16.57
Lavoriamo con i database - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-working-with-database/

CREATE TABLE `country` (


`code` CHAR(2) NOT NULL PRIMARY KEY,
`name` CHAR(52) NOT NULL,
`population` INT(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `country` VALUES ('AU','Australia',24016400);


INSERT INTO `country` VALUES ('BR','Brazil',205722000);
INSERT INTO `country` VALUES ('CA','Canada',35985751);
INSERT INTO `country` VALUES ('CN','China',1375210000);
INSERT INTO `country` VALUES ('DE','Germany',81459000);
INSERT INTO `country` VALUES ('FR','France',64513242);
INSERT INTO `country` VALUES ('GB','United Kingdom',65097000);
INSERT INTO `country` VALUES ('IN','India',1285400000);
INSERT INTO `country` VALUES ('RU','Russia',146519759);
INSERT INTO `country` VALUES ('US','United States',322976000);

yii2basic
country

config/db.php

<?php

return [
'class' => 'yii\db\Connection',

2 di 8 11/07/2019 16.57
Lavoriamo con i database - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-working-with-database/

'dsn' => 'mysql:host=localhost;dbname=yii2basic',


'username' => 'root',
'password' => '',
'charset' => 'utf8',
];

config/db.php

country
Country
models/Country.php

<?php

namespace app\models;

use yii\db\ActiveRecord;

class Country extends ActiveRecord


{
}

Country

3 di 8 11/07/2019 16.57
Lavoriamo con i database - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-working-with-database/

Country
country

use app\models\Country;

// get all rows from the country table and order them by "name"
$countries = Country::find()->orderBy('name')->all();

// get the row whose primary key is "US"


$country = Country::findOne('US');

// displays "United States"


echo $country->name;

// modifies the country name to be "U.S.A." and save it to database


$country->name = 'U.S.A.';
$country->save();

4 di 8 11/07/2019 16.57
Lavoriamo con i database - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-working-with-database/

site

CountryController index

<?php

namespace app\controllers;

use yii\web\Controller;
use yii\data\Pagination;
use app\models\Country;

class CountryController extends Controller{

public function actionIndex(){

$query = Country::find();

$pagination = new Pagination([


'defaultPageSize' => 5,
'totalCount' => $query->count(),
]);

$countries = $query->orderBy('name')
->offset($pagination->offset)
->limit($pagination->limit)
->all();

return $this->render('index', [
'countries' => $countries,
'pagination' => $pagination,
]);
}
}

5 di 8 11/07/2019 16.57
Lavoriamo con i database - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-working-with-database/

controller/CountryController.php index

country

Pagination

offset limit

index index

views
country
country views/country
index.php

<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
?>
<h1>Countries</h1>
<ul>
<?php foreach ($countries as $country): ?>
<li>
<?= Html::encode("{$country->name} ({$country->code})") ?>:
<?= $country->population ?>
</li>
<?php endforeach; ?>

6 di 8 11/07/2019 16.57
Lavoriamo con i database - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-working-with-database/

</ul>

<?= LinkPager::widget(['pagination' => $pagination]) ?>

LinkPager

http://hostname/index.php?r=country%2Findex

7 di 8 11/07/2019 16.57
Lavoriamo con i database - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-working-with-database/

http://hostname/index.php?r=country%2Findex&page=2

8 di 8 11/07/2019 16.57
Generare codice con Gii - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-generate-code-gii/

config/web.php

$config = [ ... ];

if (YII_ENV_DEV) {
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];

1 di 9 11/07/2019 16.57
Generare codice con Gii - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-generate-code-gii/

gii
web/index.php
YII_ENV_DEV true

defined('YII_ENV') or define('YII_ENV', 'dev');

http://hostname/index.php?r=gii

2 di 9 11/07/2019 16.57
Generare codice con Gii - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-generate-code-gii/

country

Country

3 di 9 11/07/2019 16.57
Generare codice con Gii - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-generate-code-gii/

models/Country.php

diff

4 di 9 11/07/2019 16.57
Generare codice con Gii - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-generate-code-gii/

5 di 9 11/07/2019 16.57
Generare codice con Gii - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-generate-code-gii/

app\models\Country

app\models\CountrySearch

app\controllers\CountryController

6 di 9 11/07/2019 16.57
Generare codice con Gii - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-generate-code-gii/

controllers/CountryController.php
views/country/index.php

http://hostname/index.php?r=country%2Findex

7 di 9 11/07/2019 16.57
Generare codice con Gii - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-generate-code-gii/

8 di 9 11/07/2019 16.57
Generare codice con Gii - YII2 - La guida italiana https://milicia4.github.io/Yii2-Ita/getting-started/sub-generate-code-gii/

controllers/CountryController.php

models/Country.php models/CountrySearch.php

views/country/*.php

9 di 9 11/07/2019 16.57