I have a symfony2 project running with nginx and the problem is that when accessing the prod environment, all the links are still with app_dev.php there.
Here are my config files:
app.php
<?php
require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';
use Symfony\Component\HttpFoundation\Request;
$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
$kernel->handle(Request::createFromGlobals())->send();
The links in the twig file are called this way:
Route name: user_login, in twig: user_login_path()
Where could the problem come from? Nginx?
You should always use path('route_name') in order to generate links or actions. It seems that the function(s) you are using (kind of twig extension) is bad coded and server dependent. But it does not comes with Symfony itself.
You have multiple options :
The cleanest : replace every href, action, ... with the path function
A (maybe) faster one : find the custom twig extension and make it work (more likely in src/Acme/MyBundle/Twig/MyExtension.php)
A quick and dirty one : rewrite every app_dev.php urls to app.dev with a .htaccess (not really recommended at all but... well...)
Related
I work with Symfony 2.8 and testing with the built-in php web server.
My Problem is, if I want to open my Webpage I have to use the url:
localhost:8000/app_dev.php
After I build in the translator, the URL must be:
localhost:8000/app_dev.php/en
And in my Ajax.js files the link to the ajax.php have also be like:
/app_dev.php/<route>
I don't want to write the complete path in the browser and in my files to link.
I like to get directly on my startpage with http://localhost:8000 without typing /app_dev.php/en/login etc...
If you want this behavior just change app.php to use the dev environment. So you don't have to type /app_dev.php and use http://localhost:8000
//$kernel = new AppKernel('prod', false);
$kernel = new AppKernel('dev', true);
I have a twig extension whose purpose is to collect a list of CSS and JS file paths given to it by function calls throughout a template hierarchy and then at the end of the twig template to take the output buffer and include these files in the <head> section of the page. For the most part it has been straightforward to implement.
In my service definition for the twig extension I am injecting the assetic.helper.dynamic service into it. The problem is when I call the javascripts() or stylesheets() method to get a URL for a CSS or JS file I get an error like this:
An exception has been thrown during the rendering of a template ("None
of the chained routers were able to generate route: Route
'_assetic_bd311c7' not found")
service.yml:
admin.twig.asset_extension:
class: Zing\Delta\AdminBundle\Twig\AssetExtension
tags:
- { name: twig.extension }
arguments: ['#assetic.helper.dynamic']
In my extension I am essentially doing this to get the URL for an asset:
$assetic_helper->stylesheets(array(
'#SomeBundle/Resources/public/js/jquery.tablesort.min.js'
));
I don't understand why the router can't find the routes or why assetic is setting up the routes.
The fix ended up being to run the following commands in the following order from the project root.
$ php app/console assets:install
$ php app/console assetic:dump
$ php app/console cache:clear
I just downloaded a fresh copy of Symfony 2.4.1 on a Debian 7.3.
The URL /Symfony/web/config.php opens correctly, but when I proceed to Configure, I get:
The requested URL /Symfony/web/app_dev.php/_configurator/ was not found on this server.
If I Bypass configuration and open /Symfony/web/app_dev.php
the page displays, but an error pops up saying:
An error occurred while loading the web debug toolbar (404: Not Found)
Do you want to open the profiler?
If I accept, I get:
The requested URL /Symfony/web/app_dev.php/_profiler/54a8bf was not found on this server.
In other questions (e.g. Symfony 2: 404 Not Found Error when tryes to open /app_dev.php) people suggest to fiddle with the .htaccess file.
I even tried removing it, but it had no effect.
The apache log file shows:
File does not exist: /var/www/Symfony/web/app_dev.php/_wdt/54a8bf, referer: http://wwwtest.di.unipi.it/Symfony/web/app_dev.php
File does not exist: /var/www/Symfony/web/app_dev.php/_profiler/54a8bf
So it looks like routing to any inner URL fails, despite the fact that the correct routing seems to be in place:
> app/console router:debug
[router] Current routes
Name Method Scheme Host Path
_wdt ANY ANY ANY /_wdt/{token}
_profiler_home ANY ANY ANY /_profiler/
_configurator_home ANY ANY ANY /_configurator/
...
I have changed memory_limit in /etc/php5/fpm/php.ini to 512Mb and debug toolbar started to work.
Found solution for me here
The solution for me was to update my app_dev.php file to resemble the one in the Symfony2 repo
Notice the Debug class being used. This wasn't used in older versions of symfony. I updated this file and the javascript alert appears to be gone.
<?php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Debug\Debug;
// If you don't want to setup permissions the proper way, just uncomment the following PHP line
// read http://symfony.com/doc/current/book/installation.html#configuration-and-setup for more information
//umask(0000);
// This check prevents access to debug front controllers that are deployed by accident to production servers.
// Feel free to remove this, extend it, or make something more sophisticated.
/*
if (isset($_SERVER['HTTP_CLIENT_IP'])
|| isset($_SERVER['HTTP_X_FORWARDED_FOR'])
|| !in_array(#$_SERVER['REMOTE_ADDR'], array('127.0.0.1', 'fe80::1', '::1'))
) {
header('HTTP/1.0 403 Forbidden');
exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
}
*/
$loader = require_once __DIR__.'/../app/bootstrap.php.cache';
Debug::enable();
require_once __DIR__.'/../app/AppKernel.php';
$kernel = new AppKernel('dev', true);
$kernel->loadClassCache();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
Is the following present in web/app_dev.php? The line below fixed it for me.
$kernel->terminate($request, $response);
Apache directive DirectoryIndex app.php does the tricks for me.
I put it in apache vhost conf with AllowOverride None to prevent override by .htacces.
Hope this help.
I'm working with a project symfony version 2.3.6 specifically. The problem is that I make changes in the code but the change is not reflected on the page until the page charge many times. I'm in dev environment. is as if not compile the code every time I load the page. Are not you supposed to be in dev environment should always be compiled? a greeting
Thanks four your answer. I use MAMP and php 5.5.3 and no select cache.
the app_dev is:
<?php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Debug\Debug;
// If you don't want to setup permissions the proper way, just uncomment the following PHP line
// read http://symfony.com/doc/current/book/installation.html#configuration-and-setup for more information
//umask(0000);
// This check prevents access to debug front controllers that are deployed by accident to production servers.
// Feel free to remove this, extend it, or make something more sophisticated.
if (isset($_SERVER['HTTP_CLIENT_IP'])
|| isset($_SERVER['HTTP_X_FORWARDED_FOR'])
|| !in_array(#$_SERVER['REMOTE_ADDR'], array('127.0.0.1', 'fe80::1', '::1'))
) {
header('HTTP/1.0 403 Forbidden');
exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
}
$loader = require_once __DIR__.'/../app/bootstrap.php.cache';
Debug::enable();
require_once __DIR__.'/../app/AppKernel.php';
$kernel = new AppKernel('dev', true);
//$kernel->loadClassCache();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
i comment the line //$kernel->loadClassCache(); but the problem no solve.
Symfony caches everything by default, especially Twig. And it's tedious, I know.
Looking at the manual (http://symfony.com/doc/current/book/templating.html) it says:
If you add a template in a new location, you may need to clear your
cache (php app/console cache:clear), even if you are in debug mode.
And so, you need to clear the cache. But attention deleting all, because the user sessions are stored in /cache/dev/sessions. That's another strong limitation: Symfony2 requires a writable filesystem.
There is a radical solution, but your app could get really slow, also in dev mode:
twig:
cache: false
In your config.yml
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.