sviluppo web con yii
DESCRIPTION
Presentation about Yii, an open source, object-oriented, component-based MVC PHP web application framework. You can find information about Active Record, Model View Controller, Routing, Authentication, Scaffolding, Migration and Internationalization.TRANSCRIPT
![Page 1: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/1.jpg)
Sviluppo Web con Yii 1.1
![Page 2: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/2.jpg)
Overview
● Introduzione al framework● Active Record● Model View Controller● Routing● Autenticazione● Automatic Code Generation● Migrazioni● I18N & L10N● Aspetti negativi● Argomenti da trattare in futuro
![Page 3: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/3.jpg)
Cos'è?
Yii è un framework PHP ad alte prestazioni adatto per lo sviluppo di applicazioni Web 2.0
![Page 4: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/4.jpg)
Storia
● Ideato da Qiang Xue● Influenzato da
○ Prado framework■ Per il quale Xue ha sviluppato per molti anni■ E' stata la maggiore sorgente di idee
○ Ruby on Rails■ Per la filosofia e alcuni pattern
○ jQuery■ Perfettamente integrato in Yii
○ Symfony■ Per il sistema di estensione
○ Joomla■ Per la modularità
![Page 5: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/5.jpg)
A chi è rivolto?
● Sviluppatori per il Web ○ con qualsiasi livello di esperienza○ con conoscenze di base su
■ PHP■ Object Oriented■ MVC■ HTML
![Page 6: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/6.jpg)
Cosa si può sviluppare?
● Portali● Forums● Content management systems (CMS)● E-commerce● Social network● Web Service (Soap o RESTful) ● ...
![Page 7: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/7.jpg)
Framework "Concorrenti"
● Ruby○ Ruby on Rails○ Sinatra
● Java○ Play!○ Apache Wicket○ Apache Tapestry○ Apache Struts○ ZK○ OpenXava○ Spring○ Google Web Toolkit○ ...
![Page 8: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/8.jpg)
Framework "Concorrenti"
● Perl○ Catalyst○ Mojolicious○ Dancer○ Mason
● Pyton○ Django○ Zope 2○ web2py○ Pyramid○ TurboGears○ Pylons○ ...
![Page 9: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/9.jpg)
Framework "Concorrenti"
● Php○ Symfony○ Framewire○ Laravel○ CodeIgniter○ CakePHP○ Zend Framework○ Kajona○ FuelPHP○ Horde○ Flow3○ Joomla! Platform○ ...
![Page 10: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/10.jpg)
Framework "Concorrenti"
● ASP.NET○ ...
● C++○ ...
● ColdFusion Markup Language (CFML)○ ...
● ECMAScript○ ...
● JavaScript○ ...
![Page 11: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/11.jpg)
Perchè usarlo?
● Basso costo di apprendimento● Largo uso di buone pratiche
○ Don't Repeat Yourself○ Convention Over Configuration
● Comuntà molto attiva● Documentazione esaustiva● Crescente numero di estensioni● Coesiste bene con altri framework● Performance...
![Page 12: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/12.jpg)
Perchè usarlo?
RPS = Request Per Second
![Page 13: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/13.jpg)
Licenza BSD
● Permette di○ usare○ modificare○ redistribuire
● A patto di○ conservare la dicitura
■ Copyright © 2008-2012 by Yii Software LLC■ All rights reserved.
○ non pubblicizzarsi tramite il marchio■ Yii Software LLC
![Page 14: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/14.jpg)
Active Record
● Pattern architetturale per l'accesso al DB
● Inventato da● Proposto nel libro
○ Patterns of Enterprise Application Architecture (2002)
● Utilizzato in tutti i framework di ultima generazione
Martin Fowler
Autore e speaker internazionale sullo sviluppo di software
E' noto come l'uomo chiave nella creazione delle metodologie agili
Capo della ThoughtWorks
![Page 15: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/15.jpg)
Active Record
● Supponiamo di avere la seguente tabella:
CREATE TABLE tbl_post (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
title VARCHAR(128) NOT NULL,
content TEXT NOT NULL,
update TIMESTAMP NOT NULL,
author VARCHAR(64) NOT NULL
);
![Page 16: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/16.jpg)
Active Record● Senza il pattern
○ aggiungere un nuovo post■ $sql = "INSERT INTO tbl_post (title,
content, update, author) VALUES ($title,$content,".now().",$author);";
■ $db->execute($sql);○ modificare un post esistente
■ $sql = "UPDATE SET title=$title, content=$content, update=".now().", author=$author WHERE id=$id;";
■ $db->execute($sql);○ risulta
■ scomodo da scrivere e manutenere■ soggetto a SQL Injection
![Page 17: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/17.jpg)
Active Record● Con il pattern
○ aggiungere un nuovo post■ $post = new Post();■ $post->title = $title;■ $post->content = $content;■ $post->update = now();■ $post->author = $author;■ $post->save()
○ modificare un post esistente■ come sopra
○ risulta■ semplice da scrivere e manutenere■ non soggetto a SQL Injection
![Page 18: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/18.jpg)
Active Record
● Definizione della classe Post
class Post extends CActiveRecord
{
public function tableName()
{
return 'tbl_post';
}
}
![Page 19: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/19.jpg)
Active Record
● metodi proposti da Yii○ attributeNames()○ find($condition,$params)○ findAll($condition,$params)○ count($condition,$params)○ exists()○ save()○ update()○ delete()○ ...
![Page 20: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/20.jpg)
Model View Controller
● Pattern architteturale● Favorisce
○ separazione degli interessi○ manutenzione
● Largamente utilizzato nel web● Pattern alternativi sono
○ Model View Presenter (MVP)○ Model View ViewModel (MVVM)○ Model View Adapter (MVA)
![Page 21: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/21.jpg)
Model View Controller
![Page 22: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/22.jpg)
Model View Controller in Yii
![Page 23: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/23.jpg)
Tipico Workflow in Yii
![Page 24: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/24.jpg)
www.miosito.it/index.php?r=site/contact
class SiteController extends CController
{
public function actionContact()
{
// renderizzazione della
// pagina dei contatti
$this->render('contact');
}
}
Controller & Routing
![Page 25: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/25.jpg)
www.miosito.it/site/contact
class SiteController extends CController
{
public function actionContact()
{
// renderizzazione della
// pagina dei contatti
$this->render('contact');
}
}
Controller & Routing
![Page 26: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/26.jpg)
class SiteController extends CController
{
public function accessRules()
{
return array(
array(
$permission, /**'allows' o 'deny'**/,
'actions'=>array(/**lista di actions**/),
'users'=>array(/**tipi di utente**/),
),
);
}
}
Tipi di utente: username, * (tutti), @ (loggati), ? (non loggati)
Autenticazione
![Page 27: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/27.jpg)
Migrazioni
● Nascono con Ruby On Rails● Permettono il versionamento del DataBase● Facilitano l'evoluzione del DataBase● Migliorano la suddivisione del lavoro in un
team
![Page 28: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/28.jpg)
class m121031_094248_create_user extends CDbMigration
{
public function up()
{
$this->createTable('user', array(
'id' => 'int UNSIGNED PRIMARY KEY',
'username' => 'string NOT NULL',
'password' => 'string NOT NULL',
'email' => 'string UNIQUE NOT NULL',
'role' => 'int UNSIGNED NOT NULL'
)
);
public function down()
{
$this->dropTable('user');
return true;
}
}
![Page 29: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/29.jpg)
Migrazioni
Tizio Caio
Migrazione1 Migrazione2
Tizio Local DataBase
Caio Local DataBase
Sempronio
MergeMigrazioni
Sempronio Local
DataBase
Source Code Management (git, svn, cvs, ecc.)
deploy & esecuzione migrazioni
check-out check-in & merge
DataBase Remoto
SERVER
check-out
MergeMigrazioni
![Page 30: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/30.jpg)
I18N & L10N
● Core completamente localizzato● Localizzatore per le date● Localizzatore per i numeri● Localizzatore per le valute● Meccanismi di localizzazione di view
○ Messaggi○ Sovrascrittura delle view
![Page 31: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/31.jpg)
Messaggi
return array('Hello World' => 'Ciao Mondo'
);
● DefinizioneFile: ../../messages/it_IT/site.php
● Uso nella view
<h1> <?php echo Yii::t('site','Hello World') ?></h1>
File: ../../view/site/index.php
![Page 32: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/32.jpg)
Sovrascrittura delle view
<h1> Hello World</h1>
● View in IngleseFile: ../../view/site/index.php
● View in Italiano
<h1> Ciao Mondo</h1>
File: ../../view/site/it_IT/index.php
![Page 33: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/33.jpg)
Automatic Code Generation
● Utilizza tecniche di scaffolding● Produce strutture per
○ Creare○ Leggere○ Aggiornare○ Eliminare
● Prevede aggiustamenti post-generazione○ In contrapposizione ai sistemi basati sulle
parametrizzazioni● Utile anche per generare prototipi● In Yii si chiama Gii
![Page 34: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/34.jpg)
![Page 35: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/35.jpg)
![Page 36: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/36.jpg)
Aspetti negativi
● Fusione tra Model e ActiveRecord● Roadmap priva di date di rilascio● Progetto abbastanza nuovo (Dic 2008)● Meno opportunità di lavoro rispetto ad altri
framework○ Che però sono in crescita○ http://www.yiiframework.com/forum/index.php/forum/15-job-opportunities/
![Page 37: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/37.jpg)
Argomenti da trattare in futuro
● Estendibilità● Test driven development● Meccanismi di Caching● Meccanismi di accesso diretto al database● Produzione automatica di HTML & temi● Regole di accesso avanzate● URL rewriting
![Page 38: Sviluppo web con Yii](https://reader033.vdocuments.site/reader033/viewer/2022042813/548457415906b5c1158b4767/html5/thumbnails/38.jpg)
Grazie per l'attenzione...
Domande??