【发布时间】:2011-11-11 19:02:47
【问题描述】:
我正在构建一个轻量级 MVC,主要用于学习过程,但我希望它足够好,最终可以使用。
下面是基本控制器外观的基本示例/演示,假设 URI 已被处理并路由到此控制器和这两种方法。
1) 我需要在我的模型类中从数据库/缓存/等获取数据,我只需要关于如何将我的模型加载到下面的示例控制器中的帮助,你可以看到我在下面添加了这个$profileData = $this->model->getProfile($userId) 只是编造出来的,不存在的,我怎么能得到这样的东西呢?还是应该以不同的方式将模型加载到类中?
2) 很多页面都需要用户登录网站。我应该在控制器中处理下面的部分以检查用户是否已登录,例如,在构建个人资料页面之前,检查用户是否已登录,如果没有,则改为构建登录页面并将这些检查添加到每个控制器方法中/页?
/**
* Example Controller
*/
class User_Controller extends Core_Controller {
// domain.com/user/id-53463463
function profile($userId)
{
//GET data from a Model
$profileData = $this->model->getProfile($userId);
$this->view->load('userProfile', $profileData);
}
// domain.com/user/friends/
function friends()
{
//GET data from a Model
$friendsData = $this->model->getFriendlist();
$this->view->load('userFriends', $friendsData);
}
}
核心
abstract class Core_Controller {
protected $view;
protected $model;
function __construct(DependencyContainer $dependencyContainer){
$this->view = new Core_View();
//$this->view = $dependencyContainer->get(view);
}
}
【问题讨论】:
标签: php oop model-view-controller model controller