Skip to content

Commit

Permalink
Feature: Add more methods to base controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Adeyemi Olaoye committed Jun 12, 2017
1 parent 760b4d8 commit 9d80f6b
Showing 1 changed file with 69 additions and 1 deletion.
70 changes: 69 additions & 1 deletion src/Controller/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Lukasoppermann\Httpstatus\Httpstatus;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\web\Controller;
use yii\web\Response;
Expand Down Expand Up @@ -242,7 +243,6 @@ private function mergeFlashMessages($messageArray)
}
}


return $flashMessage . implode('<br/>', $flashMessageArr);
}

Expand Down Expand Up @@ -283,4 +283,72 @@ public function isPostCheck($redirectUrl = null)
}
$this->sendTerminalResponse($redirectUrl);
}


/**
* @author Adeyemi Olaoye <yemi@cottacush.com>
* @param \yii\base\Action $action
* @return bool
*/
public function beforeAction($action)
{
$excludedPaths = ArrayHelper::getValue(\Yii::$app->params, 'excludedPaths', []);

$currentRoute = $this->getRoute();

/**
* Check if route is in the excluded path
*/
if ($this->getUser()->isGuest) {
if (!in_array($currentRoute, $excludedPaths)) {
$this->getUser()->loginRequired();
return false;
}
}

return true;
}

/**
* @author Adeyemi Olaoye <yemi@cottacush.com>
* @param $message
* @return \yii\web\Response
*/
public function returnError($message)
{
$this->flashError($message);
return $this->redirect($this->getRequest()->getReferrer());
}

/**
* @author Adeyemi Olaoye <yemi@cottacush.com>
* @param $message
* @return \yii\web\Response
*/
public function returnSuccess($message)
{
$this->flashSuccess($message);
return $this->redirect($this->getRequest()->getReferrer());
}

/**
* @author Adeyemi Olaoye <yemi@cottacush.com>
* @param $widget
* @param $config
* @return string
*/
public function renderWidgetAsAjax($widget, $config)
{
ob_start();
ob_implicit_flush(false);

$this->view->beginPage();
$this->view->head();
$this->view->beginBody();
echo $widget::widget($config);
$this->view->endBody();
$this->view->endPage(true);

return ob_get_clean();
}
}

0 comments on commit 9d80f6b

Please sign in to comment.