【问题标题】:php MVC without routes没有路由的php MVC
【发布时间】:2018-01-10 08:48:43
【问题描述】:

我想知道是否可以在不使用路由的情况下在 php 中创建 mvc 项目。 例如:

我有控制器 strumenti.php

class Strumenti
{
    public function index()
    { 
        require 'application/models/strumentimodel.php';
        $strumenti_model=new StrumentiModel("r");
        $strumenti = $strumenti_model->getAllInstruments();
        require 'application/views/_templates/header.php';
        require 'application/views/strumenti/index.php';
        require 'application/views/_templates/footer.php';
    }

    public function deleteStrumento($nome)
    {   
        if (isset($nome)) {
            require 'application/models/strumentimodel.php';
            $strumenti_model=new StrumentiModel("r");  
            $strum=$strumenti_model->deleteStrumentoDaArray($nome);   
        }
        header('location: 'mysite/strumenti/index');
    }
}

和我的模型 strumentimodel.php

class StrumentiModel
{
    private $handle;

    function __construct($mode) {
        try {
            $this->handle = fopen(STRUMENTI, "$mode");
        } catch (PDOException $e) {
            exit('Errore di apertura file');
        }
    }

    public function getAllInstruments()
    {
        $csv = array();
        $lines = file(STRUMENTI);

        foreach ($lines as $key => $value)
        {
            $csv[$key] = str_getcsv($value,";");
        }
        return $csv;
    }
    public function deleteStrumentoDaArray($nome)
    {
        //array con tutti gli strumenti
        $strum=$this->getAllInstruments();

        for($i=0;$i<count($strum);$i++){
            if(in_array($nome,$strum[$i])){
                $this->indice=$i;
            }
        }
        unset($strum[$this->indice]);
        return $strum;
    }
}

这是一个视图 (index.php)

<div>
            <h3>Strumenti</h3>
                <table>
                    <tr>
                        <td>nome</td>
                        <td>modello</td>
                        <td>tipo</td>
                        <td>costo</td>
                        <td>Elimina</td>
                        <td>Modifica</td>
                    </tr>
                    <tbody>
                    <?php for ($riga=1;$riga<count($strumenti);$riga++):  ?>
                        <tr>
                            <?php for ($colonna=0; $colonna<count(current($strumenti)); $colonna++):  ?>
                                <td><?php echo $strumenti[$riga][$colonna];?></td>
                            <?php endfor; ?>
                            <td><a href="<?php echo mysite/strumenti/deleteStrumento/' . $strumenti[$riga][0]; ?>">x</a></td>
                            <td><a href="<?php echo mysite/strumenti/index ?>">Index</a></td>
                        </tr>
                    <?php endfor; ?>
                    </tbody>
                </table>



        </div>

如果我从控制器调用模型,即使没有路由也没有问题,但我可以从视图调用控制器而不用路由吗?

在我的示例中,我通过链接这样称呼它:

<a href="<?php echo mysite/strumenti/deleteStrumento/' . $strumenti[$riga][0]; ?>">x</a>

结构为:类/方法/参数

是否可以在没有路由的情况下调用类方法?

【问题讨论】:

  • 如果你创建 mysite/strumenti/xyz 意味着你是一个路由或目录路径,那么你想访问带有目录的控制器吗?
  • 在表单提交选项中调用控制器功能,之后您将更改控制器。

标签: php class model-view-controller routes


【解决方案1】:

“路由”与 MVC 没有什么特别的关系。通俗理解的“路由”是一些函数/类/代码,它将 URL 解析为可执行代码段,通常是控制器。好吧,您可以通过使用像/controllers/foo-controller.php 这样的URL 来“免费”使用PHP 的标准行为,其中将执行控制器的代码。您的控制器甚至不需要是一个类,它只需要是接收请求并可以决定调用哪些模型操作和/或发送哪些视图/响应的东西。

这就是 MVC 的全部内容:拥有一个核心 model,其中包含您的应用可以“做”的所有事情,拥有独立于为您的模型提供 UI 的 views,最后有一个 controller 作用于用户输入(这里:HTTP 请求)。这种分离很简单,您可以通过更换 UI(视图)和输入处理程序(控制器)轻松地使您的应用程序(模型)适应不同的场景。其中没有规定使用类、路线或其他任何东西。

【讨论】:

  • MVC 概念对我来说很清楚,但是我可以从视图中调用正确的控制器函数(例如使用链接 /controllers/foo-controller.php 吗?在这种情况下,我调用文件但我不能选择方法/功能
  • 在 HTTP 场景中,您根本不会“调用控制器的函数”。充其量您在视图中创建一个 URL,Web 服务器将该 URL 解析为特定文件。您的控制器文件可能包含类似require 'c.php'; (new C)-&gt;action(); 的内容。这基本上是一个穷人的前端控制器/路由器。或者文件只包含导入模型并在那里调用某些东西的代码,使文件本身成为“控制器函数”。
【解决方案2】:

当然你可以从视图中调用控制器动作,但是这样做的目的是什么。 如果您调用控制器操作,渲染视图再次调用此操作,则也可能出现永无止境的循环。 请说明您的目的。

【讨论】:

  • 如果我想从视图中执行一个动作,我点击一个链接,女巫将调用一个控制器(whitch 负责执行该动作)。我可以使用他的文件名来调用控制器 ex /controllers/foo-controller.php 但我怎样才能调用方法/函数?
  • 使用 GET 参数。只需在控制器类声明后添加 php 代码即可检查是否提供了必要的参数。但这将是一个怪异的解决方案 =) 或者......您可以在入口点处理所有请求,然后解析参数。但它实际上是路由)))
猜你喜欢
  • 2019-05-10
  • 1970-01-01
  • 2021-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-07
相关资源
最近更新 更多