Symfony 4: How load config depending on host - php

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'

Related

symfony custom locale prefix configuration

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

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.

Symfony 2.6.1 app only running in PROD enviroment

I'm developping a Symfony 2 application (version 2.6.1) but I can only run it on PROD mode. When trying to access to the following URL :
http://localhost/lawAdmin/web/app_dev.php
I'm having the next error message:
RouteNotFoundException in appDevUrlGenerator.php line 32: Unable to
generate a URL for the named route "_profiler" as such route does not
exist.
Both my routing.yml and routing_dev.yml files look the same:
law_admin:
resource: "#LawAdminBundle/Controller/DefaultController.php"
type: annotation
prefix: /
Running
app/console debug:router
returns:
[router] Current routes
Name Method Scheme Host Path
porfolio_show GET ANY ANY /
gallery_show GET ANY ANY /gallery/{galleryId}
I've also tried the following command just in case unsuccessfully:
php app/console cache:clear
Change your routing dev to
_wdt:
resource: "#WebProfilerBundle/Resources/config/routing/wdt.xml"
prefix: /_wdt
_profiler:
resource: "#WebProfilerBundle/Resources/config/routing/profiler.xml"
prefix: /_profiler
_main:
resource: routing.yml
This will load profiler and toolbar routes on dev environment, and load your own application routes from routing.yml file.

FileLoaderImportCircularReferenceException when adding subdomain routing to Symfony

I am trying to route a subdomain to a specific bundle in Symfony2. Here's what I've got:
I added local domains to my hosts:
127.0.0.1 todolist.lc
127.0.0.1 manager.todolist.lc
I created a virtual host that forwards all subdomains to my Symfony installation:
<VirtualHost 127.0.0.1>
ServerName todolist.lc
ServerAlias *.todolist.lc
DirectoryIndex app_dev.php
DocumentRoot "C:\xampp\htdocs\todolist\web"
</VirtualHost>
I created a new Bundle to handle the subdomain manager.todolist.lc:
Now I am trying to set up the route to manager.todolist.lc:
frontend:
resource: "#FrontendBundle/Controller/"
type: annotation
prefix: /
backend:
resource: "#BackendBundle/Controller/"
type: annotation
prefix: /api
manager:
host: manager.todolist.lc
resource: "#ManagerBundle/Controller/"
type: annotation
prefix: /
Now after I added the manager route I get a FileLoaderImportCircularReferenceException on every route there is.
I also tried to use a prefix, but this also gives the Exception:
manager:
resource: "#ManagerBundle/Controller/"
type: annotation
prefix: /manager
I can't figure out what I am missing. What am I doing wrong? If you need any more info, just ask for it in the comments and I'll provide it.
Ok. Here's what I was missing:
1. I forgot to load the bundle into the AppKernel
Obviously, this is very important:
new FrontendBundle\FrontendBundle(),
new BackendBundle\BackendBundle(),
new ManagerBundle\ManagerBundle(),
2. The subdomain needs to be declared before the main domain
After I loaded the bundle into the AppKernel the application would still route to the FrontController. I solved this by changing the order of my routes:
manager:
host: manager.todolist.lc
resource: "#ManagerBundle/Controller/"
type: annotation
prefix: /
frontend:
resource: "#FrontendBundle/Controller/"
type: annotation
prefix: /
backend:
resource: "#BackendBundle/Controller/"
type: annotation
prefix: /api
After changing the order of the routes both manager.todolist.lc and todolist.lc worked.

Symfony2 No route found for GET ofiler or GET nfigurator

I have just installed Symfony2 and created my bundle. I removed the Acme demo bundle and tested my new route "/" which worked fine.
I then went to config.php and followed the link to _configurator and get the above route error. I also get the same error when trying to load _profiler.
Here is my route file:
asf_core_homepage:
pattern: /
defaults: { _controller: ASFCoreBundle:Home:index }
And the Symfony routing_dev file:
_wdt:
resource: "#WebProfilerBundle/Resources/config/routing/wdt.xml"
prefix: /_wdt
_profiler:
resource: "#WebProfilerBundle/Resources/config/routing/profiler.xml"
prefix: /_profiler
_configurator:
resource: "#SensioDistributionBundle/Resources/config/routing/webconfigurator.xml"
prefix: /_configurator
_main:
resource: routing.yml
Any ideas why this is happening?
EDIT
I have narrowed it down and it looks like it has nothing to do with symfony. I have it installed in a subfolder of root so I use htaccess to rewrite all requests from /forum to /forum/web
RewriteRule forum/(.*) /forum/web/$1 [L]
If I point the browser to /forum/app_dev.php it works fine. the error comes from /forum/app_dev.php/_configurator
But if I skip the rewrite rule and use /forum/web/app_dev.php/_configurator it works fine.

Categories