Jump to content



Photo

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

filter chain run() method filters account action

  • Please log in to reply
8 replies to this topic

#1 KeepLearning

KeepLearning

    Advanced Member

  • Members
  • PipPipPip
  • 55 posts

Posted 19 March 2013 - 8:48 AM

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.


  • 0

#2 Larry

Larry

    Administrator/Writer

  • Administrators
  • 4796 posts
  • LocationState College, PA (USA)

Posted 20 March 2013 - 7:29 AM

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.yiiframew...ntroller#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.


  • 0

#3 KeepLearning

KeepLearning

    Advanced Member

  • Members
  • PipPipPip
  • 55 posts

Posted 20 March 2013 - 9:40 AM

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?

  • 0

#4 KeepLearning

KeepLearning

    Advanced Member

  • Members
  • PipPipPip
  • 55 posts

Posted 20 March 2013 - 9:43 AM

On second thought, I don't see how the filters() function is calling the filterHttpsOnly function, so now I am just plain confused.


  • 0

#5 Larry

Larry

    Administrator/Writer

  • Administrators
  • 4796 posts
  • LocationState College, PA (USA)

Posted 20 March 2013 - 9:56 AM

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. 


  • 0

#6 KeepLearning

KeepLearning

    Advanced Member

  • Members
  • PipPipPip
  • 55 posts

Posted 20 March 2013 - 11:30 AM

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'?

  • 0

#7 Larry

Larry

    Administrator/Writer

  • Administrators
  • 4796 posts
  • LocationState College, PA (USA)

Posted 20 March 2013 - 12:48 PM

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).


  • 0

#8 KeepLearning

KeepLearning

    Advanced Member

  • Members
  • PipPipPip
  • 55 posts

Posted 20 March 2013 - 1:06 PM

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.


  • 0

#9 Larry

Larry

    Administrator/Writer

  • Administrators
  • 4796 posts
  • LocationState College, PA (USA)

Posted 20 March 2013 - 4:27 PM

I'm glad I was able to clear it up. Thanks for the nice words and for the understanding. Maintaining a forum like this is a lot of (unpaid) work, but it makes me a better writer. 


  • 0





Also tagged with one or more of these keywords: filter chain, run() method, filters, account, action