1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yii2mod\user\actions; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\web\Response; |
7
|
|
|
use yii\widgets\ActiveForm; |
8
|
|
|
use yii2mod\user\traits\EventTrait; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class SignupAction |
12
|
|
|
* |
13
|
|
|
* @package yii2mod\user\actions |
14
|
|
|
*/ |
15
|
|
|
class SignupAction extends Action |
16
|
|
|
{ |
17
|
|
|
use EventTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Event is triggered after creating SignupForm class. |
21
|
|
|
* Triggered with \yii2mod\user\events\FormEvent. |
22
|
|
|
*/ |
23
|
|
|
const EVENT_BEFORE_SIGNUP = 'beforeSignup'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Event is triggered after successful signup. |
27
|
|
|
* Triggered with \yii2mod\user\events\FormEvent. |
28
|
|
|
*/ |
29
|
|
|
const EVENT_AFTER_SIGNUP = 'afterSignup'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string name of the view, which should be rendered |
33
|
|
|
*/ |
34
|
|
|
public $view = '@vendor/yii2mod/yii2-user/views/signup'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string signup form class |
38
|
|
|
*/ |
39
|
|
|
public $modelClass = 'yii2mod\user\models\SignupForm'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Signup a user. |
43
|
|
|
* |
44
|
|
|
* @return string|\yii\web\Response |
45
|
|
|
*/ |
46
|
|
|
public function run() |
47
|
|
|
{ |
48
|
|
|
$model = Yii::createObject($this->modelClass); |
49
|
|
|
$event = $this->getFormEvent($model); |
50
|
|
|
|
51
|
|
|
$this->trigger(self::EVENT_BEFORE_SIGNUP, $event); |
52
|
|
|
|
53
|
|
|
$load = $model->load(Yii::$app->request->post()); |
54
|
|
|
|
55
|
|
View Code Duplication |
if (Yii::$app->request->isAjax) { |
|
|
|
|
56
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON; |
57
|
|
|
|
58
|
|
|
return ActiveForm::validate($model); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($load && ($user = $model->signup()) !== null) { |
62
|
|
|
$this->trigger(self::EVENT_AFTER_SIGNUP, $event); |
63
|
|
|
if (Yii::$app->getUser()->login($user)) { |
64
|
|
|
return $this->redirectTo(Yii::$app->getUser()->getReturnUrl()); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $this->controller->render($this->view, [ |
69
|
|
|
'model' => $model, |
70
|
|
|
]); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.