Jump to content

Url path hooks with extensions don't seem to work? [Solved]


Recommended Posts

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

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

  • Like 1
Link to comment
Share on other sites

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

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.

  • Like 1
Link to comment
Share on other sites

  • Michael Lenaghan changed the title to Url path hooks with extensions don't seem to work? [Solved]

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...