36
Y I I KEANY CHU http://keanychu.com IN ACTION

Yii in action

Embed Size (px)

DESCRIPTION

Speak about Yii Extension, Component and Module.

Citation preview

Page 1: Yii in action

Y I I KEANY CHU

http://keanychu.com

IN ACTION

Page 2: Yii in action

KeaNy Chu

fb.com/keanyc

@keany_chu

Page 3: Yii in action
Page 4: Yii in action
Page 5: Yii in action
Page 6: Yii in action
Page 7: Yii in action

let’s talk about...

● Module● Component● Extension

Page 8: Yii in action

<?php echo “Module” ?>

Page 9: Yii in action

Module

Modules are useful in several scenarios. For a large-scale

application, we may divide it into several modules, each

being developed and maintained separately. Some commonly

used features, such as user management, comment

management, may be developed in terms of modules so that

they can be reused easily in future projects.

Page 10: Yii in action

Module

A module is a self-contained software unit that consists

of models, views, controllers and other supporting

components. In many aspects, a module resembles to an

application. The main difference is that a module cannot

be deployed alone and it must reside inside of an

application. Users can access the controllers in a module

like they do with normal application controllers.

Page 11: Yii in action

File Structure

forum/

ForumModule.php the module class file

components/ containing reusable user components

views/ containing view files for widgets

controllers/ containing controller class files

DefaultController.php the default controller class file

extensions/ containing third-party extensions

models/ containing model class files

views/ containing controller view and layout files

layouts/ containing layout view files

default/ containing view files for DefaultController

index.php the index view file

Page 12: Yii in action

File Structure

forum/

ForumModule.php the module class file

components/ containing reusable user components

views/ containing view files for widgets

controllers/ containing controller class files

DefaultController.php the default controller class file

extensions/ containing third-party extensions

models/ containing model class files

views/ containing controller view and layout files

layouts/ containing layout view files

default/ containing view files for DefaultController

index.php the index view file

Page 13: Yii in action

File Structure

forum/

ForumModule.php the module class file

components/ containing reusable user components

views/ containing view files for widgets

controllers/ containing controller class files

DefaultController.php the default controller class file

extensions/ containing third-party extensions

models/ containing model class files

views/ containing controller view and layout files

layouts/ containing layout view files

default/ containing view files for DefaultController

index.php the index view file

Page 14: Yii in action

Generator in Gii

“use the module generator in Gii to create the basic

skeleton of a new module.”

Page 15: Yii in action

Config

// config/main.php

return array(

......

'modules'=>array('forum',...),

......);

Page 16: Yii in action

Config

// config/main.php

return array(

......

'modules'=>array(

'forum'=>array(

'postPerPage'=>20,

),

),

......);

Page 17: Yii in action

Usage

postPerPage=Yii::app()->controller->module->postPerPage;// or the following

if $this refers to the controller instance// $postPerPage=$this->module-

>postPerPage;

Page 18: Yii in action

<?php echo “Component” ?>

Page 19: Yii in action

Component

Yii applications are built upon components which are

objects written to a specification. A component is an

instance of CComponent or its derived class. Using a

component mainly involves accessing its properties and

raising/handling its events. The base class CComponent

specifies how to define properties and events.

Page 20: Yii in action

Code

class Document extends CComponent{ public $textWidth;}

Page 21: Yii in action

Code

class Document extends CComponent{ private $_textWidth; public function getTextWidth() { return $this->_textWidth; }}

Page 22: Yii in action

Usage

$document=new Document(); // we can write and read textWidth$document->textWidth=100;echo $document->textWidth; // we can only read textHeightecho $document->textHeight; // we can only write completed$document->completed=true;

Page 23: Yii in action

Application Component

To use an application component, we first need to change the application configuration by adding a

new entry to its components property, like the following:

Page 24: Yii in action

Application Component

return array(

// 'preload'=>array('xyz',...),

'components'=>array(

'xyz'=>array(

'class'=>'ext.xyz.XyzClass',

'property1'=>'value1',

'property2'=>'value2',

),

// other component configurations

),);

Page 25: Yii in action

<?php echo “Extensions” ?>

Page 26: Yii in action

Extensions

Extending Yii is a common activity during development. For example, when

you write a new controller, you extend Yii by inheriting its CController

class; when you write a new widget, you are extending CWidget or an

existing widget class. If the extended code is designed to be reused by

third-party developers, we call it an extension.

Page 27: Yii in action

Extensions

Using an extension usually involves the following three steps:

1. Download the extension from Yii's extension repository.

2. Unpack the extension under the extensions/xyz subdirectory of

application base directory, where xyz is the name of the extension.

3. Import, configure and use the extension.

Each extension has a name that uniquely identifies it among all extensions.

Given an extension named as xyz, we can always use the path alias ext.xyz

to locate its base directory which contains all files of xyz.

Page 28: Yii in action

Zii Extensions

Before we start describing the usage of third-party extensions, we would like to introduce the Zii

extension library, which is a set of extensions developed by the Yii developer team and included in

every release.

When using a Zii extension, one must refer to the corresponding class using a path alias in the form

ofzii.path.to.ClassName. Here the root alias zii is predefined by Yii. It refers to the root

directory of the Zii library. For example, to use CGridView, we would use the following code in a view

script when referring to the extension:

Page 29: Yii in action

Zii Extensions

$this->widget('zii.widgets.grid.CGridView', array(

'dataProvider'=>$dataProvider,));

Page 30: Yii in action

Widget

Widgets are mainly used in views. Given a widget class XyzClass belonging to the xyz extension,

we can use it in a view as follows:

Page 31: Yii in action

Usage

// widget that does not need body content

<?php $this->widget('ext.xyz.XyzClass', array(

'property1'=>'value1',

'property2'=>'value2')); ?>

Page 32: Yii in action

Usage

// widget that can contain body content

<?php $this->beginWidget('ext.xyz.XyzClass', array(

'property1'=>'value1',

'property2'=>'value2')); ?>

...body content of the widget...

<?php $this->endWidget(); ?>

Page 33: Yii in action
Page 34: Yii in action

DO NOT REINVENT THE WHEEL

Page 35: Yii in action

Q&A

Page 36: Yii in action

KeaNy Chu

http://keanychu.com

[email protected]