Browse Source
Updated AR classes with the latest changes from master to go on with separation of AR query classes in traits. Conflicts: framework/yii/db/ActiveQuery.php framework/yii/db/ActiveRecord.php framework/yii/db/ActiveRelation.phptags/2.0.0-beta
Carsten Brandt
11 years ago
549 changed files with 34807 additions and 48455 deletions
@ -0,0 +1,68 @@
|
||||
Authentication |
||||
============== |
||||
|
||||
Authentication is the act of verifying who a user is, and is the basis of the login process. Typically, authentication uses an identifier--a username or email address--and password, submitted through a form. The application then compares this information against that previously stored. |
||||
|
||||
In Yii all this is done semi-automatically, leaving the developer to merely implement [[\yii\web\IdentityInterface]]. Typically, implementation is accomplished using the `User` model. You can find a full featured example in the |
||||
[advanced application template](installation.md). Below only the interface methods are listed: |
||||
|
||||
```php |
||||
class User extends ActiveRecord implements IdentityInterface |
||||
{ |
||||
// ... |
||||
|
||||
/** |
||||
* Finds an identity by the given ID. |
||||
* |
||||
* @param string|integer $id the ID to be looked for |
||||
* @return IdentityInterface|null the identity object that matches the given ID. |
||||
*/ |
||||
public static function findIdentity($id) |
||||
{ |
||||
return static::find($id); |
||||
} |
||||
|
||||
/** |
||||
* @return int|string current user ID |
||||
*/ |
||||
public function getId() |
||||
{ |
||||
return $this->id; |
||||
} |
||||
|
||||
/** |
||||
* @return string current user auth key |
||||
*/ |
||||
public function getAuthKey() |
||||
{ |
||||
return $this->auth_key; |
||||
} |
||||
|
||||
/** |
||||
* @param string $authKey |
||||
* @return boolean if auth key is valid for current user |
||||
*/ |
||||
public function validateAuthKey($authKey) |
||||
{ |
||||
return $this->getAuthKey() === $authKey; |
||||
} |
||||
} |
||||
``` |
||||
|
||||
Two of the outlined methods are simple: `findIdentity` is provided with an ID and returns a model instance represented by that ID. The `getId` method returns the ID itself. |
||||
Two of the other methods--`getAuthKey` and `validateAuthKey`--are used to provide extra security to the "remember me" cookie. `getAuthKey` should return a string that is unique for each user. A good idea is to save this value when the user is created by using Yii's `Security::generateRandomKey()`: |
||||
|
||||
```php |
||||
public function beforeSave($insert) |
||||
{ |
||||
if (parent::beforeSave($insert)) { |
||||
if ($this->isNewRecord) { |
||||
$this->auth_key = Security::generateRandomKey(); |
||||
} |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
``` |
||||
|
||||
The `validateAuthKey` method just compares the `$authKey` variable, passed as parameter (itself retrieved from a cookie), with the value fetched from database. |
@ -0,0 +1,124 @@
|
||||
Authorization |
||||
============= |
||||
|
||||
Authorization is the process of verifying that user has enough permissions to do something. Yii provides several methods |
||||
of controlling it. |
||||
|
||||
Access control basics |
||||
--------------------- |
||||
|
||||
Basic access control is very simple to implement using [[\yii\web\AccessControl]]: |
||||
|
||||
```php |
||||
class SiteController extends Controller |
||||
{ |
||||
public function behaviors() |
||||
{ |
||||
return [ |
||||
'access' => [ |
||||
'class' => \yii\web\AccessControl::className(), |
||||
'only' => ['login', 'logout', 'signup'], |
||||
'rules' => [ |
||||
[ |
||||
'actions' => ['login', 'signup'], |
||||
'allow' => true, |
||||
'roles' => ['?'], |
||||
], |
||||
[ |
||||
'actions' => ['logout'], |
||||
'allow' => true, |
||||
'roles' => ['@'], |
||||
], |
||||
], |
||||
], |
||||
]; |
||||
} |
||||
// ... |
||||
``` |
||||
|
||||
In the code above we're attaching access control behavior to a controller. Since there's `only` option specified, it |
||||
will be applied to 'login', 'logout' and 'signup' actions only. A set of rules that are basically options for |
||||
[[\yii\web\AccessRule]] reads as follows: |
||||
|
||||
- Allow all guest (not yet authenticated) users to access 'login' and 'signup' actions. |
||||
- Allow authenticated users to access 'logout' action. |
||||
|
||||
Rules are checked one by one from top to bottom. If rule matches, action takes place immediately. If not, next rule is |
||||
checked. If no rules matched access is denied. |
||||
|
||||
[[\yii\web\AccessRule]] is quite flexible and allows additionally to what was demonstrated checking IPs and request method |
||||
(i.e. POST, GET). If it's not enough you can specify your own check via anonymous function: |
||||
|
||||
```php |
||||
class SiteController extends Controller |
||||
{ |
||||
public function behaviors() |
||||
{ |
||||
return [ |
||||
'access' => [ |
||||
'class' => \yii\web\AccessControl::className(), |
||||
'only' => ['special'], |
||||
'rules' => [ |
||||
[ |
||||
'actions' => ['special'], |
||||
'allow' => true, |
||||
'matchCallback' => function ($rule, $action) { |
||||
return date('d-m') === '31-10'; |
||||
} |
||||
], |
||||
``` |
||||
|
||||
Sometimes you want a custom action to be taken when access is denied. In this case you can specify `denyCallback`. |
||||
|
||||
Role based access control (RBAC) |
||||
-------------------------------- |
||||
|
||||
Role based access control is very flexible approach to controlling access that is a perfect match for complex systems |
||||
where permissions are customizable. |
||||
|
||||
In order to start using it some extra steps are required. First of all we need to configure `authManager` application |
||||
component: |
||||
|
||||
```php |
||||
|
||||
``` |
||||
|
||||
Then create permissions hierarchy. |
||||
|
||||
Specify roles from RBAC in controller's access control configuration or call [[User::checkAccess()]] where appropriate. |
||||
|
||||
### How it works |
||||
|
||||
TBD: write about how it works with pictures :) |
||||
|
||||
### Avoiding too much RBAC |
||||
|
||||
In order to keep auth hierarchy simple and efficient you should avoid creating and using too much nodes. Most of the time |
||||
simple checks could be used instead. For example such code that uses RBAC: |
||||
|
||||
```php |
||||
public function editArticle($id) |
||||
{ |
||||
$article = Article::find($id); |
||||
if (!$article) { |
||||
throw new HttpException(404); |
||||
} |
||||
if (!\Yii::$app->user->checkAccess('edit_article', ['article' => $article])) { |
||||
throw new HttpException(403); |
||||
} |
||||
// ... |
||||
} |
||||
``` |
||||
|
||||
can be replaced with simpler code that doesn't use RBAC: |
||||
|
||||
```php |
||||
public function editArticle($id) |
||||
{ |
||||
$article = Article::find(['id' => $id, 'author_id' => \Yii::$app->user->id]); |
||||
if (!$article) { |
||||
throw new HttpException(404); |
||||
} |
||||
// ... |
||||
} |
||||
``` |
@ -0,0 +1,76 @@
|
||||
Composer |
||||
======== |
||||
|
||||
Yii2 uses Composer as its package manager. Composer is a PHP utility that can automatically handle the installation of needed libraries and |
||||
extensions, thereby keeping those third-party resources up to date while absolving you of the need to manually manage the project's dependencies. |
||||
|
||||
Installing Composer |
||||
------------------- |
||||
|
||||
In order to install Composer, check the official guide for your operating system: |
||||
|
||||
* [Linux](http://getcomposer.org/doc/00-intro.md#installation-nix) |
||||
* [Windows](http://getcomposer.org/doc/00-intro.md#installation-windows) |
||||
|
||||
All of the details can be found in the guide, but you'll either download Composer directly from [http://getcomposer.org/](http://getcomposer.org/), or run the following command: |
||||
|
||||
``` |
||||
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: |
||||
|
||||
``` |
||||
{ |
||||
"require": { |
||||
"Michelf/php-markdown": ">=1.3", |
||||
"ezyang/htmlpurifier": ">=4.5.0" |
||||
} |
||||
} |
||||
``` |
||||
|
||||
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: |
||||
|
||||
??? |
||||
|
||||
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. |
||||
|
||||
For an existing installation, you can have Composer update the dependencies using: |
||||
|
||||
``` |
||||
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. |
||||
|
||||
|
||||
FAQ |
||||
--- |
||||
|
||||
### Getting "You must enable the openssl extension to download files via https" |
||||
|
||||
If you're using WAMP check [this answer at StackOverflow](http://stackoverflow.com/a/14265815/1106908). |
||||
|
||||
### Getting "Failed to clone <URL here>, git was not found, check that it is installed and in your Path env." |
||||
|
||||
Either install git or try adding `--prefer-dist` to the end of `install` or `update` command. |
||||
|
||||
|
||||
See also |
||||
-------- |
||||
|
||||
- [Official Composer documentation](http://getcomposer.org). |
@ -1,3 +1,90 @@
|
||||
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. |
||||
|
||||
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: |
||||
|
||||
```php |
||||
use yii\base\Model; |
||||
|
||||
class LoginForm extends Model |
||||
{ |
||||
public $username; |
||||
public $password; |
||||
|
||||
/** |
||||
* @return array the validation rules. |
||||
*/ |
||||
public function rules() |
||||
{ |
||||
return [ |
||||
// username and password are both required |
||||
['username, password', 'required'], |
||||
// password is validated by validatePassword() |
||||
['password', 'validatePassword'], |
||||
]; |
||||
} |
||||
|
||||
/** |
||||
* Validates the password. |
||||
* This method serves as the inline validation for password. |
||||
*/ |
||||
public function validatePassword() |
||||
{ |
||||
$user = User::findByUsername($this->username); |
||||
if (!$user || !$user->validatePassword($this->password)) { |
||||
$this->addError('password', 'Incorrect username or password.'); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Logs in a user using the provided username and password. |
||||
* @return boolean whether the user is logged in successfully |
||||
*/ |
||||
public function login() |
||||
{ |
||||
if ($this->validate()) { |
||||
$user = User::findByUsername($this->username); |
||||
return true; |
||||
} else { |
||||
return false; |
||||
} |
||||
} |
||||
} |
||||
``` |
||||
|
||||
The controller will pass an instance of that model to the view, wherein the Active Form widget is used: |
||||
|
||||
```php |
||||
use yii\helpers\Html; |
||||
use yii\widgets\ActiveForm; |
||||
|
||||
<?php $form = ActiveForm::begin([ |
||||
'id' => 'login-form', |
||||
'options' => ['class' => 'form-horizontal'], |
||||
]) ?> |
||||
<?= $form->field($model, 'username') ?> |
||||
<?= $form->field($model, 'password')->passwordInput() ?> |
||||
|
||||
<div class="form-group"> |
||||
<div class="col-lg-offset-1 col-lg-11"> |
||||
<?= Html::submitButton('Login', ['class' => 'btn btn-primary']) ?> |
||||
</div> |
||||
</div> |
||||
<?php ActiveForm::end() ?> |
||||
``` |
||||
|
||||
In the above code, `ActiveForm::begin()` not only creates a form instance, but also marks the beginning of the form. All of the content |
||||
placed between `ActiveForm::begin()` and `ActiveForm::end()` will be wrapped within the `<form>` tag. As with any widget, you can specify some options as to how the widget should be configured by passing an array to the `begin` method. In this case, an extra CSS class and identifying ID are passed to be used in the opening `<form>` tag. |
||||
|
||||
In order to create a form element in the form, along with the element's label, and any application JavaScript validation, the `field` method of the Active Form widget is called. When the invocation of this method is echoed directly, the result is a regular (text) input. To |
||||
customize the output, you can chain additional methods to this call: |
||||
|
||||
```php |
||||
<?= $form->field($model, 'password')->passwordInput() ?> |
||||
|
||||
// or |
||||
|
||||
<?= $form->field($model, 'username')->textInput()->hint('Please enter your name')->label('Name') ?> |
||||
``` |
||||
|
@ -0,0 +1,32 @@
|
||||
Twitter Bootstrap Extension for Yii 2 |
||||
===================================== |
||||
|
||||
This is the Twitter Bootstrap extension for Yii 2. It encapsulates Bootstrap components |
||||
and plugins in terms of Yii widgets, and thus makes using Bootstrap components/plugins |
||||
in Yii applications extremely easy. For example, the following |
||||
single line of code in a view file would render a Bootstrap Progress plugin: |
||||
|
||||
```php |
||||
<?= yii\bootstrap\Progress::widget(['percent' => 60, 'label' => 'test']) ?> |
||||
``` |
||||
|
||||
|
||||
Installation |
||||
------------ |
||||
|
||||
The preferred way to install this extension is through [composer](http://getcomposer.org/download/). |
||||
|
||||
Either run |
||||
|
||||
``` |
||||
php composer.phar require yiisoft/yii2-bootstrap "*" |
||||
``` |
||||
|
||||
or add |
||||
|
||||
``` |
||||
"yiisoft/yii2-bootstrap": "*" |
||||
``` |
||||
|
||||
to the require section of your `composer.json` file. |
||||
|
@ -0,0 +1,28 @@
|
||||
{ |
||||
"name": "yiisoft/yii2-bootstrap", |
||||
"description": "The Twitter Bootstrap extension for the Yii framework", |
||||
"keywords": ["yii", "bootstrap"], |
||||
"type": "yii2-extension", |
||||
"license": "BSD-3-Clause", |
||||
"support": { |
||||
"issues": "https://github.com/yiisoft/yii2/issues?state=open", |
||||
"forum": "http://www.yiiframework.com/forum/", |
||||
"wiki": "http://www.yiiframework.com/wiki/", |
||||
"irc": "irc://irc.freenode.net/yii", |
||||
"source": "https://github.com/yiisoft/yii2" |
||||
}, |
||||
"authors": [ |
||||
{ |
||||
"name": "Qiang Xue", |
||||
"email": "qiang.xue@gmail.com" |
||||
} |
||||
], |
||||
"require": { |
||||
"yiisoft/yii2": "*", |
||||
"twbs/bootstrap": "3.0.*" |
||||
}, |
||||
"autoload": { |
||||
"psr-0": { "yii\\bootstrap\\": "" } |
||||
}, |
||||
"target-dir": "yii/bootstrap" |
||||
} |
@ -1,44 +1,63 @@
|
||||
Yii 2.0 Public Preview - Composer Installer |
||||
=========================================== |
||||
Yii 2 Composer Installer |
||||
======================== |
||||
|
||||
Thank you for choosing Yii - a high-performance component-based PHP framework. |
||||
This is the composer installer for Yii 2 extensions. It implements a new composer package type named `yii2-extension`, |
||||
which should be used by all Yii 2 extensions if they are distributed as composer packages. |
||||
|
||||
If you are looking for a production-ready PHP framework, please use |
||||
[Yii v1.1](https://github.com/yiisoft/yii). |
||||
|
||||
Yii 2.0 is still under heavy development. We may make significant changes |
||||
without prior notices. **Yii 2.0 is not ready for production use yet.** |
||||
Usage |
||||
----- |
||||
|
||||
[![Build Status](https://secure.travis-ci.org/yiisoft/yii2.png)](http://travis-ci.org/yiisoft/yii2) |
||||
|
||||
This is the yii2 composer installer. |
||||
|
||||
|
||||
Installation |
||||
------------ |
||||
|
||||
This extension offers you enhanced Composer handling for your yii2-project. It will therefore require you to use Composer. |
||||
To use Yii 2 composer installer, simply set `type` to be `yii2-extension` in your `composer.json`, |
||||
like the following: |
||||
|
||||
```json |
||||
{ |
||||
"type": "yii2-extension", |
||||
"require": { |
||||
"yiisoft/yii2": "*" |
||||
}, |
||||
... |
||||
} |
||||
``` |
||||
php composer.phar require yiisoft/yii2-composer "*" |
||||
``` |
||||
|
||||
*Note: You might have to run `php composer.phar selfupdate` before using this extension.* |
||||
|
||||
|
||||
Usage & Documentation |
||||
--------------------- |
||||
|
||||
This extension allows you to hook to certain composer events and automate preparing your Yii2 application for further usage. |
||||
|
||||
After the package is installed, the `composer.json` file has to be modified to enable this extension. |
||||
|
||||
To see it in action take a look at the example apps in the repository: |
||||
|
||||
- [Basic](https://github.com/suralc/yii2/blob/master/apps/basic/composer.json#L27) |
||||
- [Advanced](https://github.com/suralc/yii2/blob/extensions-readme/apps/advanced/composer.json) |
||||
|
||||
However it might be useful to read through the official composer [documentation](http://getcomposer.org/doc/articles/scripts.md) |
||||
to understand what this extension can do for you and what it can't. |
||||
You may specify a bootstrap class in the `extra` section. The `init()` method of the class will be executed each time |
||||
the Yii 2 application is responding to a request. For example, |
||||
|
||||
```json |
||||
{ |
||||
"type": "yii2-extension", |
||||
..., |
||||
"extra": { |
||||
"bootstrap": "yii\\jui\\Extension" |
||||
} |
||||
} |
||||
``` |
||||
|
||||
You can also use this as a template to create your own composer additions to ease development and deployment of your app. |
||||
The `Installer` class also implements a static method `setPermission()` that can be called after |
||||
a Yii 2 projected is installed, through the `post-create-project-cmd` composer script. |
||||
The method will set specified directories or files to be writable or executable, depending on |
||||
the corresponding parameters set in the `extra` section of the `composer.json` file. |
||||
For example, |
||||
|
||||
```json |
||||
{ |
||||
"name": "yiisoft/yii2-app-basic", |
||||
"type": "project", |
||||
... |
||||
"scripts": { |
||||
"post-create-project-cmd": [ |
||||
"yii\\composer\\Installer::setPermission" |
||||
] |
||||
}, |
||||
"extra": { |
||||
"writable": [ |
||||
"runtime", |
||||
"web/assets" |
||||
], |
||||
"executable": [ |
||||
"yii" |
||||
] |
||||
} |
||||
} |
||||
``` |
||||
|
@ -0,0 +1,46 @@
|
||||
Debug Extension for Yii 2 |
||||
========================= |
||||
|
||||
This extension provides a debugger for Yii 2 applications. When this extension is used, |
||||
a debugger toolbar will appear at the bottom of every page. The extension also provides |
||||
a set of standalone pages to display more detailed debug information. |
||||
|
||||
|
||||
Installation |
||||
------------ |
||||
|
||||
The preferred way to install this extension is through [composer](http://getcomposer.org/download/). |
||||
|
||||
Either run |
||||
|
||||
``` |
||||
php composer.phar require yiisoft/yii2-debug "*" |
||||
``` |
||||
|
||||
or add |
||||
|
||||
``` |
||||
"yiisoft/yii2-debug": "*" |
||||
``` |
||||
|
||||
to the require section of your `composer.json` file. |
||||
|
||||
|
||||
Usage |
||||
----- |
||||
|
||||
Once the extension is installed, simply modify your application configuration as follows: |
||||
|
||||
```php |
||||
return [ |
||||
'preload' => ['debug'], |
||||
'modules' => [ |
||||
'debug' => 'yii\debug\Module', |
||||
... |
||||
], |
||||
... |
||||
]; |
||||
``` |
||||
|
||||
You will see a debugger toolbar showing at the bottom of every page of your application. |
||||
You can click on the toolbar to see more detailed debug information. |
@ -0,0 +1,28 @@
|
||||
{ |
||||
"name": "yiisoft/yii2-debug", |
||||
"description": "The debugger extension for the Yii framework", |
||||
"keywords": ["yii", "debug", "debugger"], |
||||
"type": "yii2-extension", |
||||
"license": "BSD-3-Clause", |
||||
"support": { |
||||
"issues": "https://github.com/yiisoft/yii2/issues?state=open", |
||||
"forum": "http://www.yiiframework.com/forum/", |
||||
"wiki": "http://www.yiiframework.com/wiki/", |
||||
"irc": "irc://irc.freenode.net/yii", |
||||
"source": "https://github.com/yiisoft/yii2" |
||||
}, |
||||
"authors": [ |
||||
{ |
||||
"name": "Qiang Xue", |
||||
"email": "qiang.xue@gmail.com" |
||||
} |
||||
], |
||||
"require": { |
||||
"yiisoft/yii2": "*", |
||||
"yiisoft/yii2-bootstrap": "*" |
||||
}, |
||||
"autoload": { |
||||
"psr-0": { "yii\\debug\\": "" } |
||||
}, |
||||
"target-dir": "yii/debug" |
||||
} |
@ -1,6 +1,6 @@
|
||||
<?php |
||||
/** |
||||
* @var \yii\base\View $this |
||||
* @var \yii\web\View $this |
||||
* @var \yii\debug\Panel[] $panels |
||||
* @var string $tag |
||||
*/ |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue