我一直在构建这个应用程序有两个部分。这个网站由CMS提供支持,并且有CMS(摔跤管理员)随之而来。使用我创建的两个控制器称为前端和后端,我相应地使用它们。前端控制器负责需要在网站上的所有控制器上运行的代码。后端控制器负责需要在CMS上的所有控制器上运行的代码。
我正在寻找在用户再次成功登录后使用用户数据的理想方式,并将其定向到由后端控制器扩展的仪表板。我听说过不同的解决方案,例如一个钩子。那只是我听过的一个。
答案 0 :(得分:2)
Inheritance是你的朋友
您只需创建一个扩展 CI_Controller
的主控制器(前端控制器)您可以考虑使用SPA应用程序,如果有的话,有许多很棒的框架可以帮助您实现这一目标,非常受欢迎的应用程序是angular.js
some more useful reading on the subject...
class MY_Controller extends CI_Controller
{
protected $currentUser;
protected $template;
public function __construct()
{
//You talked about hooks
//this constructor is the same as saying
//post_controller_constructor
$this->template = 'master/default';
}
//Ok so what functions need to be run
//throughout the application
//run them once here, keeping our app DRY
protected function isAjax()
{
return ($this->input->is_ajax_request())
? true
: show_error('Invalid Request!');
}
protected function isLoggedIN()
{
//load your auth library maybe here
$this->load->library('auth');
//check you have a "current logged In user"
//expect object or null/false
$this->currentUser = $this->auth->getLoggedInUser();
return ($this->currentUser) ?: false;
}
}
class Frontend_Controller extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function testFirst()
{
//maybe you just need to test for ajax request here
//inheritance from parent
$this->isAjax();
}
public function testSecond()
{
//maybe you DO need a user logged in here
//inheritance from parent
$this->isAjax();
//or yet again, abstract this to the parent level
if(!$this->currentUser || is_null($this->currentUser))
{
//handle errors on frontend
return $this->output->set_status_header(401); //un-authorized
}
}
}