什么是PHP的面向对象框架初始化技术? [英] What are some PHP object-oriented framework initialization techniques?

查看:68
本文介绍了什么是PHP的面向对象框架初始化技术?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用页面设计的面向对象的框架,其中每个页面都扩展了该框架的视图.因此,例如,我的站点索引页实际上是一个类,该类实现了框架提供的抽象类View.我通过在页面顶部包含启动脚本来挂钩框架,在进行一些处理之后,框架将创建页面的实例并处理其视图数据.为了增加系统的灵活性,我不需要将类名作为文件本身的名称.这使我可以创建类似于支持类的东西,并使它充当/support/子域的索引.

最初,我是通过框架的构造函数将页面的类名传递给框架的,但是这又在页面的顶部或底部增加了一些步骤.我目前正在通过数据库中的页面表获取类名,该页面由已过滤的请求 URI 标识.我使用此表构建导航,添加一个额外的表列实际上是免费的.只要我有数据库连接,此方法就可以正常工作,但是我正在尝试为状态消息和错误报告实现静态页面支持,并且我需要另一种方法来创建页面类的实例.如果我使用将文件名命名为类的标准约定,那么我知道我有可靠的推断类名的方法.我真的不想仅仅出于演示的原因来命名我所有的类索引.对于如何初始化面向对象的框架有什么建议或标准?

View.inc

<?php
abstract class View
{
    abstract function getView();
}
?>

Startup.inc

<?php
    require_once("View.inc");
    require_once("CoreController.inc");

    $Framework = new CoreController();
    $Framework->Initialize();
    exit;
?>

index.php

<?php
    require_once("Startup.inc");

    class Index extends View
    {
        public function getView()
        {
            echo "<pre>View Data</pre>";
        }
    }
?>

在我的框架内,我有一个处理页面的TemplateController.页面类是已知的,因为它是通过另一个类映射的.但是,如果没有数据库,我想在index.php中发现类,而无需更改其当前设置方式.这是TemplateController中的实际代码.

//Get the View Class from Page
$view = $page->getPageClass();

//We need to catch View creation failure
$this->Page = new $view($this->Framework);

//Initialize the Page View
$this->Page->Initialize();

//Cache the Page View
$this->cacheView($this->Page, $page->getPageName(), $this->SiteID.TCS_PAGE_SORTID);

$view上方的代码段中,是从另一个控制器映射的类的实际名称.我正在将对框架的引用传递给视图的构造函数,而其余部分确实无关紧要.我正在尝试提出一种类似的映射技术,以在数据库关闭的情况下识别页面类.我喜欢一个用于框架启动的包含行,并且希望保持简单.

在TemplateController的顶部,我还必须重新包含实际的页面,因为我的启动脚本位于顶部,所以即使它是请求的页面,也不包含实际的类.

include($_SERVER['SCRIPT_FILENAME']);

请查看堆栈溢出问题 从$ _SERVER ['SCRIPT_FILENAME]中识别类名'] 了解有关我正在尝试执行的操作的更多信息.

解决方案

这是我的页面路由检查清单:

  • 添加新页面必须快速
  • 您一定不能无意中添加页面
  • 具有百万个页面的应用程序不应比具有2个页面的应用程序更慢或更or肿
  • 提供简单的方法,让人们根据需要对其进行增强
  • 允许轻松进行重构,例如将多个页面移动到不同的位置
  • 使框架能够检测所有内容.不要强迫用户指定基本URL等.
  • 创建简单易懂的页面名称(hello/world).
  • 从URL中清除非法字符
  • 使添加静态页面变得容易
  • 提供一种从页面名称生成URL的方法.
  • 通过更改URL中页面前后的显示内容,允许用户使用漂亮的URL

最重要的是 -停止复制,考虑最好的方法.

我在 PHP UI框架-Agile Toolkit 中使用了所有这些建议. >

相关来源: 敏捷工具包-带有jQuery的PHP框架 敏捷工具包-PageManager.php 敏捷工具包-URL.php 敏捷工具包-PathFinder.php .

我们希望对开发人员来说真的很简单.并确保安全.且灵活.因此,将根据URL选择页面"类.如何?很简单:

/hello.html -> page_hello /hello/world.html -> page_hello_world

然后将类映射到文件名中. page/hello/world.php

然后在某些情况下我们也要使用动态URL,例如:/article/234

许多框架在这里实现了复杂的技术(数组,正则表达式,前端控制器).我们没有.有mod_rewrite.只需将其重写为page=article&id=234即可.并缩放.

除页面外,还有其他类,但是这些类不能直接访问.因此,页面位于/page/文件夹下,请对其进行区分并维护安全性.

然后设计者说-"我不会写PHP ".因此,我们介绍了静态页面.如果未定义类page_hello_world,我们将研究template/skin/page/hello/world.html.对于设计人员和非开发人员而言,这是添加新页面的便捷捷径.

如果找不到页面怎么办? Api->pageNotFound()被调用.可以通过SQL重新定义和加载页面,或显示带有粉红色大象图片的404页面.

然后有一些页面来自加载项.我们不想默认启用它们,而是让用户将它们添加到他们的应用程序中的某个位置.怎么样?

class page_hello_world extends Page_MegaPage

下一个问题是我们如何处理该页面的子页面,因为有时所有功能都无法在单个页面上显示.好吧,让我们添加对page_edit()方法(或任何page_XX)的支持,作为这些类内子页面的别名.现在,附加开发人员可以包括一个多功能页面,该页面可以扩展,定制并放置在应用程序中的任何位置.

