Symfony2 - prod environment doesn't work (page 404) - php

I have problem with my Symfony2 project. I created new bundle using console, but default controller is avalible only from dev environment level.
For
http://localhost/myproject/web/app_dev.php/hello/ZaqU
everything works fine, but for
http://localhost/myproject/web/app.php/hello/ZaqU
i'm redirecting on
http://localhost/myproject/web/hello/ZaqU
and then i getting only page 404.
This is new bundle and i didn't changed default files so I don't understand why it doesn't work. Files:
/* #FILE: app/AppKernel.php */
$bundles = array(
//...
new ZaqU\TestBundle\ZaqUTestBundle(),
);
.
/* #FILE: app/config/routing.yml */
ZaqU_test:
resource: "#ZaqUTestBundle/Resources/config/routing.yml"
prefix: /

Have you tried to clear your prod cache? Run the following command in the console:
./app/console cache:clear --env=prod

You might have not enabled the production environment in web/app.php. Change the false into true.
$kernel = new AppKernel('prod', true);
Hope this helps.
Cheers!

Related

Can not run in DEV environment

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

symfony2.3 routing doesn't work on prod env

I'm trying to create a /contact route on my website using symfony on windows environment.
I added in the routing.yml file the routes like this:
fstn_venice_homepage:
resource: "#FstnVeniceBundle/Resources/config/routing.yml"
prefix: /
fstn_venice_contact:
pattern: /contact
defaults: { _controller: FstnVeniceBundle:Contact:send }
I have enabled the prod env :$kernel = new AppKernel('prod', true);
Trying to execute the page on prod_env: http://localhost/fstn/web/app_dev.php/contact return an 404 error but in dev_env it displays the contact page correctly.
So that I try to debug the routes by: php app/console router:debug -e=prod
and I get this:
Name Method Scheme Host Path
fstn_venice_homepage ANY ANY ANY /
fstn_venice_contact ANY ANY ANY /contact
I even try to clear the cache by using the Command line :
php .\app\console cache:clear --env=prod --no-debug and it doesn't display any errors but no luck to display contact page on prod environment.
How can I fix this problem?
--edit---
I found the source of my error, actually I should use this path to work in prod env:http://localhost/fstn/web/app.php/contact but I used before http://localhost/fstn/web/contact to test.
Saying the kernel you want a prod environement $kernel = new AppKernel('prod', true); you tell him to forbid access to the dev url http://localhost/fstn/web/app_dev.php/contact.
If you want to access to the prod url, replace app_dev.php with app.php.
You can still let the kernel env configuration to dev, and test in prod environement. After every change, don't forget to clear your cache, and chmod it ;)
Clear your cache, It will be fine.

Twig is unable create cache directory

This is the error
RuntimeException: Unable to create the cache directory (1/25/20).
1- Changing the cache directory in the appKernel file didn't fix the problem
/*
public function getCacheDir()
{
return '/mnt/symfony_ram_cache/cache/'.$this->environment;
}
*/
2- Setting the permissions to "chmod 777 -R cache" didn't work, still throws the error
3- App works when I deactivate the twig cache (I obviously need it in production)
#Twig Configuration
twig:
cache: false
4- It DOES create several folders in cache (in both prod and dev) when I empty the cache directoy and reload, so what means the rights are actually set properly
5- Here is a screenshot of the error
6- PS: Updated to symfony 2.5.6 the twig bundle was updated but still have no luck
Maybe you should chown cache directory to user who fires up application (www-data?)?
Edit your
app/config/config.yml
And change this:
From:
twig:
cache: "%kernel.debug%"
To:
twig:
cache: "%kernel.cache_dir%/twig"
I don't know what was wrong, I installed symfony2 from scratch again and now it works.

Symfony2.3 production page blank 500

