php – 初始化视图,模板和控制器和模型是可选的

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 初始化视图,模板和控制器和模型是可选的脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在组织我的代码,并决定组织mvc’ish(像mvc一样),我不知道是否可以遵循所有的原则,但我想要至少接近这一点.

我的应用程序有一个前台控制器(dunno如果我的定义是正确的),所以我的应用程序的所有http请求将通过一个点,在我的情况下,index.PHP在我的应用程序的根目录.

说完我已经设置好了,你可以想象,我使用.htaccess来引导所有的请求到index.PHP.

我爆炸了url,并创建了一个数组,$url []像这样.所以每当我访问我的应用程序像这样http:// localhost / app / pagename它将访问一个控制器(pagename_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();

我不知道是否使用$controller变量名称是适当的,所以请原谅我,当它是错误的.

我想我可以这样设置我的应用程序:

>用户一个请求,怎么样?通过使用应用程序意味着他/她发出一个http请求,这将加载应用程序的初始状态

正如你可以在我想要的应用程序结构的图中看到的,我只能做到第一部分是将请求引导到单个条目(index.PHP)

现在的问题是应用程序的其他部分的初始化.

在这一刻,我有3个文件,我想设置,但我很困惑如何.

index_controller,index_view,Template

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
}

现在我想知道现在如何将这些东西联系在一起.目前我有一个可以包含类的系统文件夹,并为该文件夹设置一个自动加载程序.

我正在考虑创建一个Controller类和View类,它充当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
    }
}

在我的设置中你有什么建议和评论.如何发起黑社会?

那么,让我们不要太多地停留在你的实现细节上.您可以在其他时间阅读有关保护框架的信息.我们来处理你的问题

我实际上创建了一个框架,沿着你想要实现的线条工作.我想你缺少的是RoutingHandler类.路由是URL的物理操纵,它告诉您的应用程序要加载哪个Controller,以及要运行的Action.

在我的世界我也有模块,所以基本的路由方案

Module -> Controller -> Action

这三个项目以这种方式映射到我的URI方案.变量也可以这样添加

http://www.domain.com/module/controller/action/var1/val1/var2/val2

那么在解析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.');
        }
    }
?>

在Initialize方法中,我设置了一些控制器范围的东西,并且抓住了我的Model的一个实例以供以后使用.真正的肉是在indexAction方法中.这就是您要在视图中设置东西的地方.例如…

public function randomAction() {
    $this->_CONTROL->ApPEnd($intSomeVAR,42);
}

_CONTROL是我操纵并传递到View的值的数组. Controller类知道如何为View找到正确的模板,因为它是以Action(和一个兄弟目录)命名的.

Controller父类接受action方法名称,并解析它,因此…

indexAction -> index.tpl.PHP

你也可以在这里做一些其他有趣的东西,例如…

Application::SetNoRender();

…会告诉Controller不要在模板中渲染,而只是完成该方法.这对于您实际上不想输出任何内容的情况非常有用.

最后,所有的控制器,模型和视图都在自己的目录中,如

my_module
    controllers
        indexController.class.PHP
        someotherController.class.PHP
        :
        :
    models
        HomeModel.class.PHP
        :
        :
    templates
        index.tpl.PHP
        someother.tpl.PHP
        :
        :

我可以继续,但是我从记忆中写下来,这里和那里有一些皱纹,但希望这给你的思想的食物.

脚本宝典总结

以上是脚本宝典为你收集整理的php – 初始化视图,模板和控制器和模型是可选的全部内容,希望文章能够帮你解决php – 初始化视图,模板和控制器和模型是可选的所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。