5

My hybrid application is based on AngularJS and uses a php REST api.

I would like to debug the php api directly from my Angular app instead to use REST console or Postman. It would save a lot of time especially for POST and PUT requests.

In order to do so I would need to add a parameter to each request like so:

http://localhost:8000/api/contacts?XDEBUG_SESSION_START=PHPSTORM

Can I config $http to do so?

1 Answer 1

12

You can use httpInterceptor for that (official $http documentation contains more info)

// register the interceptor as a service
$provide.factory('xdebugInterceptor', function($q) {
  return {
    // optional method
    'request': function(config) {
      // do something on success

      // !!! adjust the config object
      // add request param XDEBUG_SESSION_START=PHPSTORM
      // it will be added to every made request

      config.params = config.params || {};
      config.params.XDEBUG_SESSION_START: "PHPSTORM";

      return config;
    },

    // optional method
   'requestError': function(rejection) {
      // do something on error
      return $q.reject(rejection);
    },


    // optional method
    'response': function(response) {
      // do something on success
      return response;
    },

    // optional method
   'responseError': function(rejection) {
      // do something on error
      return $q.reject(rejection);
    }
  };
});

// make this conditional so you use it only in DEV mode
$httpProvider.interceptors.push('xdebugInterceptor');
Sign up to request clarification or add additional context in comments.

10 Comments

Ok for the structure of it. When you write // add request param XDEBUG_SESSION_START=PHPSTORMhow do you actually do that? That's unclear in the doc how you can access th eurl or params. Thanks
Ok great @tomastrajan !
Ok, I added the actual code to the example, works for me, it's added for every request
Also you can use interceptors for central unwrapping of responses (eg: return response.data), and central error handling (like show some notifications) in 'responseError'
Good suggestion. Done!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.