Php 初始化视图、模板、控制器和模型是可选的
当我问这个问题时,我太困了,所以很抱歉,无论如何,为了把事情弄清楚,我准备了两个小时的问题 我试图组织我的代码,并决定将其组织为mvc风格(类似mvc),我不知道我是否能遵循所有原则,但我想至少接近这一点 我的应用程序有一个前端控制器(如果我的定义是正确的,则不知道),因此我的应用程序的所有http请求都将通过一个点,在我的例子中是应用程序根目录中的Php 初始化视图、模板、控制器和模型是可选的,php,model-view-controller,view,controller,initialization,Php,Model View Controller,View,Controller,Initialization,当我问这个问题时,我太困了,所以很抱歉,无论如何,为了把事情弄清楚,我准备了两个小时的问题 我试图组织我的代码,并决定将其组织为mvc风格(类似mvc),我不知道我是否能遵循所有原则,但我想至少接近这一点 我的应用程序有一个前端控制器(如果我的定义是正确的,则不知道),因此我的应用程序的所有http请求都将通过一个点,在我的例子中是应用程序根目录中的index.php 说了这么多,您可以想象我使用.htaccess将所有请求定向到index.php 我分解了url,并用它创建了一个数组,$url
index.php
说了这么多,您可以想象我使用.htaccess
将所有请求定向到index.php
我分解了url
,并用它创建了一个数组,$url[]
,就像这样。所以每当我像这样访问我的应用程序时http://localhost/app/pagename
它将访问一个控制器(pagename\u控制器
)
我是这样做的:
$file = $controller_path . $page . '_controller.php';
if (file_exists($file)) {
require $file;
$class_name = ucfirst($page) . '_controller';
$target = new $class_name();
}
$controller = new Wrap($target);
$controller->index();
我还将其包装在一个容器中,即“装饰器模式”,以备将来使用,或者验证。
像这样:
$file = $controller_path . $page . '_controller.php';
if (file_exists($file)) {
require $file;
$class_name = ucfirst($page) . '_controller';
$target = new $class_name();
}
$controller = new Wrap($target);
$controller->index();
我不知道使用$controller
变量名是否合适,所以如果完全错误,请原谅我
我觉得我可以这样设置我的应用程序:
$file = $controller_path . $page . '_controller.php';
if (file_exists($file)) {
require $file;
$class_name = ucfirst($page) . '_controller';
$target = new $class_name();
}
$controller = new Wrap($target);
$controller->index();
index.php
)
现在的问题是应用程序其他部分的初始化
到目前为止,我有3个文件,我想设置,但我对如何设置感到困惑
索引控制器
,索引视图
,模板
class Index_controller {
private $model;
private $view;
public function __construct(){
// optional model -> $this->model = 'index'
$this->view = 'index' //
}
public function index(){
$this->load->view($this->view)
}
}
class Index_view {
private $model;
private $template;
public function __construct(Model $model = null){
$this->template = new Template('default');
}
public function view() {
$this->template->assign('css', 'default_css'); // don't know if this is efficient
// or $this->template->assign('header', 'default_header');
// or $this->template->assign('sidebar', 'default_sidebar');
// or $this->template->assign('footer', 'default_footer');
// or any other things I want to use in the template
}
}
class Template {
public $data = array();
private $tmpl;
public function __construct($template) {
$this->tmpl = $template . '_tmpl.php';
}
public function assign($name, $value){
$this->data[$name] = $value;
}
// public function output
// function that will explode the data array and render it out as a webpage
// I'll create templates and
}
有了这些,我现在想知道如何将这些东西联系起来。目前,我有一个可以包含类的系统
文件夹,我为该文件夹设置了一个自动加载器
我正在考虑创建一个控制器
类和视图
类,作为图中所示的ActionFactory和ViewFactory,尽管我知道这不是他们的职责
我在想:
class Controller {
protected $load;
public function __construct() {
$this->load = new View();
}
}
class View {
public function __construct() {
// some things i don't know
}
public function view() {
// some things i don't know
}
}
您对我的设置有什么建议和意见。如何启动triad?好吧,我们不要过多地纠缠于实现细节。您可以在其他时间阅读有关保护框架的内容。让我们来处理你的问题 实际上,我创建了一个框架,它按照您试图实现的思路工作。我想你缺少的是一门RoutingHandler课程。路由是对URL的物理操作,它告诉应用程序要加载哪个控制器以及要运行哪个操作 在我的世界里,我也有模块,所以基本的路由方案是
Module -> Controller -> Action
这三项以这种方式映射到我的URI方案。变量也可以像这样附加
indexAction -> index.tpl.php
my_module
controllers
indexController.class.php
someotherController.class.php
:
:
models
HomeModel.class.php
:
:
templates
index.tpl.php
someother.tpl.php
:
:
那么,在解析URI并将控制权传递给相应的控制器和操作之后会发生什么呢?让我们编写一些代码来演示一个简单的示例
<?php
class indexController extends Controller {
protected function Initialize() {
$this->objHomeModel = new HomeModel;
$this->objHeader = new Header();
$this->objFooter = new Footer();
$this->objHeader
->SetPageId('home');
}
public function indexAction() {
$this->objHeader->SetPageTitle('This is my page title.');
}
}
?>
public function randomAction() {
$this->_CONTROL->Append($intSomeVar, 42);
}
Application::SetNoRender();
\u CONTROL
是我操作并传递到视图的值数组。Controller
类知道如何为视图找到正确的模板,因为它是以操作命名的(并且在同级目录中)
Controller
父类采用动作方法的名称,并像这样对其进行解析
indexAction -> index.tpl.php
my_module
controllers
indexController.class.php
someotherController.class.php
:
:
models
HomeModel.class.php
:
:
templates
index.tpl.php
someother.tpl.php
:
:
你也可以在这里做一些其他有趣的事情,例如
<?php
class indexController extends Controller {
protected function Initialize() {
$this->objHomeModel = new HomeModel;
$this->objHeader = new Header();
$this->objFooter = new Footer();
$this->objHeader
->SetPageId('home');
}
public function indexAction() {
$this->objHeader->SetPageTitle('This is my page title.');
}
}
?>
public function randomAction() {
$this->_CONTROL->Append($intSomeVar, 42);
}
Application::SetNoRender();
…将告诉控制器不要在模板内渲染,而只是完成该方法。这对于您实际上不想输出任何内容的情况非常有用
最后,所有的控制器、模型和视图都位于它们自己的目录中,就像这样
indexAction -> index.tpl.php
my_module
controllers
indexController.class.php
someotherController.class.php
:
:
models
HomeModel.class.php
:
:
templates
index.tpl.php
someother.tpl.php
:
:
我可以继续写下去,但我是凭记忆写的,到处都有皱纹,但希望这能让你深思。不清楚你真正的问题是什么。你能更新你的帖子来问一系列具体的问题吗?经过编辑,我把它说得非常清楚,我试过了too@JoeySalacHipolito您是否正在验证
$page
?如果没有,您就有一个本地文件包含漏洞,file\u exists()
无法阻止该漏洞。如何验证该漏洞?我有一个条件,如果$url[0]
为空,则将$config['default\u controller']
用作$page
,如果不是空的$page=$url[0]
。我还需要做其他事情吗?验证它,以便它只能包含a-z0-9或a-z。即使只是一个示例,您的Initialise()
方法正在创建新对象左-右-中心。这里需要补充一点:依赖注入或国际奥委会原则教育。一个词:“单身”,宝贝!我希望你的意思是“永远不要使用单身”。或针对该问题的静态方法;)你说的好像你是“决策者”。。。有很多方法和观点。如果只有一个,这个游戏就是杜尔斯维尔。当然,单身人士被过度使用,经常被误解,但他们有一席之地。当静态方法使用较少时也是如此。当然,它们经常被用来代替well thunk体系结构,但我不能解决这个问题。哦,当然。不,我不是决策者,但是我从那些比我更了解这些事情的人那里学习,然后把这些知识传递给他们,希望他们也会这样做。单例在PHP中没有位置。阅读,很有趣。静态使代码变得不稳定且难以遵循(也是糟糕的体系结构)——但如果你在工作中遇到这种情况,你就必须顺其自然。只需更好地了解当您为乐趣和利润而编写代码时:)