yii2 advanced app - Connecting Frontend to backend when login -
i have code in yii2 advanced template, want make link when log in frontend connecting in backend , when logout backend frontend,how can make that? , comon/config/main.php file
<?php return [ 'vendorpath' => dirname(dirname(__dir__)) . '/vendor', 'components' => [ 'cache' => [ 'class' => 'yii\caching\filecache', ], 'urlmanager' => [ //'class' => 'yii\web\urlmanager', 'enableprettyurl' => false, 'showscriptname' => true, 'baseurl' => '/backend/web', ], 'urlmanagerfrontend' => [ 'class' => 'yii\web\urlmanager', 'baseurl' => '/frontend/web', 'enableprettyurl' => false, 'showscriptname' => true, ] ], ];
and sitecontroller in frontend
<?php namespace frontend\controllers; use yii; use common\models\loginform; use frontend\models\passwordresetrequestform; use frontend\models\resetpasswordform; use frontend\models\signupform; use frontend\models\contactform; use yii\base\invalidparamexception; use yii\web\badrequesthttpexception; use yii\web\controller; use yii\filters\verbfilter; use yii\filters\accesscontrol; use yii\helpers\url; use backend; /** * site controller */ class sitecontroller extends controller { /** * @inheritdoc */ public function behaviors() { return [ 'access' => [ 'class' => accesscontrol::classname(), 'only' => ['logout', 'signup'], 'rules' => [ [ 'actions' => ['signup'], 'allow' => true, 'roles' => ['?'], ], [ 'actions' => ['logout'], 'allow' => true, 'roles' => ['@'], ], ], ], 'verbs' => [ 'class' => verbfilter::classname(), 'actions' => [ 'logout' => ['post'], ], ], ]; } /** * @inheritdoc */ public function actions() { return [ 'error' => [ 'class' => 'yii\web\erroraction', ], 'captcha' => [ 'class' => 'yii\captcha\captchaaction', 'fixedverifycode' => yii_env_test ? 'testme' : null, ], ]; } /** * displays homepage. * * @return mixed */ public function actionindex() { return $this->render('index'); } /** * logs in user. * * @return mixed */ public function actionlogin() { if (!\yii::$app->user->isguest) { return $this->gohome(); } $model = new loginform(); if ($model->load(yii::$app->request->post()) && $model->login()) { return $this->goback(); } else { return $this->render('login', [ 'model' => $model, ]); } } /** * logs out current user. * * @return mixed */ public function actionlogout() { yii::$app->user->logout(); return $this->gohome(); } /** * displays contact page. * * @return mixed */ public function actioncontact() { $model = new contactform(); if ($model->load(yii::$app->request->post()) && $model->validate()) { if ($model->sendemail(yii::$app->params['adminemail'])) { yii::$app->session->setflash('success', 'thank contacting us. respond possible.'); } else { yii::$app->session->setflash('error', 'there error sending email.'); } return $this->refresh(); } else { return $this->render('contact', [ 'model' => $model, ]); } } /** * displays page. * * @return mixed */ public function actionabout() { return $this->render('about'); } /** * signs user up. * * @return mixed */ public function actionsignup() { $model = new signupform(); if ($model->load(yii::$app->request->post())) { if ($user = $model->signup()) { if (yii::$app->getuser()->login($user)) { return $this->gohome(); } } } return $this->render('signup', [ 'model' => $model, ]); } /** * requests password reset. * * @return mixed */ public function actionrequestpasswordreset() { $model = new passwordresetrequestform(); if ($model->load(yii::$app->request->post()) && $model->validate()) { if ($model->sendemail()) { yii::$app->session->setflash('success', 'check email further instructions.'); return $this->gohome(); } else { yii::$app->session->setflash('error', 'sorry, unable reset password email provided.'); } } return $this->render('requestpasswordresettoken', [ 'model' => $model, ]); } /** * resets password. * * @param string $token * @return mixed * @throws badrequesthttpexception */ public function actionresetpassword($token) { try { $model = new resetpasswordform($token); } catch (invalidparamexception $e) { throw new badrequesthttpexception($e->getmessage()); } if ($model->load(yii::$app->request->post()) && $model->validate() && $model->resetpassword()) { yii::$app->session->setflash('success', 'new password saved.'); return $this->gohome(); } return $this->render('resetpassword', [ 'model' => $model, ]); } /*public function goback($defaulturl = null) { if(is_null($defaulturl)) { $defaulturl = yii::$app->request->baseurl; // ganti dibagian ini ndu } return yii::$app->getresponse()->redirect(yii::$app->getuser()->getreturnurl($defaulturl)); }*/ } and same sitecontroller in backend
Comments
Post a Comment