From php files in Symfony 4, how can I print variables (for debug) to the log profiler page ?
I mean this page :
I tried that :
$log = new Logger()
$log->info("test");
$log2 = new ConsoleLogger(OutputInterface::OUTPUT_NORMAL);
$log2->alert("hello !");
But nothing appears...
You may not have installed the monolog bundle. Without that you get a very rudimentary logging.
Run:
composer require symfony/monolog-bundle
Reload app and see if message start appearing in the log tab of the profiler.
I just follow the monolog symfony guide : http://symfony.com/doc/current/logging.html
After logger install, I add somewhere in a controller a "$logger->info("foo");" and I see the trace from the log profiler page
I am using a PaaS provider (cloudControl) for my Symfony app. Works fine in general.
But funny thing: In the app/logs directory, there is only a dev.log. Can someone explain what is wrong here?
(I surely didn't call the app_dev.php, which is by the way not possible on reomte systems - if app_dev.php isn't configured elsewise.)
Look into your web/app.php, it should be:
//...
$kernel = new AppKernel('prod', false);
//...
You probably have new AppKernel('dev', false);
I'm trying to move a Symfony 2 based project to production mode on my rented CentOS VPS, however I seem to encounter an issue when visiting a certain route address which shows the following error every time.
Method "MyNamespace\MyProjectBundle\Controller\MyController::indexAction" does not exist.
500 Internal Server Error - InvalidArgumentException
If I run rm -rf app/cache/* it will load fine, then show the same error when I refresh again. It appears to work fine in development mode. From what I can see all of the permissions are fine and have verified all of the routes are working using php app/console router:debug.
Can anyone else suggest what could be causing this issue?
Check your /app/AppKernel.php
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
$bundles[] = new Acme\ProjectBundle\ProjectDemoBundle();
I was following the tutorials for symfony, and have hit a road block.
my setup is the current version of xampp (running on windows), so my apache and php are relatively up to date.
following their "quick tour" here:
http://symfony.com/doc/current/quick_tour/the_big_picture.html
everything worked great in the development environment. following the guides a little further however, i started creating my own test bundle via the guide here:
http://symfony.com/doc/current/book/page_creation.html
and can't get it to work properly on the production environment. (it works fine in the dev environment, just like the pre-installed demos did.
i have tried clearing the cache via the php app/console cache:clear --env=prod --no-debug command, however that did not help (and also seems to be the only suggestion that pops up upon searching.
when viewing the routes, i can see that my "/hello/{name}" route is showing up fine on the list of routes.
my app/config/routing.yml has:
acme_hello:
resource: "#AcmeHelloBundle/Resources/config/routing.yml"
prefix: /
as it should, and then my src/Acme/HelloBundle/Resources/config/routing.yml has
hello:
path: /hello/{name}
defaults: { _controller: AcmeHelloBundle:Hello:index }
does anyone have any suggestions as to what the problem might be? (i have also tried converting the out of the box demo to a production route, by copying the route info from the routing_dev.yml file and reassigning the bundle in the appkernel.php file, but that had the same problem)
---edit---
per request, here is my appkernel.php file
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
new Symfony\Bundle\TwigBundle\TwigBundle(),
new Symfony\Bundle\MonologBundle\MonologBundle(),
new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
new Symfony\Bundle\AsseticBundle\AsseticBundle(),
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new JMS\AopBundle\JMSAopBundle(),
new JMS\DiExtraBundle\JMSDiExtraBundle($this),
new JMS\SecurityExtraBundle\JMSSecurityExtraBundle(),
new Acme\HelloBundle\AcmeHelloBundle(),
new Acme\DemoBundle\AcmeDemoBundle(),
);
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
}
return $bundles;
}
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
}
}
---2nd edit---
i found the problem. i thought it was strange that nothing was showing up in the prod log, so i thought maybe something was redirecting me, causing me to miss the app.php file entirely.
it turns out, this was the problem. i emptied the contents of the .htaccess file that was in the web folder (the one that symfony came preconfigured with) and then everything magically started working.
I also had the same problem. I share how I solved it (Strange but it works).
In file web/app.php change:
$kernel = new AppKernel('prod', false);
by
$kernel = new AppKernel('prod', true); // Enable debug mode
Then load the page in the browser (in app environment) and change the file again disabling debug:
$kernel = new AppKernel('prod', false);
Try loading the page in the browser again. It should work properly.
Best regards,
If you are like me, just want to get the sample application from the tutorial to run in Apache, then this may help you.
I ran into the same problem with version 2.5 just now, and finally found the answer here:
http://symfony.com/doc/current/quick_tour/the_big_picture.html
The demo routes in our application are only available in the dev environment.
Therefore, if you try to access the .../app.php/... URL, you'll get a 404 error.
The easiest way to get it to work is to use app_dev.php instead of app.php.
Just edit .htaccess and change all occurrences of app.php to app_dev.php, like this:
Line 6:
DirectoryIndex app_dev.php
Line 41:
RewriteRule .? %{ENV:BASE}/app_dev.php [L]
Did you try to warm up your cache for the prod environment? You can do this by running the following command:
php app/console cache:warmup --env=prod --no-debug
Also you could check your routes by running
php app/console router:debug
If you want to get specific information on a route you can do php app/console router:debug yourRouteName. But maybe thats what you meant with "viewing the routes".
Another hint has to do with Assets.
Usually if you move your app from dev to prod environment you have to run the following commands:
php app/console assets:install web_directory
php app/console assetic:dump web_directory
Assetic:dump physically genereates your assets, like css stylesheets or javascript files. Something to read about this in detail is here
Perhaps you could edit your post and post your AppKernel.php? Maybe the bundle isn't activated there?
i found the problem. i thought it was strange that nothing was showing up in the prod log, so i thought maybe something was redirecting me, causing me to miss the app.php file entirely.
it turns out, this was the problem. i emptied the contents of the .htaccess file that was in the web folder (the one that symfony came preconfigured with) and then everything magically started working.
For me this problem was caused by cache. After clearing the cache, the new route worked fine on prod environmment (app.php), no 404's.
I ran into the same problem. I fixed the issue by first clearing the cache from the command line using the following command:
php app/console cache:clear --env=prod
After this, I removed the cache folder itself from the app directory (as I couldn't still access my bundle but only the default Symfony homepage) and then refreshed the browser and the error was gone. Hope this helps.
I've been trying to remove the E_NOTICE and E_STRICT error levels to avoid this error:
Runtime Notice: Only variables should be passed by reference
I tried modifying php.ini but didn't work, error_reporting always is -1. Then I tried setting it dynamically in the current action, and worked fine. Then I also tried the same in the first line in app_dev.php and didn't work. Which means Symfony2 is setting it dynamically somewhere.
What should I do?
EDIT
For those who are not familiar with the error:
$user = $this->('security.context')->getToken()->getUser();
$first = reset($user->getRoles()); // error
$roles = $user->getRoles();
$first = reset($roles); // works fine
Whilst the notice is not 'retarded', this is a reasonable question in other contexts so: this is set in the Kernel instance, instantiated in app_dev.php (or app.php).
The second parameter to the construct is a boolean debug flag, and if true then error_reporting is set to -1 and display_errors to 1, otherwise default and 0 respectively.
$kernel = new AppKernel('dev', false);
symfony documentation
Hope this helps.
I got the same error in the following scenario i.e.
Scenario:
I lost the development environment for my existing LIVE project. But I got the whole code from GIT repo and then installed the symfony 2.4.2 (same version as on LIVE site) in my new development environment. Then I found that the web-application pages working on the LIVE site are broken in my new DEV environment.
Solution:
I spent quit a lot time to understand why the problem is then I found that i.e.
When I installed symfony 2.4.2 in my development environment using composer.phar then it created a new web/app_dev.php file in my development environment and it has the following entry to turn it off i.e.
Debug::enable();
Just comment the above line then the php notices will be off and then all the pages that were giving me notices are fixed.
I hope this will be helpful for someone having the same problem like me. Good Luck!
Cheers.
PS: But I will recommend to enable the above line in your new development projects so that you can see the PHP notices and then remove them during development.