最后,有些黑客想真正快速地做一些事情.对于他们,我们将相同的技术应用于API.您可以在API中定义函数page_hello_world,并且不需要类.

有人可能会说,做一件简单的事情有太多的方法.哦,好吧.

我希望这会有所帮助.

I have an object oriented framework that uses a page design, where each page extends a view of the framework. So, for instance, my index page for a site is actually a class that implements the abstract class View provided by the framework. I hook the framework by including a startup script at the top of the page and after some processing the framework creates an instance of the page and processes its view data. To add flexibility to the system, I don't require the class name to be the name of the file itself. This allows me to create something like a support class and have it behave as the index for the /support/ subdomain.

I was initially passing the page's class name into the framework via the framework's constructor, but this added a few more steps to the top or bottom of the page. I currently obtain the class name via a pages table in the database identified by a filtered requested URI. I use this table to build navigation and adding an extra table column was practically free. This works OK as long as I have a database connection, but I'm trying to implement static page support for status messages and error reporting, and I need another method for creating an instance of the page's class. If I use the standard convention of naming the class the file's name, then I know I have a dependable way of extrapolating the class name. I don't really want to name all my classes index just for presentation reasons. What is some advice or some standards for how object oriented frameworks are initialized?

View.inc

<?php
abstract class View
{
    abstract function getView();
}
?>

Startup.inc

<?php
    require_once("View.inc");
    require_once("CoreController.inc");

    $Framework = new CoreController();
    $Framework->Initialize();
    exit;
?>

index.php

<?php
    require_once("Startup.inc");

    class Index extends View
    {
        public function getView()
        {
            echo "<pre>View Data</pre>";
        }
    }
?>

Within my framework I have a TemplateController that processes the page. The page class is known because it is mapped via another class. Without a database however, I would like to discover the class within, say, index.php without changing the way it's currently set up. Here is the actual code within the TemplateController.

//Get the View Class from Page
$view = $page->getPageClass();

//We need to catch View creation failure
$this->Page = new $view($this->Framework);

//Initialize the Page View
$this->Page->Initialize();

//Cache the Page View
$this->cacheView($this->Page, $page->getPageName(), $this->SiteID.TCS_PAGE_SORTID);

In the snippet above $view is the actual name of the class that was mapped from another controller. I'm passing a reference to the framework to the view's constructor and the rest is really irrelevant. I'm trying to come up with a similar mapping technique to identify the page class in the event the database is down. I like the one include line for the framework startup and would like to keep it that simple.

At the top of the TemplateController I have to also reinclude the actual page, since my startup script is at the top, the actual class is not included even though it is the requested page.

include($_SERVER['SCRIPT_FILENAME']);

Please review Stack Overflow question Identifying class names from $_SERVER['SCRIPT_FILENAME'] for more information on what I'm attempting to do.

解决方案

Here is my checklist of page routing:

  • Adding new page must be quick
  • You must not be able to add page unintentionally
  • Application with million pages should not be slower or more bloated than application with 2 pages
  • Provide simple way and let people enhance it if they want
  • Allow easy re-factoring, such as moving several pages into different location
  • Make framework detect everything. Don't force user to specify base URL, etc.
  • Create simple to understand page names (hello/world).
  • Clean illegal characters from the URL
  • Make it possible to add static pages easy
  • Provide a way to generate URLs from page names.
  • Allow user to use pretty URLs by changing what appears before and after the page in the URL

And most importantly - Stop copying, think about the best approach.

All of those suggestions I have used in the PHP UI framework - Agile Toolkit where I am a contributor.

Relevant sources: Agile Toolkit - a PHP Framework with jQuery, Agile Toolkit - PageManager.php, Agile Toolkit - URL.php and Agile Toolkit - PathFinder.php.

We wanted to make it really simple for developers. And secure too. And flexible. Therefore the "page" class is selected based on the URL. How? Quite easy:

/hello.html -> page_hello /hello/world.html -> page_hello_world

Class then is mapped into filename. page/hello/world.php

Then there are cases when we want to use dynamic URLs too, such as: /article/234

Many frameworks implement a complex techniques here (arrays, regular expressions, front-controllers). We don't. There is mod_rewrite for this. Just rewrite this into page=article&id=234 and it works. And scales.

Apart from pages, there are other classes, but those classes can't be accessed directly. Therefore pages live under the /page/ folder, do distinguish them and maintain security.

Then comes the designer saying - "I won't write PHP". So we introduce static pages. If class page_hello_world is not defined, we'll look into template/skin/page/hello/world.html. That's a easy shortcut for designers and non-developers to add a new page.

What if a page is not found? Api->pageNotFound() is called. Feel free to redefine and load pages from SQL or display 404 page with a picture of a pink elephant.

And then there are some pages which come from add-ons. We don't want to enable them by default, but instead let users add them to their app somewhere. How?

class page_hello_world extends Page_MegaPage

Next question, is how we handle sub-pages of that page, since sometimes all the functionality can't fit on single page. Well, let's add support for page_edit() method (or any page_XX) as an alias for a sub-page inside those classes. Now add-on developer can include a multifunctional page which can be extended, customized and placed anywhere in the application.

Finally there are hackers, who want to do something really fast. For them we apply same technique to the API. You can define function page_hello_world in the API, and you don't need class.

Some might say that there are too many ways to do a simple thing. Oh well.

I hope that this was helpful.

这篇关于什么是PHP的面向对象框架初始化技术?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