Can not run in DEV environment - php

I write you because I have a problem to run my project Symfony 4 DEV environment. Indeed, I have the impression that this one does not turn correctly because:
when I make a modification it is not visible immediately and I usually have to empty the caches to see them
the profiler and the associated "black bar" do not appear and return a 404 (I have the body tags in my base.htm.twig)
he does not see "immediately" the roads that I created
I installed the profiler via the following command composer require profiler --dev and I am in web server (directly on my NDD and my server).
I also have the variable APP_ENV in dev and it shows me well "dev" when I display it directly in a twig via {{app.environment}}
I hope that my explanations are clear enough and that you will be able to help me. thank you in advance
ps: sorry for my English, but I'm on Google Translate ;)
EDIT : I found the solution with the help of someone on Symfony slack ... I did not install the apache-pack bundle which created a .htaccess file in the public folder and manages the rewrites of url ... that's why my roads were not found.
For my defense, I did not see this bundle anywhere in the documentation

The first you have to install symfony/dotenv (dotenv)
In the .env follow this:
APP_ENV=dev
If you have not install symfony/dotenv
composer require symfony/dotenv
You can read about this here https://symfony.com/doc/current/components/dotenv.html
Also you have to follow config/packages/dev/web_profiler.yaml:
web_profiler:
toolbar: true
intercept_redirects: false
framework:
profiler: { only_exceptions: false }

It's not mandatory added APP_ENV=dev to your .env.
In Symfony 4 you can see in public/index.php this
if (!isset($_SERVER['APP_ENV'])) {
if (!class_exists(Dotenv::class)) {
throw new \RuntimeException('APP_ENV environment variable is not defined. You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file.');
}
(new Dotenv())->load(__DIR__.'/../.env');
}
$env = $_SERVER['APP_ENV'] ?? 'dev';
So if you APP_ENV is empty also you environment use is dev, but it's better use APP_ENV in .env

Related

Symfony autowiring issues since docker update

