You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classMyFilterextendsCFilter
{
protectedfunctionpreFilter($filterChain)
{
// logic being applied before the action is executedreturntrue; // false if the action should not be executed
}
protectedfunctionpostFilter($filterChain)
{
// logic being applied after the action is executed
}
}
上面两个方法都携带一个类型为 CFilterChain Class 的参数 $filterChain.
1. What is Filter?
2. Creating a Filter Extension
Filter extension 必须继承自
CFilter
或其子类。实现过程主要涉及两个方法:CFilter::preFilter()
和CFilter::postFilter()
.上面两个方法都携带一个类型为
CFilterChain
Class 的参数$filterChain
.我们可以向 Controller Extension 那样,将 Filter Extension 也放在
extensions/
目录下,Practise
读完后立刻想到 commit :在想要进行访问控制的 Controller 中都添加了内置的
accessControl
filter, 此 filter 需要在accessRules()
内配置。这个 filter 功能很强大。为了实践本节学的理论知识,我们创建一个名为LoginFilter
的 filter extension, 简单实现用户是否登录的 filter.我们在
BackController
中不再使用AccessControl
Filter, 改用LoginFilter
来达到禁止未登录用户访问的目的:LoginFilter
适用于BackController
内所有 actions, 有时我们需要 filter 仅针对部分 actions, 这时我们可以在 filter path alias 后面使用+
,-
, 后面跟着 action name, 来实现指定 actions. 例如 #15 中的一个实例:The text was updated successfully, but these errors were encountered: