如何在 MVC 中将模型加载到控制器中
我正在致力于构建一个轻量级 MVC,主要用于学习过程,但我希望它足够好,最终可以使用。
下面是基本控制器外观的基本示例/演示,假设 URI 已被处理并路由到该控制器和这 2 个方法。
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);
}
}
I am working on building a lightweight MVC, mainly for the learning process but I would like it to be good enough to use eventually.
Below is a basic example/demo of how a basic controller might would look, let's assume the URI has been processed and routed to this controller and these 2 methods.
1) I need to get data from database/cache/etc... inside my Model classes, I just need help on how I should load my models into my example controller below, you can see that I have added this below $profileData = $this->model->getProfile($userId)
that is just made up and does not exist's, how could I get something like that to work though? Or should I load the model into the class a different way?
2) A lot of pages will require a user to be logged into the site. SHould I process that part below in the controller to check if a user is logged in, example, before building the profile page, check if user is logged in, if not then build a login page instead and add these checks inside of each controller method/page?
/**
* 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);
}
}
core
abstract class Core_Controller {
protected $view;
protected $model;
function __construct(DependencyContainer $dependencyContainer){
$this->view = new Core_View();
//$this->view = $dependencyContainer->get(view);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能有很多方法可以完成您正在尝试的事情。
“最简单的”可能只是重写构造函数并直接实例化模型。
在 User_Controller 中:
我猜您正在寻找更自动化的东西。如果你希望模型与控制器同名减去“_Controller”,只需在构造函数中使用 get_class($this) 并使用 PHP 的字符串函数解析出你想要的内容。一旦你将其放入变量中,你就可以使用该变量来实例化模型:
在 Core_Controller 中:
我实际上还没有使用过任何只能有一个模型与每个控制器关联的框架(除了 CakePHP?我不记得了) )。使用 Symfony,模型和控制器完全解耦,因此您可以将任何模型与任何控制器一起使用。您只需根据需要实例化模型即可。 Symfony 使用 Doctrine ORM,例如,在控制器操作中,如果您需要一个模型,您将执行以下操作:
为了促进解耦设计,考虑类似的设计可能是值得的,我保证 在某个时刻,您会希望某个控制器中有多个模型。
2.) 至于身份验证。似乎相当常见的事情是有某种设置(无论是在配置文件中还是在成员变量中)来说明当前操作是否需要对用户进行身份验证。每次操作运行时都会处理此内容(Yii 将此类事物称为过滤器)。如果用户需要登录,它会存储他们尝试访问的页面,然后将他们重定向到登录页面(您应该只需要创建一个)。一旦他们正确地进行了身份验证,它就会将他们重定向回原来的目的地。
There are probably tons of ways to accomplish what you are trying.
The "easiest" is probably to just override the constructor and instantiate the model directly.
in User_Controller:
I'm guessing that you are looking for something a little more automated though. If you want the Model to have the same name as the controller minus "_Controller", just use get_class($this) in the constructor and use PHP's string functions to parse out what you want. Once you have that in a variable, you can use that variable to instantiate the model:
in Core_Controller:
I haven't actually worked with any framework that can only have one model associated with each controller (except may CakePHP? I can't remember). With Symfony, the models and controllers are completely decoupled so you can use any model with any controller. You just instantiate the model as need. Symfony use the Doctrine ORM so for example, in a controller action, if you needed a model you would do something like this:
It might be worthwhile to consider a design more like that in order to promote a decoupled design and I promise that you will want more than one model in some controller at some point.
2.) As far as authentication. Something that seems to be fairly common is to have some sort of setting (whether in a config file or a member variable) that says whether or not the current action needs the user to be authenticated. This is processed each time the action runs (Yii calls these kinds of things filters). If the user needs to be logged in, it stores the page that they are trying to access, and then redirects them to a log in page (you should only ever have to create one). Once they properly authenticate, it will redirect them back to where they were originally heading.