首先我们要知道一个:MVC设计模式的规则.
一般都是使用一个文件来做分发控制(如果是MAGNETO还有路由控制,当然OPENCART也可以使用VQMOD实现).
index.php就是OPENCART的分发控制:
看看这一句:$controller->addPreAction(new Action(‘common/login/check’));
实例化后的$controller调用方法:
注:OPENCART的一切数据基本使用$this->session对象,而用户基本使用$this->user对象.在这些对象里封装了获取购物车数据,用户数据等方法.
控制流程:
执行check()方法,如果不存在user_id($this->user->isLogged()的返回值),跳转到common/login的index方法(index为默认方法,具体可以查看controller的封装)
index()中,默认再一次判断是否存在有user对象的user_id值
如果存在跳转仪表盘.
接着,判断是否存在有POST的参数和是否合法:
if (($this->request->server[‘REQUEST_METHOD’] == ‘POST’) && $this->validate())
validate的方法原型::
/**
* 判断用户的登陆参数
* 错误的话返回flase;
*
* @return boolean
*/
protected function validate() {
if (!isset($this->request->post[‘username’])
|| !isset($this->request->post[‘password’])
|| !$this->user->shangjialogin($this->request->post[‘username’], $this->request->post[‘password’]))
{
$this->error[‘warning’] = $this->language->get(‘error_login’);
}
return !$this->error;
}
上文的红色字体中,就是作者新增加的模型方法,如果用户密码正确,返回true,否则false;
这样,就需要在模型里对$this->user类的私有成员user_id进行赋值.
成功达到管理员登陆的效果!