symfony custom locale prefix configuration - php

I'm looking for a way to customize the locale prefixes configuration for different clients.
I have defined this as shown in the Symfony documentation
# config/routes/annotations.yaml
controllers:
resource: '../../src/Controller/'
type: annotation
prefix:
en: '' # don't prefix URLs for English, the default locale
nl: '/nl'
Is it possible to override these settings in a custom config file ? for example if a site would need fr as the default locale, what would be the best way to go ?
Thank you

you must change in services.yaml
parameters:
locale: 'en'
# This parameter defines the codes of the locales (languages) enabled in the application
app_locales: en|fr|de
and in annotations.yaml
controllers:
resource: '../src/Controller/'
type: annotation
prefix: /{_locale}
requirements:
_locale: '%app_locales%'
defaults:
_locale: '%locale%'
see more in : symfony demo
This component provides access to the localization

Related

Symfony 4: How load config depending on host

Now for routing using config specified in annotations
controllers:
resource: ../../src/Controller/Frontend
type: annotation
prefix:
en: ''
fr: '/fr'
de: '/de'
For all domains language without prefix now is english. Now to set language without prefix depending on host?
For example for a domain in Germany I want the config to be like this:
controllers:
resource: ../../src/Controller/Frontend
type: annotation
prefix:
de: ''
fr: '/fr'
en: '/en'

Unrecognized option "resource" under "fos_user"

I am integrating FOSUserBundle in my application and I am trying to run php bin/console doctrine:migration:diff after following the recommended set-up.
I am systematically getting a:
In ArrayNode.php line 319:
Unrecognized option "resource" under "fos_user"
Yet, I have set the following in my config/packages/routing.yaml:
framework:
router:
strict_requirements: ~
fos_user:
resource: "#FOSUserBundle/Resources/config/routing/all.xml"
What am I doing wrong? I have "friendsofsymfony/user-bundle": "~2.0" in my composer.json and ran composer update.
Update
My fos_user.yaml is:
fos_user:
db_driver: orm
firewall_name: main
user_class: App\Entity\Admin\User
from_email:
address: no_reply#xxx.com
sender_name: xxx
Dlondero's suggestion fixed the issue, the fos_user configuration should be set in config/routes.yaml.
From documentation, routes are never automatically added in configuration.
move the :
fos_user:
resource: "#FOSUserBundle/Resources/config/routing/all.xml"
into config/routes.yaml insted of confing/packages/routing.yml
If I am not wrong, config/packages/routing.yaml file is not for routes, it is the config file for routing bundle, you can not put your routing configs to there. Instead, you should put them under config/routes folder, as bundle_name.yml.
Like this, eg. config/routes/imagine.yml:
_liip_imagine:
resource: "#LiipImagineBundle/Resources/config/routing.yaml"
In my case with Symfony 4.3.2 skeleton
fos_user:
resource: "#FOSUserBundle/Resources/config/routing/all.xml"
should be pasted in
config/routes/annotations.yaml
and
php bin/console debug:router
displays all the fos_user routes

Symfony 4: Reusable code in "src"

I'm trying to update a Symfony 3.4 application to Symfony 4. On my current applications I always share a local AdminBundle folder inside src. I know Symfony 4 recommends to be "bundle-less" now. But this AdminBundle is the base for most of my projects, and sometimes I make some updates to it that can be deployed to all my projects just pushing to the repository.
I tried to move by AdminBundle inside src but obviously that's not working. Could anyone detail the recipe or configuration needed to make this Bundle work under Symfony 4 in a generic way?
If this is not possible what's the best way to create a reusable code in symfony 4?
I'm currently using a similiar approach:
I've got a "CoreBundle" for shared Services, Entites, Subscribers etc under "src".
In order to make this usable i had to edit the following:
config/maker.yaml -> to use make:entity and create under CoreBundle
maker:
root_namespace: 'App\CoreBundle'
config/packages/doctrine.yaml modify -> to get the Entities from CoreBundle
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/CoreBundle/Entity'
prefix: 'App\CoreBundle\Entity'
alias: App
config/services.yaml add -> to import all SymfonyLike Classes
###> CoreBundle ###
App\CoreBundle\:
resource: '../src/CoreBundle/*'
exclude: '../src/CoreBundle/{DependencyInjection,Entity,Model,Migrations,Tests}'
App\CoreBundle\Controller\:
resource: '../src/CoreBundle/Controller'
tags: ['controller.service_arguments']
###< CoreBundle ###
config/* add -> where i wanted to import a separate config file
imports:
- { resource: '../../src/CoreBundle/Resources/config/*.yaml' }

Use wildcards in Symfony routing configuration for subdomain and tld

I tried to setup the Symfony routing. I'm using Symfony 2.7. I have two bundles that work under a different subdomain. The domainname can be an wild card and the tld can be multiple.
Here is my current config file:
company_api:
resource: "#ApiBundle/Controller/"
type: annotation
prefix: /
company_core:
resource: "#CoreBundle/Controller/"
type: annotation
prefix: /
I want that the first bundle only works under the subdomain "api", the domain can be an wildcard and for the TLD I want to specify a few like (nl|eu).
edited
company_core:
resource: "#ApiBundle/Controller/"
type: annotation
prefix: /
host: www.mydomainname.{tld}
defaults:
tld: nl
requirements:
tld: "nl|eu"
I have nog upgarde the config to this setup. And the tld works correct. Only would it be possible to have an wildcard for the domain name "mydomainname"? This is easy as the dev and producten server use different doamain names.
Here is an example of what I use on a 3.1 app, where I use a placeholder for the top-level domain that varies by environment —
app:
resource: "#AppBundle/Controller/"
type: annotation
host: www.example.{tld}
defaults:
tld: "%tld%"
requirements:
tld: "%tld%"
I don’t see why the following would not work for your API routes, as this should all be compatible with 2.7 according to the docs:
company_api:
resource: "#ApiBundle/Controller/"
type: annotation
host: api.{domain}.{tld}
defaults:
domain: yourdefaultdomain
tld: nl
requirements:
tld: "nl|eu"
I do remember that during development the routing config seemed to be aggressively cached, so as always, be sure to clear the cache after making any routing changes.

How to start Symfony through app.php?

http://localhost/Symfony/web/app.php/demo/hello/Alex
shows
"Cannot import resource "#AcmeDemoBundle/Controller/SecuredController.php" from "D:/UserData/WWWRoot/Symfony/app/config/routing.yml". Make sure the "AcmeDemoBundle/Controller/SecuredController.php" bundle is correctly registered and loaded in the application kernel class."
This is my routes of app\config\routing.yml
Internal routing configuration to handle ESI
_internal:
resource: "#FrameworkBundle/Resources/config/routing/internal.xml"
prefix: /_internal
_welcome:
pattern: /
defaults: { _controller: AcmeDemoBundle:Welcome:index }
_demo_secured:
resource: "#AcmeDemoBundle/Controller/SecuredController.php"
type: annotation
_demo:
resource: "#AcmeDemoBundle/Controller/DemoController.php"
type: annotation
prefix: /demo
AcmeDemoBundle is activated in AppKernel for dev and test environments only:
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
$bundles[] = new Acme\DemoBundle\AcmeDemoBundle();
}
If you want to access it via app.php — which is for the prod envifonment — you need to move the bundle activation out of the condition.
AFAIK, adding prod in the getEnvironment() options array is OK, assuming you know what bundles dont go to prod.
Have a separate condition to check if its dev or test for other bundles that don't go to prod

Categories