MVC 控制器的示例

2021-12-21 00:00:00 php model-view-controller

我已经阅读了很多关于如何以及为什么在应用程序中使用 MVC 方法的文章.我已经看到并理解了模型的例子,我已经看到并理解了视图的例子......但我仍然对控制器有点模糊.我真的很想看到一个足够完整的控制器示例.(如果可能,使用 PHP,但任何语言都会有所帮助)

I have been reading a lot about how and why to use an MVC approach in an application. I have seen and understand examples of a Model, I have seen and understand examples of the View.... but I am STILL kind of fuzzy on the controller. I would really love to see a thorough enough example of a controller(s). (in PHP if possible, but any language will help)

谢谢.

PS:如果我能看到一个 index.php 页面的例子也很棒,它决定使用哪个控制器以及如何使用.

PS: It would also be great if I could see an example of an index.php page, which decides which controller to use and how.

我知道控制器的工作是什么,我只是不明白如何在 OOP 中实现这一点.

I know what the job of the controller is, I just don't really understand how to accomplish this in OOP.

推荐答案

请求示例

在你的 index.php 中加入类似的内容:

Put something like this in your index.php:

<?php

// Holds data like $baseUrl etc.
include 'config.php';

$requestUrl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$requestString = substr($requestUrl, strlen($baseUrl));

$urlParams = explode('/', $requestString);

// TODO: Consider security (see comments)
$controllerName = ucfirst(array_shift($urlParams)).'Controller';
$actionName = strtolower(array_shift($urlParams)).'Action';

// Here you should probably gather the rest as params

// Call the action
$controller = new $controllerName;
$controller->$actionName();

非常基础,但你明白了......(我也没有负责加载控制器类,但我想这可以通过自动加载来完成,或者你知道如何去做.)

Really basic, but you get the idea... (I also didn't take care of loading the controller class, but I guess that can be done either via autoloading or you know how to do it.)

简单的控制器示例(controllers/login.php):

Simple controller example (controllers/login.php):

<?php    

class LoginController
{
    function loginAction()
    {
        $username = $this->request->get('username');
        $password = $this->request->get('password');

        $this->loadModel('users');
        if ($this->users->validate($username, $password))
        {
            $userData = $this->users->fetch($username);
            AuthStorage::save($username, $userData);
            $this->redirect('secret_area');
        }
        else
        {
            $this->view->message = 'Invalid login';
            $this->view->render('error');
        }
    }

    function logoutAction()
    {
        if (AuthStorage::logged())
        {
            AuthStorage::remove();
            $this->redirect('index');
        }
        else
        {
            $this->view->message = 'You are not logged in.';
            $this->view->render('error');
        }
    }
}

如您所见,控制器负责应用程序的流程"——即所谓的应用程序逻辑.它不关心数据存储和呈现.而是收集所有必要的数据(取决于当前请求)并将其分配给视图...

As you see, the controller takes care of the "flow" of the application - the so-called application logic. It does not take care about data storage and presentation. It rather gathers all the necessary data (depending on the current request) and assigns it to the view...

请注意,这不适用于我知道的任何框架,但我相信您知道这些函数应该做什么.

Note that this would not work with any framework I know, but I'm sure you know what the functions are supposed to do.

相关文章