coldfusion
One of the greatest and probably most anticipated features to come with Macromedia’s Coldfusion MX is ColdFusion Components (CFCs). In its most basic form, a CFC is a collection of code and data that’s stored in a central file. In the past, you’ve probably created a file that contained all your functions, and which you could simply include in a document to have all the functions available to you. This is a very simple application of CFCs, but, as we’ll see, it’s not the whole story.
Macromedia的Coldfusion MX附带的最大,可能也是最值得期待的功能之一是ColdFusion组件(CFC)。 在最基本的形式中,CFC是存储在中央文件中的代码和数据的集合。 过去,您可能已经创建了一个包含所有功能的文件,并且可以将其简单地包含在文档中以使所有功能可用。 这是CFC的非常简单的应用程序,但是,正如我们将看到的,这还不是全部。
CFCs represent an attempt by Macromedia to bring ColdFusion closer to an Object Oriented Programming (OOP) language. ColdFusion is in no way an OOP language, but thanks in part to CFCs, it does boast some of the attributes that make OOP languages so popular. For instance, ColdFusion gives developers the ability to extend a piece of code to include items from another piece of code; it also gives us the ability to transplant and reuse code across a variety of systems and applications.
CFC表示Macromedia试图使ColdFusion更接近于面向对象编程(OOP)语言。 ColdFusion绝不是OOP语言,但部分要归功于CFC,它确实拥有使OOP语言如此受欢迎的一些属性。 例如,ColdFusion使开发人员能够扩展一段代码以包含另一段代码中的项; 它还使我们能够跨各种系统和应用程序移植和重用代码。
So how do CFCs bring ColdFusion closer to OOP, and what’s the rest of the story?
那么,CFC如何使ColdFusion更接近OOP,其余的内容又是什么?
CFCs are not your normal function inclusion pages. When you create a CFC, you gain access to many more features than you would if you created an include page that contained a bunch of functions. What exactly are these features and benefits? They include:
CFC不是您的正常功能包含页面。 创建CFC时,与创建包含一堆功能的包含页面相比,您可以获得更多的功能。 这些功能和好处到底是什么? 它们包括:
-
Security – you have the ability restrict access to your CFCs, and the methods within them, based on user roles and settings in your CF setup.
安全性 –您可以根据CF设置中的用户角色和设置来限制对CFC及其中方法的访问。
-
Speed – CFCs are compiled so they execute faster. Keep this in mind when building your sites, as the first time a CFC is called, it is compiled and the output is returned; you might want to run through your site before releasing it to the public. Also remember that each time you make and upload a change to your code, the CFC will have to be recompiled — but there’s no need to worry, as the server handles all this for you.
速度– CFC被编译,因此它们执行得更快。 在构建站点时,请记住这一点,因为第一次调用CFC时,会对其进行编译并返回输出。 您可能需要先浏览网站,然后再将其发布给公众。 还要记住,每次对代码进行更改并将其上传到计算机上时,都必须重新编译CFC,但是不必担心,因为服务器会为您处理所有这一切。
-
Extensibility – CFCs can be expanded upon and they can share methods with other CFCs, much like Java and PHP classes. CFCs can also be called externally using the SOAP protocol or URL calls, which allows you to make them available to other programmers and other languages.
可扩展性– CFC可以扩展,并且可以与其他CFC共享方法,就像Java和PHP类一样。 也可以使用SOAP协议或URL调用从外部调用CFC,这使您可以将其提供给其他程序员和其他语言使用。
-
Reusability – If you code your CFC properly and are mindful of the implications of extensibility, your CFC can be shared and moved from server to server and script to script.
可重用性–如果您对CFC进行了正确的编码,并且牢记可扩展性的含义,则可以共享CFC,并在服务器与服务器之间以及脚本与脚本之间进行迁移。
-
Local Reusability – Once a CFC method is invoked on a page, it’s typically reusable throughout that page. There are some exceptions to this rule of thumb, but the potential for code reuse means you don’t have to call the function multiple times on the same page.
本地可重用性–在页面上调用CFC方法后,通常可以在整个页面中重用它。 此经验法则有一些例外,但是潜在的代码重用意味着您不必在同一页面上多次调用该函数。
-
Documentation – CFCs can generate their very own documentation using the hint attributes in the cfcomponent and cffunction tags. To see this functionality at work, simply open up a CFC, for example http://www.example.com/myCFC.cfc, in a Web browser (note that you will have to login with either a RDS or Administrator login to see the page). The server will generate a page that documents myCFC.
文档– CFC可以使用cfcomponent和cffunction标记中的hint属性生成自己的文档。 要在工作中看到此功能,只需在Web浏览器中打开一个CFC,例如http://www.example.com/myCFC.cfc (请注意,您必须使用RDS或管理员登录名登录才能看到这一页)。 服务器将生成一个记录myCFC的页面。
-
Forward Thinking – CFCs are represent the future for ColdFusion; it’s my opinion that we should embrace them now and push them to the limit so that the nice folks over at Macromedia will continue to improve upon them.
前瞻性思考–氟氯化碳代表ColdFusion的未来; 我认为我们应该现在就接受它们并将其推向极限,以便Macromedia的优秀人才将继续对它们进行改进。
In the old days before ColdFusion MX, you might have created custom tags or User Defined Functions (UDFs), or found some other way to develop reusable code. CFCs allow you to achieve this aim, while delivering the added benefits listed here.
在ColdFusion MX之前的过去,您可能已经创建了自定义标签或用户定义函数(UDF),或者找到了其他方法来开发可重用代码。 使用CFC可以实现此目标,同时提供此处列出的其他好处。
术语 (Terminology)
Before we jump into a simple CFC, we need to understand some of the key terminology.
在开始使用简单的CFC之前,我们需要了解一些关键术语。
First, a CFC is called a Component, or a ColdFusion Component, and you can think of it as a container. Now, an empty container has no value until you fill it. We fill the component with methods, which are defined by the <cffunction>
tag.
首先,CFC称为组件或ColdFusion组件,您可以将其视为容器。 现在,一个空容器没有值,直到您将其填充。 我们用<cffunction>
标记定义的方法填充组件。
Once we have a component and fill it with methods, we can then invoke specific methods, which can be found in the component, using the <cfinvoke>
tag.
一旦有了组件并用方法填充它,便可以使用<cfinvoke>
标记调用特定方法,这些方法可以在组件中找到。
一个简单的CFC (A Simple CFC)
Now that you have a general overview of the flow and lingo, let’s put this knowledge to work so you can really grasp the concept of CFCs.
现在,您已经对流程和术语有了一个大致的了解,现在让我们运用这些知识,以便您可以真正掌握CFC的概念。
The first thing we need to do is create a new file and save it with a .cfc extension. Call this file users.cfc; we’ll use it to store all our user methods. I also recommend you store all your CFCs in a central folder called components, cfc — whatever name takes your fancy.
我们需要做的第一件事是创建一个新文件,并将其保存为.cfc扩展名。 将此文件命名为users.cfc; 我们将使用它来存储我们所有的用户方法。 我还建议您将所有CFC都存储在一个名为components cfc的中央文件夹中,无论您喜欢哪种名称。
Now, open this file in your favorite text editor and type the following:
现在,在您喜欢的文本编辑器中打开此文件,然后键入以下内容:
<cfcomponent displayname="Users CFC" hint="This is the CFC for my user stuff">
<!--- This function gets all the users and their data from the DB --->
<cffunction name="get_all_users" hint="Gets all users in the database" returntype="query">
<cfquery name="get_em" datasource=#APPLICATION.db_source#>
select * from from users
</cfquery>
<cfreturn get_em>
</cffunction>
</cfcomponent>
Save your file, so you don’t lose the work, and let’s go through the code line by line.
保存文件,以免丢失工作,让我们逐行浏览代码。
<cfcomponent displayname="user cfc" hint="This is the CFC for my user stuff">
<cfcomponent displayname="user cfc" hint="This is the CFC for my user stuff">
This first line tells the CF server that this is a Coldfusion Component. The displayname
is the name that displays when documentation is generated; the hint
contains descriptive text about the CFC as a whole.
第一行告诉CF服务器这是Coldfusion组件。 所述displayname
的是,当产生显示的文档的名称; hint
包含有关整个CFC的描述性文字。
<!--- This function gets all the users and their data from the DB --->
<cffunction name="get_all_users" hint="Gets all users in the database" returntype="query" output="false">
Next we see a comment that describes what the CFC does. After the comment is a cffunction
tag with the attribute’s name
, hint
, returntype
, and output setting
. name
presents the name of the function being called. hint
presents descriptive text about this function. returntype
tells the CF server what kind of data is going to be returned by this function. And finally, output
tells the server whether this function acts as if it’s in a <cfoutput>
tag, or a <cfsilent>
tag. Basically, we use the output
setting to define whether we want the function to process cfoutput tags within the cffunction
(output="true"
), or we want those tags to be suppressed (output="false"
).
接下来,我们将看到描述CFC功能的注释。 注释后是带有cffunction
name
, hint
, returntype
和output setting
的cffunction
标记。 name
表示要调用的函数的名称。 hint
显示有关此功能的描述性文字。 returntype
告诉CF服务器此函数将返回哪种数据。 最后, output
告诉服务器此功能的作用类似于在<cfoutput>
标记还是<cfsilent>
标记中。 基本上,我们使用output
设置定义我们是否希望内的函数来处理CFOUTPUT标签cffunction
( output="true"
),或我们希望这些标签将被抑制( output="false"
)。
You have a few choices for returntype
, so be sure to pick the one that suits your function. These types are pretty self-explanatory:
对于returntype
,您有几种选择,因此请务必选择适合您功能的选择。 这些类型非常不言自明:
- Any
任何
- Array
数组
- Binary
二元
- Boolean
布尔型
- Numeric
数字
- Date
日期
- Guid
引导
- Query
询问
- String
串
- Struct
结构
- Uuid
Uuid
- Variablename
变量名
- Void
空洞
On top of all this, we could also define access
and role
attributes to help control who has access to the function and its output. The roles
attribute works in unison with the cflogin
tag. We can define roles
, etc., with the cflogin
tag; those roles then dictate the specific methods a user can and cannot invoke. We could devote a whole article to this topic but, in short, roles are great for security when your application utilizes the built-in authentication of ColdFusion. If you’d like to know more about these topics, check out the ColdFusion documentation and Macromedia Website.
最重要的是,我们还可以定义access
和role
属性,以帮助控制谁有权访问该功能及其输出。 roles
属性与cflogin
标记一起使用。 我们可以使用cflogin
标记定义roles
等。 这些角色然后决定了用户可以调用和不能调用的特定方法。 我们可以为该主题写整篇文章,但是简而言之,当您的应用程序利用ColdFusion的内置身份验证时,角色对于安全性至关重要。 如果您想进一步了解这些主题,请查看ColdFusion文档和Macromedia网站 。
<cfquery name="get_em" datasource=#APPLICATION.db_source#>
select * from from users
</cfquery>
This next snippet of code represents the actual query that we’ll run against the database. You’ll notice that I’ve used the variable #APPLICATION.db_source#
instead of naming a specific datasource. This is a personal code preference, so feel free to either store this value in your application.cfml using the cfset
tag, or specify the datasource name as it is set up in the CF Administrator.
下一个代码片段表示将对数据库运行的实际查询。 您会注意到,我使用了变量#APPLICATION.db_source#
而不是命名特定的数据源。 这是个人代码首选项,因此可以使用cfset
标记将此值存储在application.cfml中,也可以指定在CF Administrator中设置的数据源名称。
<cfreturn get_em>
Next we have to return the query to the page that called this function. As we want to return the entire query to the calling page, and because we set returntype="query"
, we insert the name of the query as it appears in the name attribute for the cfquery
tag.
接下来,我们必须将查询返回到调用此函数的页面。 因为我们想将整个查询返回到调用页面,并且因为我们设置了returntype="query"
,所以我们插入了查询的名称,因为它出现在cfquery
标记的name属性中。
One thing to keep in mind for the cfreturn
tag is that it will return only one value at a time. So if you want or need multiple values returned, you must put them in an array or structure, and then return that array or structure.
cfreturn
标签要记住的一件事是,它一次只会返回一个值。 因此,如果您希望或需要返回多个值,则必须将它们放入数组或结构中,然后返回该数组或结构。
</cffunction>
</cfcomponent>
Finally, we close out the function and component tags.
最后,我们关闭了功能和组件标签。
使用并致电您的CFC (Use and Call Your CFC)
So there you have it! You’ve just built your first component and function. But now you need to know how to use and call it. For this we need to create a new page and save it as users.cfml or users.cfm. Once this page is opened, saved, and ready to go, insert the following line at the top of the page, before any HTML code:
所以你有它! 您刚刚构建了第一个组件和功能。 但是现在您需要知道如何使用和调用它。 为此,我们需要创建一个新页面并将其另存为users.cfml或users.cfm。 打开,保存并准备好该页面后,请在页面顶部的任何HTML代码之前插入以下行:
<cfinvoke component="cfc.users" method="get_all_users" returnvariable="u"></cfinvoke>
The above line is our cfinvoke
tag, which is used to invoke a method within a component. The break down of this is rather simple. The component="cfc.users"
tells the server what component you want to grab, and where to find it. As I saved my CFC in a folder named “cfc”, I had to use dot notation to tell the server the location, and which file I wanted. If you named your directory “components”, your code would read: component="components.users"
. If you had a more complex directory structure, such as /components/users/general/, your code would resemble this: component="compontents.users.general.users"
上一行是我们的cfinvoke
标记,用于在组件中调用方法。 分解很简单。 component="cfc.users"
告诉服务器您要获取哪个组件以及在哪里找到它。 当我将CFC保存在名为“ cfc”的文件夹中时,我必须使用点表示法来告知服务器位置以及所需的文件。 如果将目录命名为“ components”,则代码将显示为: component="components.users"
。 如果您具有更复杂的目录结构,例如/ components / users / general /,则您的代码将类似于以下内容: component="compontents.users.general.users"
The next part, method="get_all_users"
, tells the server which method we want to use. Finally we need to specify how the returned data will be referred to. For this case we have chosen a simple “u”, as in user, which we’ve specified using returnvariable="u"
.
下一部分method="get_all_users"
告诉服务器我们要使用哪种方法。 最后,我们需要指定如何引用返回的数据。 对于这种情况,我们选择了一个简单的“ u”,如在用户中一样,我们使用returnvariable="u"
指定了它。
Next, we want to create the following bit of HTML and ColdFusion code to output our data:
接下来,我们要创建以下HTML和ColdFusion代码以输出数据:
<table width="100%" border="1" cellspacing="0" cellpadding="3">
<tr>
<td>Users Name</td>
<td>Users Login</td>
<td>Users Email</td>
<td>Users Status</td>
</tr>
<cfoutput query="u">
<tr>
<td>#name#</td>
<td>#login#</td>
<td>#email#</td>
<td>#status#</td>
</tr>
</cfoutput>
</table>
This will output a nice little table with a row of headings for each column in the table, and a new row for each corresponding row/user in the table.
这将输出一个漂亮的小表,该表的每个列都有一行标题,表中的每个对应行/用户都有一个新行。
You’ll notice that we used the <cfoutput query="u">
tag. This allows us to quickly output the data in a query without having to use loops. The CF server will know that it needs to execute the cfoutput tag, and the code it contains, until the query reaches the end of the output.
您会注意到我们使用了<cfoutput query="u">
标记。 这使我们能够快速输出查询中的数据,而不必使用循环。 CF服务器将知道需要执行cfoutput标记及其包含的代码,直到查询到达输出末尾为止。
So there you have it! You’ve successfully created a component and a function, invoked that method in a separate page, and output the data.
所以你有它! 您已经成功创建了组件和函数,并在单独的页面中调用了该方法,并输出了数据。
It’s my hope that this article has helped you to better understand CFCs and that it has sparked your interest in learning more. This is by no means a complete overview of CFCs — there are many topics that were hinted at, but not expanded on here. I suggest that you read the documentation found on the Macromedia Website or on your server, and check out the Macromedia Coldfusion DevNet for some great CF and fusion组件库_fusion框架分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/67425.html