Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6858995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:13:44+00:00 2026-05-27T02:13:44+00:00

I have a situation in one of my controllers that should only be accessed

  • 0

I have a situation in one of my controllers that should only be accessed via AJAX, I have the following code.

if (!$request->isXmlHttpRequest()) {
    $response = new Response();
    $response->setContent('AJAX requests only!');
    return $response;
}

When I am testing this gives me an issue because the request hasn’t actually been made via AJAX. This then breaks my tests every time. How should I go about working around this?

My Ideas:

  1. I have tried to set a server header but have had absolutely no success.
  2. Check if I am in the test environment in the controller and don’t do the check if it is. I know this is dirty, but it would work. :-/ The problem was that I couldn’t figure out how to discover what environment I am in.

Anyone else have any other ideas or tips that I am missing to get one of the above to work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:13:45+00:00Added an answer on May 27, 2026 at 2:13 am

    Looking at the code for isXmlHttpRequest in class Request and method getHeaders in class ServerBag, the piece of code below should do the trick:

    $client->request(
        'GET',
        '/path/to/test',
        array(),
        array(),
        array(
            'HTTP_X-Requested-With' => 'XMLHttpRequest',
        )
    );
    

    I did not test it personally but I think it should works. The line of code below in Request is used to check if the http request is a XmlHttpRequest.

    return 'XMLHttpRequest' == $this->headers->get('X-Requested-With');
    

    In the code, $this->headers is set using:

    $this->headers = new HeaderBag($this->server->getHeaders());
    

    The method getHeaders creates an array of headers. Each server variable starting with HTTP_, plus some special server variables like CONTENT_TYPE, are put in this array.

    Hope this helps.

    Regards,
    Matt

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following situation: There is one custom view inside of the first
Here is my situation: I have one table that contains a list of drugs
Consider the following situation: We have two Localizable.string files, one in en.lproj and one
I have a situation where I have multiple views controlled via UITabBarController . One
According to Apple one should have 2 controllers and 2 nibs in order to
The current situation is that I have two separate applications. One is the one
Here is the situation: You have one long-running calculation running in a background thread.
I have a situation where different threads populate a queue (producers) and one consumer
Here's the situation I have a webpage which has one drop down called prefer.
I have a one-to-many relationship between two classes for this situation. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.