2. What is Yii?
• MVC Framework
– Model, View, Controller
• PHP
• Built to be fast, secure and professional
– Faster than Cake, Codeigniter, Zend1
• Lots of plumbing already in place
– Build a site quickly, don’t reinvent the wheel
• Gii GUI for quickly creating application scaffolding
– Web-based code generation
1http://www.yiiframework.com/performance/
3. How do we start?
• Basic VM image provided – run with VirtualBox
– Already set-up : CentOS 6, Nginx, PHP, MySQL, Yii
• Some initial personlisation
– Login as root, (password yii)
– Create a user
• useradd -G wheel akelk
• passwd akelk
• visudo
• Make sure wheel can run sudo
– Login as yourself (using putty)
4. Creating our site with Yii
• Run yiic
– Directory already in place, navigate to /var/www/html/yiisite
– Initially we run using sudo as all files are owned by nginx
– sudo ../../yii/framework/yiic webapp .
– Then we’ll change ownership and give ourselves write
permissions for easier development purposes
– sudo chown -R nginx:wheel .; sudo chmod -R g+w .
• Now we’re ready to visit our site
– Fire up your favourite browser
– Visit the main Yii URL (e.g. http://192.168.0.107/)
– Explore the site
5. Configuring our site
• Main config file is in protected/config/main.php
– Set up access to the mysql database
• 'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=iproperty',
'emulatePrepare' => true,
'username' => 'iproperty',
'password' => 'iproperty',
'charset' => 'utf8',
),
– Enable the Gii GUI
• 'modules'=>array(
'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>'iproperty',
'ipFilters'=>array('127.0.0.1','::1','192.168.*'),
),
),
6. Create the scaffolding
• Naïve SQL schema
– CREATE TABLE `listing` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) DEFAULT NULL,
`description` text,
`state` varchar(50) DEFAULT NULL,
`area` varchar(50) DEFAULT NULL,
`price` decimal(10,0) DEFAULT NULL,
`status` char(1) DEFAULT NULL,
`issale` tinyint(1) DEFAULT NULL,
`isrent` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
• Fire up gii and login
– http://192.168.0.107/index.php?r=gii
– Password is iproperty
• Create model (named listing)
• Create CRUD (using Listing model and listing controller)
7. Try it out
• Add a new listing or two
– Using the create action
• View the listings
– Using the view action
• Manage listings
– Using the manage action
• But no search?
– Only accessible via manage when you’re logged in
– Let’s create one!
8. Extending our site
• Create a search action
– Very similar to admin
• Allow access to all
– Edit the accessRules method
• Create a view to render it
– Similar to admin but use a CLinkColumn for the grid display
– array(
'class'=>'CLinkColumn',
'urlExpression'=>
'Yii::app()->createUrl("listing/view",array("id"=>$data->id))',
),
– Re-use _search.php from the admin action
9. Review
• For simple CRUD, all you need is a sensible database schema,
ActiveRecord and Gii; relationships will help you navigate
between objects.
• You can add new actions – remember to keep the controllers
skinny, the models fat
• There’s loads of helper classes:
– HTTP Responses (including errors)
– Caching (File, MemCache, Opcode caching)
– Data structures (Maps, Iterators, Queues)
– Validators (String, URL, Number, Email, etc)
– Web widgets (grids, forms, inputs)
– Web services (SOAP, REST)
– I18n (Translating, Messages, Date formats, etc)
• Read the documentation