我創建了自己的MVC的免費PHP當然,我進行對於想要在PHP中變得更好的少數人來說。
到目前爲止,最好的方法是使用Command + Factory模式。
E.g.
interface ControllerCommand
{
public function execute($action);
}
在每個控制器:
class UserController implements ControllerCommand
{
public function execute($action)
{
if ($action == 'login')
{
$data['view_file'] = 'views/home.tpl.php';
}
else if ($action == 'edit_profile')
{
$data['view_file'] = 'views/profile.tpl.php';
$data['registration_status'] = $this->editProfile();
}
return $data;
}
}
從你的主前端控制器:
$data = ControllerCommandFactory::execute($action);
if (!is_null($data)) { extract($data); }
/* We know the view_file is safe, since we explicitly set it above. */
require $view_file;
的一點是,每Controllercommand類有一個執行功能和返回其視圖和任何數據爲此觀點。
對於完整的MVC,您可以通過在theodore [at] phpexperts.pro上給我發郵件來訪問開源應用程序。
所以你建議只返回控制器中的變量。然後在視圖中說$ vars = new my_controller();然後使用適當的功能。這確實是一個很好的簡單解決方案。 – Mike 2010-08-24 22:25:52