Symfony2: Bundle does not exist - symfony

I have this problem:
InvalidArgumentException: Bundle "DipUserBundle" does not exist or it is not enabled.
Btw I have two bundle inside Dip namespace. Both of them I made through console.
Please, is there someone who can help me.. here is my code!! Tnx
config.yml
orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: default
entity_managers:
default:
mappings:
# ...
DipBiznisBundle: ~
DipUserBundle: ~
AppKernel.php
class AppKernel extends Kernel {
public function registerBundles() {
$bundles = array(
...
new Dip\BiznisBundle\DipBiznisBundle(),
new Dip\UserBundle\UserBundle(),
);
routing.yml
homepage:
pattern: /
defaults: { _controller: DipUserBundle:Default:index }
UserBundle.php
<?php
namespace Dip\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class UserBundle extends Bundle {
}

Your problem will be solved if you rename file and BiznisBundle to DipBiznisBundle and UserBundle to DipUserBundle... to avoid problems on future generate bundles using generate command and would define namespaces using slashes instead of backslashes....

Related

Symfony 4 with translations, no route to / without _locale

I created a Symfony 4.4.8 project with translations to English and French languages, so I followed the steps:
https://symfony.com/doc/current/translation.html
and set:
config/packages/translation.yaml
framework:
default_locale: 'fr'
translator:
default_path: '%kernel.project_dir%/translations'
fallbacks: ['en', 'fr']
config/routes/annotations.yaml
controllers:
resource: ../../src/Controller/
type: annotation
prefix: /{_locale}/
requirements:
_locale: fr|en
and a src/Controller/HomeController.php with
class HomeController extends AbstractController
{
private $session;
public function __construct(SessionInterface $session)
{
$this->session = $session;
}
/**
* #Route("/", name="home")
*/
public function index() {
return $this->render('home/index.html.twig', [
'controller_name' => 'HomeController',
]);
}
I have the translation files in translations folder and when I run localhost:8000/fr or localhost:8000/en, it works fine, my home/index.html.twig is shown.
The issue is when I run localhost:8000/, it shows me the default Welcome to Symfony page with "You're seeing this page because you haven't configured any homepage URL."
Is this solution for sf2 will work on sf4?
Is anybody know how to solve it ?
I tested also to change the route in HomeController to:
#Route("/{_locale}", name="home", defaults={"_locale"="fr"}
but it returns exception:
Route pattern "/{_locale}/{_locale}" cannot reference variable name "_locale" more than once.
workaround, added:
RedirectMatch ^/$ /fr
in my virtualhost (with apache2 server)
pure symfony solution shoud be better !
I would guess a bad routing configuration. I couldn't recall a default behaviour where we can set the locale by adding the locale code in the URL so I would guess it is something your added ;)
Symfony resolves the route in the same order as you declare the route. So one solution would be to import 2 times the same route but with the prefix:
# config/route.yaml
app_front:
type: annotation
resource: '../src/Controller/*.php'
prefix: /
options:
expose: true
app_localized_front:
type: annotation
resource: '../src/Controller/*.php'
name_prefix: localized_
prefix: /{locale}
options:
expose: true
I know the previous config works, but I don't know if this is the more elegant way to do it :)

How to hide Api-plaform Docs from Nelmio Docs

I hope someone could help me to use Api-platorm with Nelmio.
I use Api-plaform and Nelmio. I need to hide the Api-platform docs from Nelmio.
I need to have 3 routes:
/internal -> API-Platform Docs
/external -> NELMIO-Docs
/admin -> NELMIO-Docs
My config of Nelmio:
# config/packages/nelmio_api_doc.yaml
nelmio_api_doc:
documentation:
info:
title: ...
description: ...
version: 0.2.0
areas: # to filter documented areas
default:
path_patterns: [ ^/external ]
external:
path_patterns: [ ^/external ]
admin:
path_patterns: [ ^/admin ]
My config of Nelmio (routes):
# config/routes/nelmio_api_doc.yaml
app.swagger:
path: /{area}/json
methods: GET
defaults: { _controller: nelmio_api_doc.controller.swagger, area: default }
app.swagger_ui:
path: /{area}
methods: GET
defaults: { _controller: nelmio_api_doc.controller.swagger_ui, area: default }
My config of API-Platform:
# config/routes/api_platform.yaml
api_platform:
resource: .
type: api_platform
prefix: /internal/
But if I go to http://localhost/external or http://localhost/admin I see always not only needed routes, but also the routes from API-Platform:
I know this question is old by now, but I am facing the same situation and I found a workaround that may help some one, so I am posting it.
API Platform lets you decorate Swagger so you can customize the final documentation output. I took advantage of this to get rid of the entire api platform documentation when not asking for it.
<?php
namespace App\Swagger;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
final class SwaggerDecorator implements NormalizerInterface
{
private $decorated;
private $requestStack;
public function __construct(NormalizerInterface $decorated, RequestStack $requestStack)
{
$this->decorated = $decorated;
$this->requestStack = $requestStack;
}
public function normalize($object, $format = null, array $context = [])
{
if ('/internal/docs' !== $this->requestStack->getCurrentRequest()->getPathInfo()) {
// request is not for internal docs (maybe it is for external or admin one) so get rid of api platform docs
return null;
}
$docs = $this->decorated->normalize($object, $format, $context);
// here you can customize documentation
return $docs;
}
public function supportsNormalization($data, $format = null)
{
return $this->decorated->supportsNormalization($data, $format);
}
}
I hope this helps someone, happy coding!
UPDATE
In order to enable that decorator, you must declare it as so in your services file:
App\Swagger\SwaggerDecorator:
decorates: 'api_platform.swagger.normalizer.api_gateway'
arguments: [ '#App\Swagger\SwaggerDecorator.inner' ]
autoconfigure: false
Then, in the class itself, replace '/internal/docs' with the actual URL you are using for your API Platform documentation.
Hope this helps.
On your nelmio configuration yaml file, use a regex to exclude the docs. For instance, for excluding the /external/doc you should:
external:
path_patterns: [ ^/external(?!/doc$) ]

Service Container Alias only for a certain environment

Symfony 2.8.7 I have a simple alias definition in my services.yml:
h4cc_alice_fixtures:
alias: h4cc_alice_fixtures.manager
This works perfectly in DEV because in my AppKernel h4cc is registered:
public function registerBundles()
{
$bundles = [
//...
];
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
$bundles[] = new h4cc\AliceFixturesBundle\h4ccAliceFixturesBundle();
}
return $bundles;
}
In PROD now I get an dependency injection error because of course h4cc_alice_fixtures.manager cannot be resolved.
I want to have h4ccAliceFixturesBundle only in DEV and TEST but currently I have only one services.yml.
Is there a way to define the alias only for DEV and TEST?
UPDATE:
As there is something like:
my_service:
class: Acme\AcmeBundle\Services\MyService
arguments: [ '#tenant_user_service', '#?debug.stopwatch' ]
which is only injecting Stopwatch when App is in debug-mode...
I thought there might be existing something similar for Alias, too.
You can have separate services.yml similar what you have already with your routing_dev.yml. In the imports section of your config_dev.yml and config_test.yml you can replace the existing line:
imports:
- { resource: config.yml }
with following entry:
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services_dev.yml }
like you have it in your global config.yml already. Just add your suffix to the files.

Can I put doctrine dbal and orm config in a vendor bundle?

I'm just asking a question, that's can we put all doctrine (orm and dbal) configuration in the config.yml file which is localized inside a vendor bundle ? And how to access to this entity manager outside of this bundle ?
Thanks per advance
EDIT1:
I think it's possible by using the PrependExtensionInterface like that :
//vendor/XXXXBundle/DependencyInjection/XXXXExtension.php
class XXXXExtension extends Extension implements PrependExtensionInterface
{
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
public function prepend(ContainerBuilder $container)
{
$configFile = __DIR__ . '/../Resources/config/config.yml';
$content = Yaml::parse(
file_get_contents($configFile)
);
$container->prependExtensionConfig('', $content);
}
}
# vendor/XXXXBundle/Resources/config/config.yml
imports:
- { resource: parameters.yml }
doctrine:
dbal:
default_connection: default_conn
connections:
default:
driver: "%XXXXBundle.database_driver%"
host: "%XXXXBundle.database_host%"
port: "%XXXXBundle.database_port%"
dbname: "%XXXXBundle.database_name%"
user: "%XXXXBundle.database_user%"
password: "%XXXXBundle.database_password%"
charset: UTF8
orm:
default_entity_manager: default_em
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
entity_managers:
default_em:
connection: default_conn
mappings:
XXXXBundle:
type: annotation
is_bundle: false
dir: %kernel.root_dir%/../vendor/awcb/Aw/Common/Model/XXXXBundle/Entity
prefix: Aw\Common\Model\XXXXBundle\Entity
alias: XXXXBundle
# vendor/YYYYBundle/Resources/config/services.yml
services:
common_business_profile:
class: Aw\Common\Business\ProfileBundle\Lib\ProfileLibrary
arguments: ["#doctrine.orm.default_em"] # already tried default_entity_manager and entity_manager
And I get the error :
The service "common_business_profile" has a dependency on a non-existent service "doctrine.orm.entity_manager".
You can note that a php app/console container:debug shows that there is no doctrine.* that's loaded...
Can you help me more please ? :)
Regards,

How to forward to a named route?

How do I do forwarding a Symfony2 (2.2) class for which I do not know the name?
Please note, I do NOT want to redirect, since I must POST and I would prefer the POST parameters not to be sent via the browser.
The path's class is not obvious as it's the internal 'login_check'.
\\routing.yml
login:
pattern: /login
defaults: { _controller : MyAppBundle:Security:login }
login_check:
pattern: /login_check
logout:
pattern: /logout
If credentials are available from elsewhere, I want to just return a response as follows:
// SecurityController.php
...
class SecurityController extends Controller{
public function loginAction(){
if($test_if_credentials_present){
$response = $this->forward('login_check',
array('_username' => $username, '_password' => $password);
return $response;
}
}
}
This fails as login_check is a route, not a class.
Is there some way to use router to find what class login_check corresponds to?
I could hard-code it also by knowing what the class name is from router:debug
php app/console router:debug login_check
Output:
[router] Route "login_check"
Name login_check
Path /login_check
Host ANY
Scheme ANY
Method ANY
Class Symfony\Component\Routing\Route
Defaults
Requirements NO CUSTOM
Options compiler_class: Symfony\Component\Routing\RouteCompiler
Path-Regex #^/login_check$#s
Thus, no class listed under Defaults.

Resources