Skip to content

Commit

Permalink
restructuring guide index.md and structure
Browse files Browse the repository at this point in the history
worked though all the guide files adjusted structure and
added some missing does
  • Loading branch information
cebe committed Nov 20, 2013
1 parent 987d9ab commit d775814
Show file tree
Hide file tree
Showing 27 changed files with 366 additions and 101 deletions.
Empty file removed docs/guide/application.md
Empty file.
4 changes: 4 additions & 0 deletions docs/guide/apps-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Creating your own Application structure
=======================================

TDB
83 changes: 83 additions & 0 deletions docs/guide/basics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
Basic concepts of Yii
=====================


Component and Object
--------------------

Yii 2.0 breaks the `CComponent` class in 1.1 into two classes: `Object` and `Component`.
The `Object` class is a lightweight base class that allows defining class properties
via getters and setters. The `Component` class extends from `Object` and supports
the event feature and the behavior feature.

If your class does not need the event or behavior feature, you should consider using
`Object` as the base class. This is usually the case for classes that represent basic
data structures.


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);
```

More on configuration in [configuration](configuration.md)


Path Alias
----------

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
4 changes: 4 additions & 0 deletions docs/guide/behaviors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Behaviors
=========

TDB
23 changes: 14 additions & 9 deletions docs/guide/composer.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ curl -s http://getcomposer.org/installer | php
Adding more packages to your project
------------------------------------

The act of [installing a Yii application](installing.md) creates the `composer.json` file in the root directory of your project.
In this file you list the packages that your application requires. For Yii sites, the most important part of the file is `require` the section:
The act of [installing a Yii application](installation.md) creates the `composer.json` file in the root directory of your project.
In this file you list the packages that your application requires. For Yii sites, the most important part of the file is the `require` section:

```
{
Expand All @@ -33,19 +33,23 @@ In this file you list the packages that your application requires. For Yii sites
}
```

Within the `require` section, you specify the name and version of each required package. The above example says that a version greater than or equal to 1.3 of Michaelf's PHP-Markdown package is required, as is version 4.5 or greater of Ezyang's HTMLPurifier. For details of this syntax, see the [official Composer documentation](http://getcomposer.org).
Within the `require` section, you specify the name and version of each required package.
The above example says that a version greater than or equal to 1.3 of Michaelf's PHP-Markdown package is required,
as is version 4.5 or greater of Ezyang's HTMLPurifier.
For details of this syntax, see the [official Composer documentation](http://getcomposer.org).

The full list of available Composer-supported PHP packages can be found at [packagist](http://packagist.org/). Any Yii extension can also be explicitly named using the syntax:
The full list of available Composer-supported PHP packages can be found at [packagist](http://packagist.org/).

???

Once you have edited the `composer.json`, you can invoke Composer to install the identified dependencies. For the first installation of the dependencies, use this command:
Once you have edited the `composer.json`, you can invoke Composer to install the identified dependencies.
For the first installation of the dependencies, use this command:

```
php composer.phar install
```

This must be executed within your Yii project's directory, where the `composer.json` file can be found. Depending upon your operating system and setup, you may need to provide paths to the PHP executable and to the `composer.phar` script.
This must be executed within your Yii project's directory, where the `composer.json` file can be found.
Depending upon your operating system and setup, you may need to provide paths to the PHP executable and
to the `composer.phar` script.

For an existing installation, you can have Composer update the dependencies using:

Expand All @@ -55,7 +59,8 @@ php composer.phar update

Again, you may need to provide specific path references.

In both cases, after some waiting, the required packages will be installed and ready to use in your Yii application. No additional configuration of those packages will be required.
In both cases, after some waiting, the required packages will be installed and ready to use in your Yii application.
No additional configuration of those packages will be required.


FAQ
Expand Down
23 changes: 13 additions & 10 deletions docs/guide/configuration.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
Configuration
=============

Yii applications rely upon components to perform most of the common tasks, such as connecting to a database, routing browser requests, and handling sessions. How these stock components behave can be adjusted by *configuring* your Yii application. The majority of components have sensible defaults, so it's unlikely that you'll spend a lot of time configuring
Yii applications rely upon components to perform most of the common tasks, such as connecting to a database, routing browser
requests, and handling sessions. How these stock components behave can be adjusted by *configuring* your Yii application.
The majority of components have sensible defaults, so it's unlikely that you'll spend a lot of time configuring
them. Still there are some mandatory settings, such as the database connection, that you will have to establish.

How application is configured depends on application template but there are some general principles applying in any case.
How an application is configured depends on application template but there are some general principles applying in any case.

Configuring options in bootstrap file
-------------------------------------
Expand All @@ -14,37 +16,38 @@ console applications it's `yii`. Both are doing nearly the same job:

1. Setting common constants.
2. Including Yii itself.
3. Including Composer autoloader.
3. Including [Composer autoloader](http://getcomposer.org/doc/01-basic-usage.md#autoloading).
4. Reading config file into `$config`.
5. Creating new application instance using `$config` and running it.

Bootstrap file is not the part of framework but your application so it's OK to adjust it to fit your application. Typical
The Bootstrap file is not the part of framework but your application so it's OK to adjust it to fit your application. Typical
adjustments are the value of `YII_DEBUG` that should never be `true` on production and the way config is read.

Configuring application instance
--------------------------------

It was mentioned above that application is configured in bootstrap file when its instance is created. Config is typically
stored in a PHP file in `/config` directory of the application and looks like the following:
stored in a PHP file in the `/config` directory of the application and looks like the following:

```php
<?php
return [
'id' => 'applicationId',
'basePath' => dirname(__DIR__),
'components' => [
// ...
// configuration of application components goes here...
],
'params' => require(__DIR__ . '/params.php'),
];
```

In the above array keys are names of application properties. Depending on application type you can check properties of
either `\yii\web\Application` or `\yii\console\Application`. Both are extended from `\yii\base\Application`.
either [[yii\web\Application]] or [[yii\console\Application]]. Both are extended from [[yii\base\Application]].

> Note that you can configure not only public class properties but anything accessible via setter. For example, to
configure runtime path you can use key named `runtimePath`. There's no such property in the application class but
since there's a corresponding setter named `setRuntimePath` it will be properly configured.
This feature is added to any class that extends from [[yii\base\Object]] which is nearly any class of the Yii framework.

Configuring application components
----------------------------------
Expand Down Expand Up @@ -76,11 +79,11 @@ return [

In the above four components are configured: `cache`, `user`, `errorHandler`, `log`. Each entry key is a component ID
and the value is the configuration array. ID is used to access the component like `\Yii::$app->myComponent`.
Configuration array has one special key named `class` that sets component class. The rest of the keys and values are used
Configuration array has one special key named `class` that sets the component class. The rest of the keys and values are used
to configure component properties in the same way as top-level keys are used to configure application properties.

Each application has predefined set of the components. In case of configuring one of these `class` key is omitted and
application default class is used instead. You can check `registerCoreComponents` method of the application you are using
Each application has a predefined set of components. In case of configuring one of these, the `class` key is omitted and
application default class is used instead. You can check `registerCoreComponents()` method of the application you are using
to get a list of component IDs and corresponding classes.

Note that Yii is smart enough to configure the component when it's actually used i.e. if `cache` is never used it will
Expand Down
4 changes: 4 additions & 0 deletions docs/guide/console.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Building console applications
=============================

TDB
28 changes: 26 additions & 2 deletions docs/guide/controller.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,36 @@ public SiteController extends \yii\web\Controller

After doing so you can access your action as `http://example.com/?r=site/about`.

