jsantari

Adding an ajax api custom route

Recommended Posts

Trying to do some ajax calls to an api  named somedomain.com/xapi/index.php using intercoolerjs in my home page. I'm trying to make calls like this in intercoolerjs. somedomain.com/xapi/controller/method. Looked through a bunch of the posts but nothing seems to match what I am trying to do. Is there a change I can make to the htaccess file  to get this to work? Anyone have a suggestion on this or is there a better way to handle the api?

Share this post


Link to post
Share on other sites

I looked at some of the posts on that module and it's not what I'm looking for. I can handle the intercoolerjs side fine. I've done a few small projects with it using mysql & slim as the route handler. I can use a simple template in PW like api.php and call url's like somedomain.com/api/?c=controller&m=method but then intercoolerjs can't take advantage of the dependency system. Basically what I'd like is to just be able to route to the subfolder with PW getting out of the way. I can bootstrap PW at that point and do what I want.

Share this post


Link to post
Share on other sites

Do you mean that the PW htaccess is disallowing access to your PHP file (you are getting the 404 page)?

PW blocks direct access to PHP files inside /site/ but you should be able to access it inside the root, e.g.

/api.php

...or...

/xapi/index.php

 

Share this post


Link to post
Share on other sites

Can you explain a bit more why ProcessWire does get in the way of this dependency support? I mean your api could still map to paths using urlSegments.

Share this post


Link to post
Share on other sites

@Robin SIt will go to api.php when that was setup as a template for the page /api. I did figure out that PHP files don't work in /site/ and I moved it to /xapi below root. I can access it now and just figured out how to get what I wanted.

@LostKobrakaiMoving it outside of /site/ as mentioned above got it working with parameters but still didn't get me the ability to use segments. Was able to get that working by adding a rewrite rule in the PW .htaccess. Don't see any other way to get it to work so that will need to go into any updated .htaccess I use down the road.

Share this post


Link to post
Share on other sites
On 13.1.2017 at 7:23 PM, jsantari said:

 I'm trying to make calls like this in intercoolerjs. somedomain.com/xapi/controller/method.

Do you mean...  you want handle the request/url in your template (controller) and take the url segment (method) and map it to a corresponding method in a class of your template?

...like Codeigniter Framework has it? url/xxx/yyy/zzz calls method yyy in controller xxx with zzz as argument?  Or do you just don´t know how to use url segments?

 

here this guy uses this kind of url segments...

 

Share this post


Link to post
Share on other sites
On 1/13/2017 at 7:23 PM, jsantari said:

I'm trying to make calls like this in intercoolerjs. somedomain.com/xapi/controller/method

Also:

 

Share this post


Link to post
Share on other sites

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By gerald
      In my frontend I would like to implement a small AJAX-solution, but it does´nt work. For example:
      template/js/ajax.js: contains jquery-ajax-snippet with click-function like this: $.post('ajax.inc', function(e) {});
      template/ajax.inc: contains db-query with HTML-output
      template/home.php: contains div-element for the ajax-response, e.g. <div id='result'>...ajax-response...</div>
      It seems, PW does´nt allow database-requests via AJAX (jquery). I know the post from Ryan "How to work with AJAX driven content in ProcessWire". But this solution requires a completely new template-concept. I only need a simple solution for a small ajax-db-request.
      Thanks to all
    • By neophron
      Hi there,
      a few weeks ago I completed a 3.098 installation (server php 7.2). Yesterday I wanted to login but failed. I tried different browsers, meanwhile on two different PCs, but no login.
      Then I realized, that after hitting the »Login« button, there was no message (admin name – Login failed). I also tried all procedures (https://processwire-recipes.com/recipes/resetting-admin-password-via-api/). Then I checked the errors.txt (site/assets/logs) – nothing.
      The frontend works fine. Is there a way to check this Processwire installation? Could be something missing?
       
    • By Jennifer Stock
      Greetings. I would like to restrict access to certain sections of my organization's ProcessWire site using pubcookie. We are rolling out Shibboleth authentication later this year but for now, it seems I can only make use of our institution's single sign-on routine by utilizing rules in an .htaccess file. 
      I am wondering if there is a way to ask PW to apply these rules to certain pages in the site, whether via template type or location in the page tree:
      AuthType UWNetID PubcookieAppID "MyApplication" require type staff faculty  
    • By louisstephens
      So I was going to build a todo tracking app for myself to test/broaden my knowledge of processwire, and so far it has been taxing 😓.  My Site structure is:
      - Project One - Phase One - Task - Task - Task - Phase Two - Task - Task - Task So far it was pretty easy, I can easily foreach through the Project and get the phases with their tasks. However, it gets a bit muddled when I have more than one project as I was trying to have a dashboard where the content switches out to the selected project as opposed to accessing each project via their own url. How would yall handle this?
      My next hurdle is each task has a select field (for project status) that I want to update via ajax (for the smooth transitioning).
      Scenario: You finish a task, change the option from "new" to "completed", and an onclick changes the status drop down background to green (which I have working), but then posts/saves a field on the backend to the new option.
       
      I have a page called "Actions" set up with url segments using
      if ($config->ajax && $input->urlSegment1 == 'change-status') { //save update field on admin } However, I am a little fuzzy on how to actually pass the current page along with the new selected status to actually update the page (task). I guess I am not very far into my endeavor. Any help would be greatly appreciated.
    • By louisstephens
      So I have a project where multiple pages are sending POST data to 1 single template page.  All was working well (well, at least with one ajax post), but now I have hit a stumbling block. I figured  the "best" way to handle the request were to use url segments and then use the following in the status page:
      if ($config->ajax && $input->urlSegment1 == 'add-bookmark') { // some code here } However, this doesnt seem to really work (as I assume the the request isnt being posted to /status/ but rather to /status/add-bookmark/). What is the best way to actually handle this?