Jump to content

Hooks, Which One To choose?


Mustafa-Online
 Share

Recommended Posts

Hi Everyone, I have built until now two websites using ProcessWire & it's really powerful- the only thing that seems blurry to me is Hooks

not as a concept, but i don't know which one to choose and when ??

For example, i tried to change to login redirect URL :

So why choosing "___loginSuccess" over "___afterLogin" ?

There is a lack of tutorials related to this topic / and docs too :'(

Link to comment
Share on other sites

Haven't used those hooks, yet.
Probably ___loginSucces() - but probably you could do a $session->redirect() as a somewhat dirty hack at ___afterLogin(), too.

I'd suggest to just try it out. If you run into a more specific problem and you have some code to look at, we could help more I guess ;)

 

  • Like 1
Link to comment
Share on other sites

6 hours ago, Mustafa Online said:

but i don't know which one to choose and when

To solve this you need to look at the code for each method you are considering hooking and ask yourself things like:

  • What class is the method in?
  • Does the method fire when I need it to?
  • Does the method have an argument or return value that I need to use in my hook?

So you are considering hooking ProcessLogin::afterLogin() or Session::loginSuccess(). When you look at afterLogin() you see:

  1. It is a method of ProcessLogin, a Process module that handles the PW login form. So it is only going to fire if a user logs in via the core PW login form. Maybe that isn't what you want if you are using a custom login form or logging in users via the API as part of some script. It depends on what you are doing.
  2. The method comments in the source code say it is only intended for when a superuser logs in, which could give you a clue if it is the best method to hook or not.
  3. It has no arguments that could be useful to quickly tell you things about the user who has logged in (although you could still get the $user object in other ways).

So chances are Session::loginSuccess() is going to be a better option because it is a method of Session, so more closely connected to the current user session regardless of how they logged in. And it conveniently has the $user object as an argument so you can easily check properties of the user such as name, role, etc, in your hook.

  • Like 5
Link to comment
Share on other sites

6 hours ago, Mustafa Online said:

Hi Everyone, I have built until now two websites using ProcessWire & it's really powerful- the only thing that seems blurry to me is Hooks

not as a concept, but i don't know which one to choose and when ??

For example, i tried to change to login redirect URL :

So why choosing "___loginSuccess" over "___afterLogin" ?

There is a lack of tutorials related to this topic / and docs too :'(

A few references to Hooks:

https://processwire.com/api/hooks/

https://somatonic.github.io/Captain-Hook/index.html

https://github.com/adrianbj/TracyDebugger/tree/master/panels/CaptainHook

http://www.flamingruby.com/blog/using-hooks-to-alter-default-behavior-of-processwire/

https://webdesign.tutsplus.com/tutorials/a-beginners-introduction-to-writing-modules-in-processwire--cms-26862

  • Like 6
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...