Jump to content
Larry Ullman's Book Forums

What Is A Filter Chain? What Does "Invoking Its Run() Method" Mean?


KeepLearning
 Share

Recommended Posts

version 0.5

 

Page 154 - 155

 

Larry,

 

1. What exactly is is a filter chain?

 

2. In the particular example given, does "invoking its run() method" mean invoking the "account" action's run method?

 

3. Where can I find the "account" action? Is it in the yii_cms? Should I search for "actionAccount" as a string within the files in the Protected folder?

 

4. If the "Account" action is hypothetical, would it really have a method called "run()"? If not, what is a run() method?

 

 

Quoting from the book"

 

"The method needs to take one parameter, which will be a filter chain. This variable will be used to allow the action to take place by invoking its run() method (i.e., continue the filtering and such)."

 

The example given is:

 

 

public function filterHttpsOnly($fc) {
if (Yii::app()->getRequest()->getIsSecureConnection()) {
$fc->run();
} else {
throw new CHttpException(400,
'This page needs to be accessed securely.');
}
}
 
 
public function filters() {
return array(
'accessControl',
// account must be accessed over HTTPS:
'httpsOnly + account',
);
}
 
 

Thanks.

Link to comment
Share on other sites

version 0.5

 

Page 154 - 155

 

Larry,

 

1. What exactly is is a filter chain?

 

A filter chain is a list of filters. See: http://www.yiiframework.com/doc/guide/1.1/en/basics.controller#filter

 

2. In the particular example given, does "invoking its run() method" mean invoking the "account" action's run method?

 

No, running the filter chain's run() method, as in the code that follows.

 

3. Where can I find the "account" action? Is it in the yii_cms? Should I search for "actionAccount" as a string within the files in the Protected folder?

 

This is a hypothetical. I suspect I started this discussion (in the book) with something like "If you had...".

 

4. If the "Account" action is hypothetical, would it really have a method called "run()"? If not, what is a run() method?

 

Again, as in the code you posted, the run() method is defined within the filter chain object.

Link to comment
Share on other sites

Larry,

 

I think you are saying:

 

The following code dictates that the httpsOnly filter should be applied to the "account" action:

 

 

public function filters() {
return array(
'accessControl',
// account must be accessed over HTTPS:
'httpsOnly + account',
);
}
 
The above code calls the following function, which verifies there is a secure connection before running $fc:
 
public function filterHttpsOnly($fc) {
if (Yii::app()->getRequest()->getIsSecureConnection()) {
$fc->run();
} else {
throw new CHttpException(400,
'This page needs to be accessed securely.');
}
}
 
I guess $fc is the array returned by the previous function:
 
array('accessControl', 'httpsOnly + account',);
 
But how can you "run" an array?
Link to comment
Share on other sites

The filters() method declares what functions should be called before actions are executed (if any). So just listing a function within filters() tells the controller to execute the associated functions.  

 

When those filter functions are called, they will automatically be provided with a "filter chain". If you look at the link I provided previously, you'll see that the filter chain is an instance of the CFilterChain class. So the filter chain is an object, not an array. 

Link to comment
Share on other sites

The filters() method declares what functions should be called before actions are executed (if any). So just listing a function within filters() tells the controller to execute the associated functions.  

 

Larry, I think you are saying that in this code:

 

 

"public function myFunctionName() {
return array('accessControl',"

 

the "accessControl" part tells Yii that this function is calling a filter (from page 142). The function could be named anything, such as "myFunctionName". It does not need to be named "filters()".

 

The next part:

 

 

", 'httpsOnly + account',);
}"
 
tells Yii that httpsOnly must be applied to the account action.
 
Is this correct?
 
By the way, why is there a comma after 'account'?
Link to comment
Share on other sites

Larry, I think you are saying that in this code:

 

 

"public function myFunctionName() {
return array('accessControl',"

 

the "accessControl" part tells Yii that this function is calling a filter (from page 142). The function could be named anything, such as "myFunctionName". It does not need to be named "filters()".

 

No, no, not at all. The function MUST be named filters(). The filters() method of a controller tells the controller what filter methods to invoke. Put another way, the controller looks at the filters() method to see what checks must be executed. Those "checks" are other methods, such as the https only method.

 

The "accessControl" part says to use the access control filter. 

 

 

The next part:

 

 

", 'httpsOnly + account',);
}"
 
tells Yii that httpsOnly must be applied to the account action.
 
Is this correct?
 
Yes, exactly.
 

 

By the way, why is there a comma after 'account'?

 

That's just a comma at the end of the array items. It doesn't need to be there but there's no harm in it being there.

 

By the way, if I'm terse in my responses, please understand it's only because I'm busy, and providing free technical support to anyone that asks takes a lot of time (and takes me away from paying work).

Link to comment
Share on other sites

Your answer this time really cleared up this question for me. Thanks.

 

Actually, I'm amazed that you are so responsive. On the other hand, customer feedback can help any business improve their product. Your product is your book, and I suppose your reader feedback helps you polish your book. It's a great idea, if you have the time and motivation.

 

By the way, I do appreciate the other answers you have provided -- even those that I did not thank you for specifically. I don't want to clutter the forum with posts that say "Thanks", but have no other value. Anyway, I do read and appreciate your answers to my questions.

 

Thanks again.

Link to comment
Share on other sites

 Share

×
×
  • Create New...