after working with my project on dev mod, i found some problems with prod on OVH.
it shows me a blank page ! i try to follow the issue on app.php and i found that the problem persist with the execution of $response = $kernel->handle($request); and it don't logging on prod.
so when i change the row on app.php : $kernel = new AppKernel('prod', false); with $kernel = new AppKernel('dev', false); it works well !!!
here's my app.php
<?php
/*
* This file is part of the Sonata package.
*
* (c) Thomas Rabaix <thomas.rabaix#sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
umask(0000);
require_once __DIR__ . '/../app/bootstrap.php.cache';
require_once __DIR__ . '/../app/AppKernel.php';
//use Symfony\Component\HttpFoundation\Request;
// if you want to use the SonataPageBundle with multisite
// using different relative paths, you must change the request
// object to use the SiteRequest
use Sonata\PageBundle\Request\SiteRequest as Request;
$request = Request::createFromGlobals();
$kernel = new AppKernel('prod', false);
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
EDIT :
in dev and prod local mode test it works well with
php app/console cache:clear --env=prod --no-debug
php app/console assets:install web_directory
php app/console assetic:dump web_directory
should i add or install php5 on my project ?
EDIT :
my project contains : sonata-project, fosUserBundle,etc...
EDIT
the problem was on config_prod.xml
doctrine:
orm:
entity_managers:
default:
metadata_cache_driver: apc
query_cache_driver: apc
result_cache_driver: apc
intil now after it return an error 500 ! why ? because of apc was not enabled !
so my question how to enable apc on OVH pro !
Go to web/config.php and comment ot the following lines:
if (!in_array(#$_SERVER['REMOTE_ADDR'], array(
'127.0.0.1',
'::1',
))) {
header('HTTP/1.0 403 Forbidden');
exit('This script is only accessible from localhost.');
}
That will enable the config.php to be called from "extern". Don't forget to remove the comments after you've checked everything.
If you have console access on the production server, enter the root directory of your project and call php app/check.php to run the checks on the console.

request.ERROR: Symfony\Component\HttpKernel\Exception\NotFoundHttpException: No route found for "GET /"

I have installed Symfony2, after fixing the file permissions, I can access the dev environment from my browser by pointing it to:
http://localhost/app_dev.php
However, when I try to access the production environment by pointing the browser to http://localhost, I get the following exception (from app/logs/prod.log):
[2012-08-13 11:30:03] request.ERROR:
Symfony\Component\HttpKernel\Exception\NotFoundHttpException: No route
found for "GET /" (uncaught exc eption) at
/path/to/frameworks/Symfony2/app/cache/prod/classes.php line 4584 []
[]
I then checked the available routes for the prod environment from the command line. here is the result of that investigation.
root#yourbox:~/path/to/frameworks/Symfony2$ php app/console
router:debug -e=prod [router] Current routes Name Method Pattern
Incredibly, it shows that there are no routes defined for this environment (I didn't believe the error message - which essentially said the same thing).
So, my conclusion is this: out of the box installation of Symfony2, and the production environment has no default routes - is this true, or have I made a mistake somewhere?
More importantly, how do I fix this?. In SF1.x, it was straight forward to switch from dev to prod and vice versa. How do I view the AcmeDemo app for example, in a prod environment. ?
[[UPDATE]]
After feedback from thecatontheflat, I added a simple test route to my routing.yml file. The contents of app/config/routing.yml are now:
_welcome2:
pattern: /test
defaults: { _controller: AcmeDemoBundle:Welcome:index }
When I try http://localhost/test in the browser, I get the same 404 error. When I debug the routes available at the console, I get the following output:
root#yourbox:~/path/to/frameworks/Symfony2$ php app/console router:debug -e=prod
[router] Current routes
Name Method Pattern
_welcome2 ANY /test
I had the exact same problem!!
Symfony2 has two generic routing files called:
app/config/routing.yml and app/config/routing_dev.yml
However, it also has a bundle specific routing file in each bundle. The standard procedure is to define all of your bundle routes in your bundle routing.yml file then reference this from the main routing.yml file by adding:
YourbundlenameMainBundle:
resource: "#YourbundlenameMainBundle/Resources/config/routing.yml"
prefix: /
This is what I had and I was still getting the error. But then I read the error more closely... no route found for GET / .... then I checked my routing_dev.yml file and of course it had a route for / from the Acme demo bundle _welcome route. This is why it works for the dev version but not the prod version!
Solution:
Add a route for / to either your routing.yml global file or your routing.yml bundle file by adding the following:
_welcome:
pattern: /
defaults: { _controller: YourbundlenameMainBundle:Default:index }
you can change index to some other route if your welcome page is not index
After you make sure you have your / route defined in any of your routing.yml
_welcome:
pattern: /
defaults: { _controller: YourbundlenameMainBundle:Default:index }
clear your prod environment cache!
app/console cache:clear --env=prod
I had the exact same problem. And it was caused because of a missing bundel in the AppKernel.php.
A little late but you might try to change this line in the web/app.php:
$kernel = new AppKernel('prod', false);
to:
$kernel = new AppKernel('prod', true);
That will enable debugging and you should be able to find your problem. When fixed you will ofcorse need to change it back to false ;).
Hope this helps.

Categories