I'm having a bunch of problems with routing in Symfony2 and I'm at my wits end searching for an answer.
I'm currently trying to use the #Route annotation. In the dev environment with debug enabled, everything works. In prod with debug enabled, everything works. If I disabled debug, only the index route responds, everything else returns a 404.
I initially thought it was the cache, so I followed the usual process of clearing it. That led to my next problem:
[Doctrine\Common\Annotations\AnnotationException]
[Semantical Error] The annotation "#Sensio\Bundle\FrameworkExtraBundle\Configuration\Route" in method LeagueOfData\Controller\DefaultController::indexAction() does not exist, or could not be auto-loaded.
This error appears when you run bin/console cache:clear --env=prod or bin/console cache:clear --env=dev.
So I checked everything was set up correctly (bare in mind, this works completely fine in the browser with debug enabled no matter the environment).
routing.yml
parser:
resource: "#LeagueOfData/Controller/"
type: annotation
This is included in config.yml and routing_dev.yml (due to the dev override).
AppKernal.php
public function registerBundles()
{
$bundles = [
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new Symfony\Bundle\MonologBundle\MonologBundle(),
new Symfony\Bundle\TwigBundle\TwigBundle(),
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new LeagueOfData\LeagueOfData()
];
if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {
$bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
}
return $bundles;
}
SensioFrameworkExtraBundle is registered correctly. (Full source: https://github.com/Acaeris/lol-parser/blob/broken-routing/app/AppKernel.php)
app.php
<?php
use Symfony\Component\HttpFoundation\Request;
/** #var \Composer\Autoload\ClassLoader $loader */
$loader = require __DIR__ . '/../app/autoload.php';
include_once __DIR__ . '/../app/bootstrap.php.cache';
$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
app_dev.php
<?php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Debug\Debug;
/** #var \Composer\Autoload\ClassLoader $loader */
$loader = require __DIR__.'/../app/autoload.php';
Debug::enable();
$kernel = new AppKernel('dev', true);
$kernel->loadClassCache();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
As you can see, not much difference. The key seems to be in enabling debug... but why?
Full source is on GitHub if it'll help anyone: https://github.com/Acaeris/lol-parser/tree/broken-routing
You need to import it with an use statement, as example:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
Hope this help
Related
given is my appKernel class while registering the bundles it is sing to generate the classes
new FOS\RestBundle\FOSRestBundle(),
new JMS\SerializerBundle\JMSSerializerBundle(),
new Nelmio\CorsBundle\NelmioCorsBundle(),
but i used following commands to generate them from composer
composer require friendsofsymfony/rest-bundle
composer require jms/serializer-bundle
composer require nelmio/cors-bundle
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = [
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 Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new AppBundle\AppBundle(),
new FOS\RestBundle\FOSRestBundle(),
new JMS\SerializerBundle\JMSSerializerBundle(),
new Nelmio\CorsBundle\NelmioCorsBundle(),
];
if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {
$bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
}
return $bundles;
}
while running my php code it is giving exceptions because given bundles are not register
run the command and clear cache:
php bin/console cache:clear
also add the use classes at top AppKernel.php
There will also be some configuration for them all in a config file. Often in app/config/config.yml. Even if it will all be the default, they need to be fully enabled by that entry in the configuration (yml, or xml).
You can see the sample (and complete, with all defaults) with the command bin/console debug:config JMSSerializerBundle, replacing the bundle-name as appropriate.
There is a page of details for JMSSerializer and similar exist for the others.
After updating to the latest version of Symfony from 3.0.2 to 3.1.2 when I run the command.
php bin/console cache:clear --env=prod
I now get the following error:
[Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException]
The service "profiler" has a dependency on a non-existent service
"debug.security.access.decision_manager".
Does anyone know why this would happen, or what I could do to resolve this? I can add any additional information as needed. Thanks in advance!!!
The issue had to do with the fact I was including debugging resources in the production environment. I was performing tests on the caching mechanisms and forgot to remove the inclusion from the config.yml and AppKernel.php files.
if (in_array($this->getEnvironment(), ['dev','test','prod'], true)) {
$bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
//... Extensions From Base
$bundles[] = new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle();
}
as such the AppKernel instantiation needed the debug parameter to be set to true.
$kernel = new AppKernel('prod', true);
Otherwise it would cause the initial issue I asked this question to fix.
while putting my app in production, i had to FTP-transfer the JMS Serializer as i can't use composer on the production server.
It worked, i see it in the vendor.
My AppKernel is the same as in local (where it works, as tested in both production and dev environment) :
<?php
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 Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(),
new FOS\JsRoutingBundle\FOSJsRoutingBundle(),
new Hager\TransformationBundle\TransformationBundle(),
new JMS\SerializerBundle\JMSSerializerBundle()
);
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
$bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
$bundles[] = new Acme\DemoBundle\AcmeDemoBundle();
$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');
}
}
but i recieve a very strange
ClassNotFoundException in AppKernel.php line 22:
Attempted to load class "JMSSerializerBundle" from namespace "JMS\SerializerBundle".
Did you forget a "use" statement for another namespace?
As the console isn't available on production server either, i did rm -RF /app/cache/* but still got the error.
Help very wlecome.
Also upload the vendor/composer directory again.
and upload your web directory too since there might be new assets.
Then go to app/cache and delete everything inside.
Another approach:
download and install the CoreSphere ConsoleBundle
add your ip address in app_dev.php:
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', '100.200.300.400')) || php_sapi_name() === 'cli-server')
)
(replace 100.200.300.400 for your own ip) and upload this file to the server . Then visit your website in app_dev.php/_console and you can execute your console commands from there.
You said you cannot run Composer in your prod server (why?!). In that case, even if you copy your JMS Serializer files to the vendor dir, they will not be autoloaded by the autoloader that composer generates. You will need to edit the autoload_namespaces.php file in your vendor/composer dir and add this line:
'JMS\\SerializerBundle' => array ($vendorDir . '/jms/serializer-bundle'),
DO not rename or replace Comoser.lock andcomposer.json files.
Installnew project symfony new project and replace folders from broken project to new one : app,src,web.
I have gone through many questions related to this in SO, but couldn't find a solution to my problem.
I have copied the demo symfony application to /var/www/html/myproject folder. I can only access the localhost/myproject/web/app_dev.php but not localhost/myproject/web/app.php . Basically I want to switch from development environment to prodution environment.
I just get a blank page when I access app.php. How do I solve this issue?
Following is my routing.yml file
app:
resource: "#AppBundle/Controller/"
type: annotation
EDIT
Error log (app/logs/prod.log)
[2015-04-06 22:54:53] request.ERROR: Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "No route found for "GET /"" at /home/fazlan/ppp/myproject/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php line 144 {"exception":"[object] (Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException(code: 0): No route found for \"GET /\" at /home/fazlan/ppp/myproject/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php:144, Symfony\\Component\\Routing\\Exception\\ResourceNotFoundException(code: 0): at /home/fazlan/ppp/myproject/app/cache/prod/appProdUrlMatcher.php:35)"} []
You need to create your bundle and configure routes for it. After this prod environment will be work. It is possible that the acme isnt work in prod.
#Cedric: Acme Demo Bundle is only configured on app_dev.php you have to create another bundle first with proper routes, you can see the list of routes for your bundle in app/config/routing.yml or whatever you set as a the extension of your configs.
More info here.
After going through different tutorials and manuals, I finally was able to solve this issue by the following changes.
routing.yml
app:
resource: "#AppBundle/Controller/"
type: annotation
_acme_demo:
resource: "#AcmeDemoBundle/Resources/config/routing.yml"
AppKernel.php (The whole file is not shown here)
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 AppBundle\AppBundle(),
new Acme\DemoBundle\AcmeDemoBundle(),
);
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
$bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
//$bundles[] = new Acme\DemoBundle\AcmeDemoBundle();
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
}
return $bundles;
}
in the above file AcmeDemoBundle is registered. You have to comment out //$bundles[] = new Acme\DemoBundle\AcmeDemoBundle(); to prevent the bundle being registered twice in dev and test environments.
After editing those two files go to the project folder and do
php app/console --env=prod cache:clear
and then in the browser localhost/myproject/web/app.php would load the same bundle as in app_dev.php
I have the following new environment:
<?php
use Symfony\Component\ClassLoader\ApcClassLoader;
use Symfony\Component\HttpFoundation\Request;
$loader = require_once __DIR__.'/../app/bootstrap.php.cache';
// Use APC for autoloading to improve performance
// Change 'sf2' by the prefix you want in order to prevent key conflict with another application
/*
$loader = new ApcClassLoader('sf2', $loader);
$loader->register(true);
*/
require_once __DIR__.'/../app/AppKernel.php';
//require_once __DIR__.'/../app/AppCache.php';
$kernel = new AppKernel('mobile', false);
$kernel->loadClassCache();
//$kernel = new AppCache($kernel);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
however when I do the following:
sudo php app/console assetic:dump --env=mobile
it gives me:
Clearing the cache for the mobile environment with debug true
How is it possible that debug is set to true while I have specifically in the new AppKernel('mobile' false);
I have cleared the cache and everything but it is still the same
When I do the following:
$kernel = $this->get('kernel');
ladybug_dump($kernel->isDebug());
this returns false, however it's just the console command that is not getting it right
sudo php app/console assetic:dump --env=dev --no-debug