Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use string object name for hooks #2012

Open
trk opened this issue Dec 20, 2024 · 4 comments
Open

Allow to use string object name for hooks #2012

trk opened this issue Dec 20, 2024 · 4 comments

Comments

@trk
Copy link

trk commented Dec 20, 2024

Allow to use string class name as object when using hooks

use Foo\Bar\Controller;

// This is not working
$wire->addHook('/hello', Controller::class, 'indexHello');

// This one is working
$wire->addHook('/hello', new Controller, 'indexHello');
<?php

namespace Foo\Bar;

class Controller
{

public function indexHello(HookEvent $event)
{
     header('Content-Type: application/json');
     json_encode(['message' => 'String class name not working :(']);
     exit;
}

}
@ryancramerdesign
Copy link
Member

ryancramerdesign commented Dec 20, 2024

@trk An instance of a class can have methods called, but a class name on its own can't, so isn't going to be useful here, unless maybe referring to a static method. ProcessWire knows nothing about your custom class or how it needs to be constructed, initialized, dependencies, etc. So what I'd suggest instead is this:

$wire->addHook('/hello', function($e) {
  (new Controller())->indexHello();
}); 

Or encapsulate it all in your class. Extend Wire and attach your hooks within the class:

class Controller extends Wire {
  public function __construct() {
    $this->addHook('/hello', $this, 'indexHello');
  }
  public function indexHello($e) { ... }
}
new Controller();

Lastly, do you even need a class at all?

$wire->addHook('/hello', function($e) {
  header('Content-Type: application/json');
  return json_encode(['message' => 'hello world']);
}); 

@trk
Copy link
Author

trk commented Dec 20, 2024

@ryancramerdesign

Above example usage is only for example to show you the simplest usage

$wire->addHook('/user/register', User::class, 'register');
$wire->addHook('/user/login', User::class, 'login');
$wire->addHook('/user/logout', User::class, 'logout');
$wire->addHook('/user/reset-password', User::class, 'resetPassWord');
$wire->addHook('/product/{id}/similar', ProductPage::class, 'similar');
$wire->addHook('/product/{id}/slider', ProductPage::class, 'slider');

Checking $toObject variable at
https://github.com/processwire/processwire/blob/3cc76cc886a49313b4bfb9a1a904bd88d11b7cb7/wire/core/WireHooks.php#L1091 line can help

if (is_string($toObject) && class_exists($toObject)) {
     $toObject = new $toObject;
}

The main purpose of this usage is don't call class until the hook triggered.

if you don't want to extend this object usage, I will use your first suggestion

$wire->addHook('/hello', function($e) {
  (new Controller())->indexHello();
}); 

@matjazpotocnik
Copy link
Collaborator

@trk are you ok with Ryan's answer? If yes, can we close this?

@trk
Copy link
Author

trk commented Jan 5, 2025

@matjazpotocnik I think we can use Class names as string for hooks, waiting for @ryancramerdesign answer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants