Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get environment inside controller

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?

like image 395
Icode4food Avatar asked Nov 25 '11 16:11

Icode4food


People also ask

Where .env file is located?

The “.env” file Starting from v1.28 , the .env file is placed at the base of the project directory. Project directory can be explicitly defined with the --file option or COMPOSE_FILE environment variable. Otherwise, it is the current working directory where the docker compose command is executed ( v1.28 ).

How do I get environment variables in .NET core?

Open project properties by right clicking on the project in the solution explorer and select Properties. This will open properties page. Click on Debug tab and you will see Environment Variables as shown below. You may change the value as per your need.


1 Answers

Of course in Icode4food's case, it's better to use Matt's solution, but here is how to find the current environment:

$this->container->getParameter(‘kernel.environment’)
like image 54
dvb Avatar answered Oct 13 '22 04:10

dvb