du5739 2014-06-06 06:57
浏览 57

我的MVC PHP应用程序在哪里放置这个构建函数?

I'm trying to convert my PHP app into more of an MVC app. I don't have much experience with MVC and I don't fully understand some of/all of the concepts or how to do it with PHP, so I need some help understanding where a particular function goes.

This function returns some HTML depending on if the user is logged in.

public function buildLoggedInMessage() {
    if ($this->User->isLoggedIn()) {
        return ' You are logged in as <strong>'.$this->User->getUsername().'</strong> (<a href="/logout.php">logout</a>)';
    } else {
        return ' <a href="/login.php">Login</a>';
    }
}

My initial thought was to place this function in my "controller" because it asks the User model if they are logged in (which checks the database record), however it "builds" some HTML, so maybe it should be in the view. Should I move it?

I will eventually move the HTML from the function into a template, so ignore the inline HTML.

Would the function be more suitable in the view if it was like this:

public function buildLoggedInMessage() {
    if ($this->Controller->isLoggedIn()) {
        return ' You are logged in as <strong>'.$this->User->getUsername().'</strong> (<a href="/logout.php">logout</a>)';
    } else {
        return ' <a href="/login.php">Login</a>';
    }
}

and the controller asks the model if the user is logged in?

Thanks.

  • 写回答

3条回答 默认 最新

  • douxi1738 2014-06-06 07:33
    关注

    put this function in the controller from where you are calling a login function after if a user authenticated then it will set the session or flash data i.e $this->session->set_flashdata('success', 'you are loged in as $username'); else redirect('login');

    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序