Paul Klimov
11 years ago
59 changed files with 879 additions and 310 deletions
@ -0,0 +1,4 @@
|
||||
Creating your own Application structure |
||||
======================================= |
||||
|
||||
TDB |
@ -0,0 +1,117 @@
|
||||
Managing assets |
||||
=============== |
||||
|
||||
An asset in Yii is a file that is included into the page. It could be CSS, JavaScript or |
||||
any other file. Framework provides many ways to work with assets from basics such as adding `<script src="` tag |
||||
for a file that is [handled by View](view.md) section to advanced usage such as pusblishing files that are not |
||||
under webserve document root, resolving JavaScript dependencies or minifying CSS. |
||||
|
||||
Declaring asset bundle |
||||
---------------------- |
||||
|
||||
In order to publish some assets you should declare an asset bundle first. The bundle defines a set of asset files or |
||||
directories to be published and their dependencies on other asset bundles. |
||||
|
||||
Both basic and advanced application templates contain `AppAsset` asset bundle class that defines assets required |
||||
applicationwide. Let's review basic application asset bundle class: |
||||
|
||||
```php |
||||
class AppAsset extends AssetBundle |
||||
{ |
||||
public $basePath = '@webroot'; |
||||
public $baseUrl = '@web'; |
||||
public $css = [ |
||||
'css/site.css', |
||||
]; |
||||
public $js = [ |
||||
]; |
||||
public $depends = [ |
||||
'yii\web\YiiAsset', |
||||
'yii\bootstrap\BootstrapAsset', |
||||
]; |
||||
} |
||||
``` |
||||
|
||||
In the above `$basePath` specifies web-accessible directory assets are served from. It is a base for relative |
||||
`$css` and `$js` paths i.e. `@webroot/css/site.css` for `css/site.css`. Here `@webroot` is an alias that points to |
||||
application's `web` directory. |
||||
|
||||
`$baseUrl` is used to specify base URL for the same relative `$css` and `$js` i.e. `@web/css/site.css` where `@web` |
||||
is an alias that corresponds to your website base URL such as `http://example.com/`. |
||||
|
||||
In case you have asset files under non web accessible directory, that is the case for any extension, you need |
||||
to additionally specify `$sourcePath`. Files will be copied or symlinked from source bath to base path prior to being |
||||
registered. In case source path is used `baseUrl` is generated automatically at the time of publising asset bundle. |
||||
|
||||
Dependencies on other asset bundles are specified via `$depends` property. It is an array that contains fully qualified |
||||
names of bundle classes that should be published in order for this bundle to work properly. |
||||
|
||||
Here `yii\web\YiiAsset` adds Yii's JavaScript library while `yii\bootstrap\BootstrapAsset` includes |
||||
[Bootstrap](http://getbootstrap.com) frontend framework. |
||||
|
||||
Asset bundles are regular classes so if you need to define another one, just create alike class with unique name. This |
||||
class can be placed anywhere but the convention for it is to be under `assets` directory of the applicaiton. |
||||
|
||||
Registering asset bundle |
||||
------------------------ |
||||
|
||||
Asset bundle classes are typically registered in views or, if it's main application asset, in layout. Doing it is |
||||
as simple as: |
||||
|
||||
```php |
||||
use app\assets\AppAsset; |
||||
AppAsset::register($this); |
||||
``` |
||||
|
||||
Since we're in a view context `$this` refers to `View` class. |
||||
|
||||
Overriding asset bundles |
||||
------------------------ |
||||
|
||||
Sometimes you need to override some asset bundles application wide. A good example is loading jQuery from CDN instead |
||||
of your own server. In order to do it we need to configure `assetManager` application component via config file. In case |
||||
of basic application it is `config/web.php`: |
||||
|
||||
```php |
||||
return [ |
||||
// ... |
||||
'components' => [ |
||||
'assetManager' => [ |
||||
'bundles' => [ |
||||
'yii\web\JqueryAsset' => [ |
||||
'sourcePath' => null, |
||||
'js' => ['//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js'] |
||||
], |
||||
], |
||||
], |
||||
], |
||||
]; |
||||
``` |
||||
|
||||
In the above we're adding asset bundle definitions to `bunldes` property of asset manager. Keys there are fully |
||||
qualified class paths to asset bundle classes we want to override while values are key-value arrays of class properties |
||||
and corresponding values to set. |
||||
|
||||
Setting `sourcePath` to `null` tells asset manager not to copy anything while `js` overrides local files with a link |
||||
to CDN. |
||||
|
||||
Enabling symlinks |
||||
----------------- |
||||
|
||||
Asset manager is able to use symlinks instead of copying files. It is turned off by default since symlinks are often |
||||
disabled on shared hosting. If your hosting environment supports symlinks you certainly should enable the feature via |
||||
application config: |
||||
|
||||
```php |
||||
return [ |
||||
// ... |
||||
'components' => [ |
||||
'assetManager' => [ |
||||
'linkAssets' => true, |
||||
], |
||||
], |
||||
]; |
||||
``` |
||||
|
||||
There are two main benefits in enabling it. First it is faster since no copying is required and second is that assets |
||||
will always be up to date with source files. |
@ -0,0 +1,86 @@
|
||||
Basic concepts of Yii |
||||
===================== |
||||
|
||||
|
||||
Component and Object |
||||
-------------------- |
||||
|
||||
Classes of the Yii framework usually extend from one of the two base classes [[Object]] and [[Component]]. |
||||
These classes provide useful features that are added automatically to all classes extending from them. |
||||
|
||||
The `Object` class provides the [configuration and property feature](../api/base/Object.md). |
||||
The `Component` class extends from `Object` and adds [event handling](events.md) and [behaviors](behaviors.md). |
||||
|
||||
`Object` is usually used for classes that represent basic data structures while `Component` is used for |
||||
application components and other classes that implement higher logic. |
||||
|
||||
|
||||
Object Configuration |
||||
-------------------- |
||||
|
||||
The [[Object]] class introduces a uniform way of configuring objects. Any descendant class |
||||
of [[Object]] should declare its constructor (if needed) in the following way so that |
||||
it can be properly configured: |
||||
|
||||
```php |
||||
class MyClass extends \yii\base\Object |
||||
{ |
||||
public function __construct($param1, $param2, $config = []) |
||||
{ |
||||
// ... initialization before configuration is applied |
||||
|
||||
parent::__construct($config); |
||||
} |
||||
|
||||
public function init() |
||||
{ |
||||
parent::init(); |
||||
|
||||
// ... initialization after configuration is applied |
||||
} |
||||
} |
||||
``` |
||||
|
||||
In the above, the last parameter of the constructor must take a configuration array |
||||
which contains name-value pairs for initializing the properties at the end of the constructor. |
||||
You can override the `init()` method to do initialization work that should be done after |
||||
the configuration is applied. |
||||
|
||||
By following this convention, you will be able to create and configure a new object |
||||
using a configuration array like the following: |
||||
|
||||
```php |
||||
$object = Yii::createObject([ |
||||
'class' => 'MyClass', |
||||
'property1' => 'abc', |
||||
'property2' => 'cde', |
||||
], $param1, $param2); |
||||
``` |
||||
|
||||
|
||||
Path Aliases |
||||
------------ |
||||
|
||||
Yii 2.0 expands the usage of path aliases to both file/directory paths and URLs. An alias |
||||
must start with a `@` character so that it can be differentiated from file/directory paths and URLs. |
||||
For example, the alias `@yii` refers to the Yii installation directory. Path aliases are |
||||
supported in most places in the Yii core code. For example, `FileCache::cachePath` can take |
||||
both a path alias and a normal directory path. |
||||
|
||||
Path alias is also closely related with class namespaces. It is recommended that a path |
||||
alias be defined for each root namespace so that you can use Yii the class autoloader without |
||||
any further configuration. For example, because `@yii` refers to the Yii installation directory, |
||||
a class like `yii\web\Request` can be autoloaded by Yii. If you use a third party library |
||||
such as Zend Framework, you may define a path alias `@Zend` which refers to its installation |
||||
directory and Yii will be able to autoload any class in this library. |
||||
|
||||
|
||||
Autoloading |
||||
----------- |
||||
|
||||
TBD |
||||
|
||||
Helper classes |
||||
-------------- |
||||
|
||||
TDB |
@ -0,0 +1,4 @@
|
||||
Building console applications |
||||
============================= |
||||
|
||||
TDB |
@ -0,0 +1,43 @@
|
||||
Events |
||||
====== |
||||
|
||||
TBD, see also [Component.md](../api/base/Component.md). |
||||
|
||||
There is no longer the need to define an `on`-method in order to define an event in Yii 2.0. |
||||
Instead, you can use whatever event names. To attach a handler to an event, you should |
||||
use the `on` method now: |
||||
|
||||
```php |
||||
$component->on($eventName, $handler); |
||||
// To detach the handler, use: |
||||
// $component->off($eventName, $handler); |
||||
``` |
||||
|
||||
|
||||
When you attach a handler, you can now associate it with some parameters which can be later |
||||
accessed via the event parameter by the handler: |
||||
|
||||
```php |
||||
$component->on($eventName, $handler, $params); |
||||
``` |
||||
|
||||
|
||||
Because of this change, you can now use "global" events. Simply trigger and attach handlers to |
||||
an event of the application instance: |
||||
|
||||
```php |
||||
Yii::$app->on($eventName, $handler); |
||||
.... |
||||
// this will trigger the event and cause $handler to be invoked. |
||||
Yii::$app->trigger($eventName); |
||||
``` |
||||
|
||||
If you need to handle all instances of a class instead of the object you can attach a handler like the following: |
||||
|
||||
```php |
||||
Event::on(ActiveRecord::className(), ActiveRecord::EVENT_AFTER_INSERT, function ($event) { |
||||
Yii::trace(get_class($event->sender) . ' is inserted.'); |
||||
}); |
||||
``` |
||||
|
||||
The code above defines a handler that will be triggered for every Active Record object's `EVENT_AFTER_INSERT` event. |
@ -0,0 +1,25 @@
|
||||
The Gii code generation tool |
||||
============================ |
||||
|
||||
Yii2 includes a handy tool that allows rapid prototyping by generating commonly used code snippets |
||||
as well as complete CRUD controllers. |
||||
|
||||
Installing and configuring |
||||
-------------------------- |
||||
|
||||
How to use it |
||||
------------- |
||||
|
||||
Add these lines to your config file: |
||||
|
||||
```php |
||||
'modules' => [ |
||||
'gii' => ['yii\gii\Module'] |
||||
] |
||||
``` |
||||
|
||||
Creating your own templates |
||||
--------------------------- |
||||
|
||||
TDB |
||||
|
After Width: | Height: | Size: 27 KiB |
Binary file not shown.
After Width: | Height: | Size: 5.9 KiB |
Binary file not shown.
@ -1,68 +0,0 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\jui; |
||||
|
||||
use Yii; |
||||
use yii\helpers\Html; |
||||
|
||||
/** |
||||
* Slider renders a slider jQuery UI widget. |
||||
* |
||||
* For example: |
||||
* |
||||
* ```php |
||||
* echo Slider::widget([ |
||||
* 'model' => $model, |
||||
* 'attribute' => 'amount', |
||||
* 'clientOptions' => [ |
||||
* 'min' => 1, |
||||
* 'max' => 10, |
||||
* ], |
||||
* ]); |
||||
* ``` |
||||
* |
||||
* The following example will use the name property instead: |
||||
* |
||||
* ```php |
||||
* echo Slider::widget([ |
||||
* 'name' => 'amount', |
||||
* 'clientOptions' => [ |
||||
* 'min' => 1, |
||||
* 'max' => 10, |
||||
* ], |
||||
* ]); |
||||
*``` |
||||
* |
||||
* @see http://api.jqueryui.com/slider/ |
||||
* @author Alexander Makarov <sam@rmcreative.ru> |
||||
* @since 2.0 |
||||
*/ |
||||
class Slider extends InputWidget |
||||
{ |
||||
/** |
||||
* Renders the widget. |
||||
*/ |
||||
public function run() |
||||
{ |
||||
echo $this->renderWidget(); |
||||
$this->registerWidget('slider', SliderAsset::className()); |
||||
} |
||||
|
||||
/** |
||||
* Renders the Slider widget. |
||||
* @return string the rendering result. |
||||
*/ |
||||
public function renderWidget() |
||||
{ |
||||
if ($this->hasModel()) { |
||||
return Html::activeTextInput($this->model, $this->attribute, $this->options); |
||||
} else { |
||||
return Html::textInput($this->name, $this->value, $this->options); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue