Jump to content

allow access to single php files in templates folder for ajax processing


ttbb
 Share

Recommended Posts

i've a (kind of) shopping cart function and a customized form processing which are both handled with ajax.
data is transported via json.
xhttp get or post requests to php files inside of the sites folder or its subfolders are blocked by the .htaccess files on the different levels of the directory architecture.
i tried to fiddle around in these htaccess files with the mod_rewrite and files_match sections, but with no avail and i besides it don't like it very much to drill holes into a security concept.
how do make this (not so exotic) procedure work in processwire. may i have overseen something here? are there any straightforward methods in processwire i've missed?

thank you all for any hints!
thomas

Link to comment
Share on other sites

18 minutes ago, 3fingers said:

See here :

Or just use the URL Path Hooks function ?

https://processwire.com/blog/posts/pw-3.0.173/#introducing-url-path-hooks

/site/init.php

<?php
$wire->addHook('/hello/world', function($event) {
  include($this->wire->config->paths->templates."test.php");
  return true;
});

Now you can use the Url "https://your.site/hello/world/" - and the content comes from /site/templates/test.php

  • Like 5
Link to comment
Share on other sites

You can also create a new template (ex. api) with just the title field. Create the template file (ex. api.php) and publish a page on your site without access restriction (ex. domain.com/api) and that's it. In your template file (which is your entry point) you can use: $input->post(), $input->get() or urlSegments to get the request data, and then you can include files, or execute functions or whatever you want. Finally you print everything in JSON format, or in html format if you want to use htmx. Piece a cake! ?

  • Like 2
Link to comment
Share on other sites

thank you guys for your helpful answers!

as i'm still a pw rookie this helps a lot!

crawling through all the articles and posts mentioned here i ended up with an approach mostly similar to what @Pixrael suggests and i'm happy to see this work like a charm ??

  • Like 1
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

  • Recently Browsing   0 members

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