我一直在使用 CodeIgniter 开发一个 Web 应用程序。
我还实现了 MVC 结构之后的所有功能,但我不确定到目前为止我是否感到困惑......
如果我在视图中使用模型中的函数,是否违反了 MVC 模式?这是否被视为与模型交谈的视图?
让我展示一些代码:
我的控制器中的一个:
public function loader($page){
#load pages if these settings will be loaded for sure
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$this->load->helper('url');
$this->load->library('session');
$this->load->model('app_model');
$this->load->model('app_user_model');
$this->load->view('app/header');
$this->load->view($page);
$this->load->view('app/footer');
}
我的模型中的一个:
public function submit_login($username, $pass){
//try to login, if the login credentials are correct, set the user session to username.
$this->db->select("name, PersonName");
$this->db->where("name", $username);
$this->db->where("pass", $pass);
$result = $this->db->get("members");
if ($result->num_rows() > 0){
foreach($result->result() as $user){
$this->session->set_userdata('user_name', $user->PersonName);
$this->session->set_userdata('user', $user->name);
}
return 1;
}return 0;
}
部分观点:
$is_login = $this->app_user_model->is_loggedin();
if($is_login){
//if user is logged in, prompt to log out
echo "Welcome back ".$this->session->userdata['user_name'].".";
echo anchor(base_url('profile/logout'), "logout" );
echo "<br/>";
}else{
echo validation_errors();
//if user is not logged in, ask to log in first
echo form_open('profile/login');
$user_name = array(
'maxlength' => '25',
'size' => '55',
'style' => 'width:30%; margin-left:5%',
'name' => 'username',
);
//get the date
echo "<div class='user_name_input'>";
echo "Username(email)";
echo form_input($user_name, '', 'class="user_name"');
echo "</div>";
在这种情况下,我从 user_model 调用 is_loggedin() ..
我是否正确遵循 MVC 模式?
感谢您的帮助。