Michael Lenaghan Posted April 23, 2024 Share Posted April 23, 2024 This works: $wire->addHook('/hello', function($event) { return "Hello"; }); This doesn't: $wire->addHook('/hello.txt', function($event) { return "Hello"; }); (Note the `.txt` extension.) I see that in `WireHooks.php`, here, the extension will be removed, because "." will fail the `ctype_alnum` test. But the hook doesn't work even if I comment that block of code out, so that isn't the only issue. This is all rather curious because the original post that introduced url path hooks specifically called out `/sitemap.xml` as a possible use case. Am I doing something wrong? (Btw, curiously, when adding a `/hello.txt` hook, a `/hello.txt` URL doesn't work, but it doesn't produce a 404 either; it produces a blank page.) Link to comment Share on other sites More sharing options...
bernhard Posted April 23, 2024 Share Posted April 23, 2024 hit the wrong button - please delete this post ? Link to comment Share on other sites More sharing options...
bernhard Posted April 23, 2024 Share Posted April 23, 2024 58 minutes ago, Michael Lenaghan said: This is all rather curious because the original post that introduced url path hooks specifically called out `/sitemap.xml` as a possible use case. I'm doing exactly that in RockFrontend: https://github.com/baumrock/RockFrontend/blob/e2b0beb70427c39ade99dc36547a370dd091f59f/RockFrontend.module.php#L2338 1 Link to comment Share on other sites More sharing options...
Michael Lenaghan Posted April 23, 2024 Author Share Posted April 23, 2024 OK, thank you! It helps a lot to know that it *should* work. I'll keep trying to figure out what I'm doing wrong. 1 Link to comment Share on other sites More sharing options...
Michael Lenaghan Posted April 23, 2024 Author Share Posted April 23, 2024 OK, got it. I'm using PHP's built-in web server for development. You can often use it directly, like this: php -S localhost:8080 But occasionally you run into situations that require a router script, like this: php -S localhost:8080 router.php I generally use something like this: <?php if (PHP_SAPI !== 'cli-server') { die('Expected cli-server'); } if (file_exists($_SERVER['DOCUMENT_ROOT'] . $_SERVER['SCRIPT_NAME'])) { return false; } $_SERVER['SCRIPT_NAME'] = '/index.php'; require __DIR__ . '/index.php'; ProcessWire required one additional line: <?php if (PHP_SAPI !== 'cli-server') { die('Expected cli-server'); } if (file_exists($_SERVER['DOCUMENT_ROOT'] . $_SERVER['SCRIPT_NAME'])) { return false; } $_SERVER['SCRIPT_FILENAME'] = 'index.php'; // <== Added! $_SERVER['SCRIPT_NAME'] = '/index.php'; require __DIR__ . '/index.php'; That's because of some assumptions in ProcessWire.php. With that added line url path hooks with extensions now work. (I'm going to hunt around a bit to see if I should make any other changes...) Link to comment Share on other sites More sharing options...
Michael Lenaghan Posted April 23, 2024 Author Share Posted April 23, 2024 OK, here's my slightly revised `router.php` script: <?php if (PHP_SAPI !== 'cli-server') { die('Expected cli-server'); } if (file_exists($_SERVER['DOCUMENT_ROOT'] . $_SERVER['SCRIPT_NAME'])) { return false; } $_SERVER['SCRIPT_NAME'] = '/index.php'; $_SERVER['SCRIPT_FILENAME'] = $_SERVER['DOCUMENT_ROOT'] . $_SERVER['SCRIPT_NAME']; require $_SERVER['SCRIPT_FILENAME']; The main change is that `$_SERVER['SCRIPT_FILENAME']` is now an absolute path. Not critical, but more correct. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now