in my Symfony 5 project, I am looking to use different subdomains for my controllers depending on the environment I am in (dev, prod).
For this, I created:
config/routes/dev/annotations.yaml
and
config/routes/prod/annotations.yaml
Each file contains its own host for my controllers.
config/routes/dev/annotations.yaml:
blog_controllers:
resource: ../../../src/Controller/BlogController.php
type: annotation
host: 'blog.{domain}'
defaults:
domain: '%domain%'
requirements:
domain: '%domain%'
app_controllers:
resource: ../../../src/Controller/HomeController.php
type: annotation
host: 'app.{domain}'
defaults:
domain: '%domain%'
requirements:
domain: '%domain%|www.%domain%'
config/routes/prod/annotations.yaml:
blog_controllers:
resource: ../../../src/Controller/BlogController.php
type: annotation
host: 'prod.blog.{domain}'
defaults:
domain: '%domain%'
requirements:
domain: '%domain%'
The %domain% is the APP_DOMAIN in my .env, no problem with that.
So, when I'm on prod environment, when I'm trying to access https://prod.blog.[myDomain] (the homepage on my blog project part), I'm redirected on the homepage of the HomeController. I don't know why...
My controllers :
BlogController.php:
class BlogController extends AbstractController
{
/**
* #Route("/", name="blog")
*/
public function index()
{
return $this->render('blog/index.html.twig');
}
}
HomeController.php:
class HomeController extends AbstractController
{
/**
* #Route("/", name="home")
*/
public function index()
{
return $this->render('home/index.html.twig');
}
}
edit:
I tested with this configuration :
Just in prod, set this route in the /config/prod/annotations.yaml
blog_controllers:
resource: ../../../src/Controller/BlogController.php
type: annotation
host: 'blog.{domain}'
defaults:
domain: '%domain%'
requirements:
domain: '%domain%'
No host for dev environment.
So, on prod environment, if I make a debug:router, I've :
[![enter image description here][1]][1]
So both routes here are correct.
On the one hand, the "home" route without a subdomain, accessible by http s://myDomain.com
and on the other, the "blog" route accessible by http s://blog.myDomain.com
Are we OK?
But when I launch http://blog.myDomain.com/, It's the Home controller which is displayed !
EDIT 2 : Okay I found the solution.
Just add host for "home" route, like this :
app_controllers:
resource: ../../../src/Controller/HomeController.php
type: annotation
host: '{domain}'
defaults:
domain: '%domain%'
requirements:
domain: '%domain%|www.%domain%'
No subdomain. It works.
Thanks all !
[1]: https://i.stack.imgur.com/GPJlN.png
Related
My initial problem is this error:
Too few arguments to function
FOS\UserBundle\Controller\ResettingController::__construct(), 0 passed
in
/var/www/project/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Controller/ControllerResolver.php
on line 200 and exactly 6 expected
that happens when i open the link in the automatic Mail of FosUserBundle FOSMailer::sendResettingEmailMessage
// routing.yml
app:
resource: "#AppBundle/Controller"
type: annotation
prefix:
/{_locale}/
requirements:
_locale: fr|en|es
app_api:
resource: "#AppBundle/Controller/Api"
type: annotation
fos_js_routing:
resource: "#FOSJsRoutingBundle/Resources/config/routing/routing.xml"
fos_user_security_login:
path: /connexion
methods: [ GET, POST ]
defaults: { _controller: FOSUserBundle:Security:login }
fos_user_security_check:
path: /login_check
methods: [ POST ]
defaults: { _controller: FOSUserBundle:Security:check }
fos_user_security_logout:
path: /logout
methods: [ GET, POST ]
defaults: { _controller: FOSUserBundle:Security:logout }
fos_user_resetting_reset:
path: /resetting/reset/{token}
methods: ['GET', 'POST']
defaults: { _controller: FOSUserBundle:Resetting:reset }
I cleared the cache, and i added the 6 parameters with dependency injection:
//services.yml
services:
fos_user.resetting.reset:
class: FOS\UserBundle\Controller\ResettingController
arguments:
- "#event_dispatcher"
- "#fos_user.resetting.form.factory"
- "#fos_user.user_manager"
- "#fos_user.util.token_generator"
- "#fos_user.mailer"
- "%fos_user.resetting.retry_ttl%"
That worked well in dev environnement (in local and on a server), but i still got the same error "too few arguments..." in production.
So cleared the cache again : bin/console c:c --env=prod
removed the cache folder: rm -Rf var/cache/prod
But i still got the error :/
Does anyone have an idea ?
Thank you #Cerad for your help.
I finally found the solution to my problem :
i just realized that the version of fosUserBundle wasn't the same on the 2 servers ...
so i just changed my composer.json with that:
"friendsofsymfony/user-bundle": "2.0.*"
And this works fine!
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.
I got a hard time learning Symfony 2.8.
I have created a bundle called BlogBudle and inside this I created a controller called HomeController.
My goal is:
Create a /test URL and assign it to home page url
Below is my code:
/var/www/symfony/app/config/routing.yml
blog:
resource: "#BlogBundle/Controller/"
type: annotation
prefix: /
app:
resource: "#AppBundle/Controller/"
type: annotation
/var/www/symfony/src/BlogBundle/Controller/HomeController.php
namespace BlogBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class HomeController extends Controller
{
/**
* #Route("/test")
*/
public function indexAction()
{
return $this->render('BlogBundle:Default:index.html.twig');
}
}
When I run php app/console debug:router, it shows
Name Method Scheme Host Path
blog_home_index ANY ANY ANY /test
Problem:
If I visit http://www.example.com/test, it's showing 404 error.
Also below is my VH Configuration:
<VirtualHost *:80>
#ServerAdmin admin#test.com
ServerName mysmfony.com
ServerAlias www.mysymfony.com
DocumentRoot /var/www/symfony/web
DirectoryIndex app_dev.php
<Directory /var/www/symfony/web>
AllowOverride All
Options All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/mysymfony.com.log
CustomLog ${APACHE_LOG_DIR}/mysymfony.com.log combined
</VirtualHost>
Need to overwrite your bundle route in app/config/routing.yml such as :
user_test:
resource: "#BlogBundle/Resources/config/routing.yml"
prefix: /
and
Here is route for redirect to your Home page
BlogBundle/Resources/config/routing.yml
any_unique_route_name:
pattern: /test
defaults: { _controller: BlogBundle:Home:index }
Make sure this is helpful to you.
Here is an example :
routing.yml
my_route_name:
path: /test
defaults:
_controller: NamespaceBlogBundle:Home:index
Also if your controller is called HomeController render should be like this
public function indexAction()
{
return $this->render('NamespaceBlogBundle:Home:index.html.twig');
}
Symfony routing doc
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.
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