Filters
-------

Action Filters
--------------

Action filters are implemented via behaviors. You should extend from `ActionFilter` to
define a new filter. To use a filter, you should attach the filter class to the controller
as a behavior. For example, to use the `AccessControl` filter, you should have the following
code in a controller:

```php
public function behaviors()
{
return [
'access' => [
'class' => 'yii\web\AccessControl',
'rules' => [
['allow' => true, 'actions' => ['admin'], 'roles' => ['@']],
),
),
);
}
```

more TDB

Catching all incoming requests
------------------------------

TDB

See also
--------

Expand Down
1 change: 1 addition & 0 deletions docs/guide/database-basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Yii has a database access layer built on top of PHP's [PDO](http://www.php.net/m
uniform API and solves some inconsistencies between different DBMS. By default Yii supports the following DBMS:

- [MySQL](http://www.mysql.com/)
- [MariaDB](https://mariadb.com/)
- [SQLite](http://sqlite.org/)
- [PostgreSQL](http://www.postgresql.org/)
- [CUBRID](http://www.cubrid.org/) (version 9.1.0 and higher).
Expand Down
42 changes: 42 additions & 0 deletions docs/guide/events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Events
======


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.
4 changes: 4 additions & 0 deletions docs/guide/extension.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Extending Yii
=============

TDB
2 changes: 1 addition & 1 deletion docs/guide/form.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Working with forms
==================

The primary way of using forms in Yii is through [[\yii\widgets\ActiveForm]]. This approach should be preferred when the form is based upon a model. Additionally, there are some useful methods in [[\yii\helpers\Html]] that are typically used for adding buttons and help text to any form.
The primary way of using forms in Yii is through [[yii\widgets\ActiveForm]]. This approach should be preferred when the form is based upon a model. Additionally, there are some useful methods in [[\yii\helpers\Html]] that are typically used for adding buttons and help text to any form.

When creating model-based forms, the first step is to define the model itself. The model can be either based upon the Active Record class, or the more generic Model class. For this login example, a generic model will be used:

Expand Down
25 changes: 25 additions & 0 deletions docs/guide/gii.md
Original file line number Diff line number Diff line change
@@ -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

Binary file added docs/guide/images/flow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/guide/images/flow.vsd
Binary file not shown.
Binary file added docs/guide/images/structure.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/guide/images/structure.vsd
Binary file not shown.
Loading

0 comments on commit d775814

Please sign in to comment.