覆盖 Application.cfc 中的 onMissingTemplate 处理
我想处理这样的场景:当该模板不存在时,用户点击 /somePage.cfm 的 url,并使用另一个目录中的模板。 我知道我可以通过在 apache 等中重写来做到这一点,但我真的不想在那里存储逻辑,所以我尝试覆盖 Application.cfc 中的 onTemplateMissing
行为。
它在我的测试中似乎运行良好,但我担心通过执行这个黑客解决方案,我会缩短一些我还没有看到的部分(例如我当前没有使用的方法,例如 onSessionStart 等)并且将来可能会遇到问题。
以下是我当前正在做的事情:
<cffunction name="onMissingTemplate">
<cfargument name="targetPage" />
<!--- Handle any templates that we're really loading from elsewhere --->
<cfif isFooTemplate(arguments.targetPage)>
<cfset onRequestStart(arguments.targetPage) />
<cfset onRequest(arguments.targetPage) />
<cfset onRequestEnd(arguments.targetPage) />
<cfreturn true />
</cfif>
<cfreturn false />
</cffunction>
请注意,在我的 onRequest
方法中,我还在对 isFooTemplate()
将返回 true 的模板进行进一步处理。
I want to handle a scenario where user hits a url of /somePage.cfm when that template doesn't exist and use a template from another directory. I know I can do this via rewrites in apache etc. but I don't really want to store logic in there so I gave trying to override onTemplateMissing
behaviour in my Application.cfc.
It seems to be working fine in my testing but I'm worried by doing this hacky solution I'm short cutting some parts that I haven't seen yet (e.g. methods that I'm not currently using such as onSessionStart etc.) and may run into issues in the future.
Here is what I'm currently doing:
<cffunction name="onMissingTemplate">
<cfargument name="targetPage" />
<!--- Handle any templates that we're really loading from elsewhere --->
<cfif isFooTemplate(arguments.targetPage)>
<cfset onRequestStart(arguments.targetPage) />
<cfset onRequest(arguments.targetPage) />
<cfset onRequestEnd(arguments.targetPage) />
<cfreturn true />
</cfif>
<cfreturn false />
</cffunction>
Note that also in my onRequest
method I'm doing further handling for templates that isFooTemplate()
would return true to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为这是一个 hacky 解决方案。 这就是该方法的用途,如果需要,返回 false 时,ColdFusion 将调用您在管理员中设置的标准错误处理程序。
onSessionStart() 未运行的唯一情况是用户在第一个页面请求上点击 onMissingTemplate() 。 如果您出于某种原因需要用户拥有会话,您可以检查会话范围是否存在,因为会话范围应该在 onMissingTemplate() 方法中可用并进行适当处理。
I don't think this is a hacky solution. This is what the method is for, and on returning false, ColdFusion will invoke the standard error handler you setup in the administrator if you want.
The only case were onSessionStart() hasn't run is if the user hits the onMissingTemplate() on the first ever page request. If you for some reason need the user to have a session, you can check for the existence of the session scope, since the session scope is supposed to be available in the onMissingTemplate() method and handle appropriately.
它实际上是onMissingTemplate而不是onTemplateMissing; 这是推荐的做法,根本不是“hacky”。 你这样做就很好了。
It's actually onMissingTemplate not onTemplateMissing; and this is a recommended practice, not 'hacky' at all. You're fine doing it this way.