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.
Related
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'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.
I'm using FOS User bundle to allow users to register and log in to my app. Until now it works just fine but i don't know how to get my hand on those views? Can i generate them somehow? Documentation doesnt seem to know anything about it, just about overriding templates, which is not what i need...
At first i thought routing file might be of some use, since it points to specific places in the project, but it lists folders I dont even have in my application
fos_user_security:
resource: "#FOSUserBundle/Resources/config/routing/security.xml"
fos_user_profile:
resource: "#FOSUserBundle/Resources/config/routing/profile.xml"
prefix: /profile
fos_user_register:
resource: "#FOSUserBundle/Resources/config/routing/registration.xml"
prefix: /register
fos_user_resetting:
resource: "#FOSUserBundle/Resources/config/routing/resetting.xml"
prefix: /resetting
fos_user_change_password:
resource: "#FOSUserBundle/Resources/config/routing/change_password.xml"
prefix: /profile
There is no FOSUserBundle directory at all...
You can find third party bundles installed in the vendor directory.
In this case, #FOSUserBundle should be a pointer to vendor/friendsofsymfony/user-bundle/FOS/UserBundle/.
If you don't have that directory, try running this on your project root: composer update.
Overriding templates is well documented here
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.