Jump to content

AJAX Paths within templates


JayGee
 Share

Recommended Posts

I'm trying to make an AJAX call from within a template to a php script within my templates folder, but I'm getting a 404 from all URLs. Is there a proper way to directly address scripts within PW templates? I've read it will work in the site root, but I'd rather keep all the code together if possible.

Link to comment
Share on other sites

Ok cool thanks - had read that thread and was secretly hoping for 'hidden option c' that I hadn't yet come across that would allow me to keep the file in templates without relating it to a page! :P

Link to comment
Share on other sites

NOTE 2022: Better use URL hooks! https://processwire.com/blog/posts/pw-3.0.173/#introducing-url-path-hooks

Hidden option ?

// site/ready.php
$wire->addHookBefore('Page::render', function($event) {
  $page = $event->object;
  if($page->template == ...) include(yourcodefile.php); // or any other condition
});

// yourcodefile.php
<?php namespace ProcessWire;
if(!$this->config->ajax) return; // early exit
// any other conditions

// return json
header('Content-Type: application/json');
die(json_encode($yourdata));

@Guy Incognito you might also be interested in this thread:

 

 

  • Like 4
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...