Qiang Xue
12 years ago
9 changed files with 134 additions and 4 deletions
@ -0,0 +1,9 @@
|
||||
<?php |
||||
|
||||
defined('YII_DEBUG') or define('YII_DEBUG', true); |
||||
|
||||
require(__DIR__ . '/../framework/yii.php'); |
||||
|
||||
$config = require(__DIR__ . '/protected/config/main.php'); |
||||
$application = new yii\web\Application($config); |
||||
$application->run(); |
@ -0,0 +1,15 @@
|
||||
<?php |
||||
|
||||
return array( |
||||
'id' => 'hello', |
||||
'basePath' => dirname(__DIR__), |
||||
'components' => array( |
||||
'cache' => array( |
||||
'class' => 'yii\caching\FileCache', |
||||
), |
||||
'user' => array( |
||||
'class' => 'yii\web\User', |
||||
'identityClass' => 'app\models\User', |
||||
) |
||||
), |
||||
); |
@ -0,0 +1,22 @@
|
||||
<?php |
||||
|
||||
class SiteController extends \yii\web\Controller |
||||
{ |
||||
public function actionIndex() |
||||
{ |
||||
echo $this->render('index'); |
||||
} |
||||
|
||||
public function actionLogin() |
||||
{ |
||||
$user = app\models\User::findIdentity(100); |
||||
Yii::$app->getUser()->login($user); |
||||
Yii::$app->getResponse()->redirect(array('site/index')); |
||||
} |
||||
|
||||
public function actionLogout() |
||||
{ |
||||
Yii::$app->getUser()->logout(); |
||||
Yii::$app->getResponse()->redirect(array('site/index')); |
||||
} |
||||
} |
@ -0,0 +1,43 @@
|
||||
<?php |
||||
|
||||
namespace app\models; |
||||
|
||||
class User extends \yii\base\Object implements \yii\web\Identity |
||||
{ |
||||
public $id; |
||||
public $name; |
||||
public $authKey; |
||||
|
||||
private static $users = array( |
||||
'100' => array( |
||||
'id' => '100', |
||||
'authKey' => 'test100key', |
||||
'name' => 'admin', |
||||
), |
||||
'101' => array( |
||||
'id' => '101', |
||||
'authKey' => 'test101key', |
||||
'name' => 'demo', |
||||
), |
||||
); |
||||
|
||||
public static function findIdentity($id) |
||||
{ |
||||
return isset(self::$users[$id]) ? new self(self::$users[$id]) : null; |
||||
} |
||||
|
||||
public function getId() |
||||
{ |
||||
return $this->id; |
||||
} |
||||
|
||||
public function getAuthKey() |
||||
{ |
||||
return $this->authKey; |
||||
} |
||||
|
||||
public function validateAuthKey($authKey) |
||||
{ |
||||
return $this->authKey === $authKey; |
||||
} |
||||
} |
@ -0,0 +1,22 @@
|
||||
<?php |
||||
/** |
||||
* @var $this \yii\base\View |
||||
* @var $content string |
||||
*/ |
||||
use yii\helpers\Html; |
||||
?> |
||||
<!DOCTYPE html> |
||||
<html> |
||||
<?php $this->beginPage(); ?> |
||||
<head> |
||||
<title><?php echo Html::encode($this->title); ?></title>
|
||||
<?php $this->head(); ?> |
||||
</head> |
||||
<body> |
||||
<h1>Welcome</h1> |
||||
<?php $this->beginBody(); ?> |
||||
<?php echo $content; ?> |
||||
<?php $this->endBody(); ?> |
||||
</body> |
||||
<?php $this->endPage(); ?> |
||||
</html> |
@ -0,0 +1,17 @@
|
||||
<?php |
||||
/** @var $this \yii\base\View */ |
||||
|
||||
use yii\helpers\Html; |
||||
|
||||
$this->title = 'Hello World'; |
||||
|
||||
$user = Yii::$app->getUser(); |
||||
if ($user->isGuest) { |
||||
echo Html::a('login', array('login')); |
||||
} else { |
||||
echo "You are logged in as " . $user->identity->name . "<br/>"; |
||||
echo Html::a('logout', array('logout')); |
||||
} |
||||
?> |
||||
|
||||
|
Loading…
Reference in new issue