【发布时间】: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