I'm having autowiring issues within symfony since I have updated my Docker to the latest version.
I am not entirely sure of the causality here but certainly, my issues started appearing only after updating my docker. Since this was the case I obviously tried to revert back to a previous docker version but still had the same issues as on the new build. That's why I am doubtful of the causality.
I'm now on: Docker version 3.5.2 with Docker Engine v20.10.7
docker-engine config is the unchanged config:
{
"registry-mirrors": [],
"insecure-registries": [],
"debug": false,
"experimental": false,
"features": {
"buildkit": true
},
"builder": {
"gc": {
"enabled": true,
"defaultKeepStorage": "20GB"
}
}
}
when I started my first project I had an issue with the composer not reading my lib-folder. This was solved as described below but it might provide some more insight, context and things I have checked/done
At first my mind went to this Windows env variable. Alas, it was set so it was not the problem.
Secondly, my mind went to file ownership or file permissions but also not the problem. Everything in my containers was owned by root and had rwx permissions.
Thirdly I thought it might be because of the new WSL2 backend I had to install. Until the day of the update I had still been running on the Hyper-V backend. Luckily there is a setting in docker where you can switch off the use of WSL-2 and revert to Hyper-V but this was also not the solution to my problem.
My composer.json looked like this:
"autoload": {
"classmap": [
"scripts/composer/ScriptHandler.php",
"lib/",
...
],
"psr-4": {
...
}
},
Everything but (some) files of lib were present in my autoload_classmap.php
After fiddling about for too long I finally tried the following and suddenly my problems on my first project were gone
"autoload": {
"classmap": [
"scripts/composer/ScriptHandler.php",
"lib/*",
...
],
"psr-4": {
...
}
},
I still don't understand why the first one should not work. It has worked until this thursday since the infamous update.
Now on to the second project and I really can't figure out what's going on and I'm at a loss for words. My colleagues can't seem to figure out what's wrong either. This second project is something more recent so not loading a classmap anymore and everything follows PSR-4 standards.
This is the autoload part of the composer.json:
"autoload": {
"psr-4": {
"App\\": "src/",
"Project\\": "scripts/"
}
},
This is my services.yaml (These are just the standard symfony settings AFAIK):
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
public: false # Allows optimizing the container by removing unused services; this also means
# fetching services directly from the container via $container->get() won't work.
# The best practice is to be explicit about your dependencies anyway.
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/*'
exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'
This is my docker PHP container:
php:
container_name: "${DOCKER_PROJECT_NAME}_php"
environment:
COLUMNS: 80
PHP_FPM_GROUP: wodby
PHP_FPM_USER: wodby
PHP_SENDMAIL_PATH: "/usr/sbin/sendmail -t -i -S mailhog:1025"
PHP_DEFAULT_CHARSET: 'utf-8'
PHP_DATE_TIMEZONE: 'UTC'
PHP_UPLOAD_MAX_FILESIZE: '10M'
PHP_POST_MAX_SIZE: '10M'
PHP_DISPLAY_ERRORS: 'On'
PHP_DISPLAY_STARTUP_ERRORS: 'On'
PHP_MAX_EXECUTION_TIME: '30000'
PHP_MAX_INPUT_TIME: '60'
PHP_MAX_INPUT_VARS: '2000'
PHP_ERROR_REPORTING: 'E_ALL'
PHP_LOG_ERRORS: 'On'
PHP_LOG_ERRORS_MAX_LEN: '0'
PHP_MEMORY_LIMIT: '512M'
PHP_SESSION_GC_MAXLIFETIME: '700000'
PHP_REALPATH_CACHE_SIZE: '4096K'
PHP_REALPATH_CACHE_TTL: '3600'
PHP_XHPROF: $PROFILING_ENABLED
env_file:
- .env
image: "wodby/php:7.4-dev-4.16.2"
volumes:
- "./:/var/www/html"
## For php profiler traces
- "files:/mnt/files"
On startup I am getting the following Runtime Exception:
Cannot autowire service "App\Form\Wizard\DaysOffType": argument "$daycareTransformer" of method "__construct()" references class "App\Form\DaycareTransformer" but no such service exists.
This class is not new, it's been in code for 2 years. It's in the right folder and the namespacing is correct. Also the src folder is included in the autoload_psr4.php.
The DaycareTransformer that "can't be found" is located in src/Form under the namespace App\Form as per psr4 namespace convention
<?php
namespace App\Form;
use App\Entity\Daycare;
use App\Service\DaycareService;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
class DaycareTransformer implements DataTransformerInterface
{
private $daycareService;
public function __construct(DaycareService $daycareService)
{
$this->daycareService = $daycareService;
}
...
}
There have not been any recent changes to this code. Not to the DaysOffType which has the DaycareTransformer injected and not to the DaycareTransformer.
I am the only person having these issues as I am the only one who runs Docker on a Windows machine. Production and QA environments are all up and running so there is no structural problem in the code.
It could be because the project running symphony in this case was locate at Windows partition (C: or D: or whatever inside your window) and not Linux distro.
If you are using WSL (most likely), you should always store Linux related project inside your distro (Ubuntu in my case, I store it it ~/...). You should not store Linux related project in /mnt/... when using WSL.
When I said "Linux related" I mean whatever project usually want to run on Linux environment, or using docker
Don't know if this could solve it for others because there could be a wide range of reason for this problem. But this is how I solve mine.
We've got the same problem on a Windows machine with Docker and Symfony 5.x.
We continued to work on this machine, but we changed our configuration.
For example, for your error I would use the following config.
Cannot autowire service "App\Form\Wizard\DaysOffType": argument "$daycareTransformer" of method "__construct()" references class "App\Form\DaycareTransformer" but no such service exists.
config/services.yml or config/services_dev.yml
services:
App\Form\DaycareTransformer: ~
We defined all the required classes from our errors and it solved our problem. But it solved problem only for one URL instead of all. You need to check each your routes or classes that use Symfony's autowire.
Unfortunately, the solution like declaration of resources didn't for us. We had to define all the required classes from our errors.
services:
App\DataTransformer\:
resource: '../src/DataTransformer/'
Don't redefined your classes that have already defined in your config, because it replaces your configuration and may make other errors.
You can do it in config/services.yml and commit it or you can use config/service_dev.yml and don't commit it if you haven't use this config before. In the second way you also can change .gitignore and add there the filename.
# Ignore changes for the file and don't track it.
config/services_dev.yml
WARNING! If you still have problems with it define all the required classes in services.yml instead of two or more config files.
We don't have this problem on Linux and MacOS machines.

Memcache is required error - symfony on appengine

Created new project with appengine-starter-symfony-project (https://github.com/GoogleCloudPlatform/appengine-symfony-starter-project) and tried to run app/server as follows:
composer run-script server --timeout=0
Error recived:
// Clearing the cache for the dev environment with debug
// false
[Symfony\Component\Cache\Exception\CacheException]
Memcached >= 2.2.0 is required
Running PHP Version 7.2.8 on Xampp (Win 10) and cant see a php_memcache in ext folder.
Indeed, this is the expected behavior. If you take a look inside of config.yml you will see the following:
services:
memcache:
class: Memcache
session.handler.memcache:
class: Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcacheSessionHandler
arguments:
- "#memcache"
- { prefix: ~, expiretime: ~ }
Basically, you cache is dependent on Memchace. You can either tweak around and switch to some other vendor or install the appropriate DLL.
Hope this helps a bit...

Symfony delete a bundle manually

I tried to delete a bundle manually, but an error appeared...
I guess I forgot an essential edition, but I don't know what.
I deleted the file of that bundle, I delete its line in the AppKernel, and I deleted it's lines in the app/routing.yml.
Here is the error :
FileLoaderLoadException in FileLoader.php line 118:
Bundle "DbToYmlBundle" does not exist or it is not enabled. Maybe you forgot to add it in the registerBundles() method of your AppKernel.php file? in #DbToYmlBundle/Resources/config/services.yml (which is being imported from "/opt/lampp/htdocs/dublin/app/config/config.yml"). Make sure the "DbToYmlBundle" bundle is correctly registered and loaded in the application kernel class. If the bundle is registered, make sure the bundle path "#DbToYmlBundle/Resources/config/services.yml" is not empty.
Could someone help to find what is wrong ?
The error message states that the #DbToYmlBundle/Resources/config/services.yml is imported from /opt/lampp/htdocs/dublin/app/config/config.yml
Please check your config.yml's or provide some more information/codes, maybe the output for your prod.log /opt/lampp/htdocs/dublin/app/logs/prod.log
You can try to clear the cache with the console.
For the Dev environment:
php bin/console cache:clear
And for production environment :
php bin/console cache:clear --env=prod

behat.yml in app/config instead of root directory

I installed behat with mink and selenium2-driver for my Symfony2 project.
Is it possible to use the /app/config/behat.yml instead of the /behat.yml file?
I searched on google but I can't find anything else this command.
php bin/behat --config app/config/behat.yml
But the command isn't working either.
I think there must be a config-path in composer.json.
Yes, you can configure which config file you want to use. Look at this part of the doc.
http://docs.behat.org/guides/7.config.html#paths
What error do you get when running your command?
php bin/behat --config app/config/behat.yml
This error?
[RuntimeException]
Context class not found.
Maybe you have provided a wrong or no `bootstrap` path in your behat.yml:
http://docs.behat.org/guides/7.config.html#paths
If that's the case, I think if might be because you need to specify where to find the features in your behat.yml file.
Now you moved the file to /app/config/behat.yml, the related path from behat.yml to the feature directory has changed, so you should add the following to the file:
default:
paths:
features: ../features/
bootstrap: ../features/bootstrap

Keep getting 'You have requested a non-existent service "test.client"' in Symfony2 Beta3

I'm trying to setup a unit test but whenever I run "phpunit -c app" I get this error:
Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "test.client".
The code in my test case is just:
public function testNonAuthenticatedPathsIndex()
{
$client = $this->createClient();
}
If I don't make the call to createClient everything runs correctly. I've checked AppKernel.php to make sure the FrameworkBundle (I believe that's where this service is defined) is definitely still in there and it is.
I'm a bit confused as to what might be causing this as I've managed to do this kind of thing before.
Thanks for any help.
Have you enabled the framework.test option in config_test.yml?
framework:
test: ~
In phpunit.xml you should have env variable like:
<env name="APP_ENV" value="test"/>
I had this problem with symfony version 3.3.13.
The config_test.yml file was fine, but I did the same mistake.
I solved adding the <server name ="APP_ENV" value="test" /> option under php in the phpunit.xml file
composer require symfony/browser-kit
The definition test.client depends upon AbstractBrowser::class presence
I had same issue with remote builds of my project. This issue was connected with empty symfony.lock file in the root of my project. This file is for preventing symfony recipes execution twice. But my symfony.lock was corrupted and empty so framework-bundle executed it's recipe and replaced my APP_ENV variable in phpunit.xml.dist from test to dev. The reason why it happened was repositories node in composer.json (https://github.com/symfony/flex/issues/347)
I'm fixing this situation by adding dependencies in symfony.lock by my hands

Categories