Second Route doesn't work in symfony2 - php

I originally thought that this was an issue with my routes as one route works and the other doesn't. However, upon changing my route around it is now the otherway round. The route code is below:
_website_site:
resource: "#SiteWebsiteBundle/Resources/config/routing.yml"
prefix: /
_mobile_site:
resource: "#SiteMobileBundle/Resources/config/routing.yml"
prefix: /m
This code is situated at the bottom of my route_dev.yml in my app folder. Using the code this way the /m/ prefix works but if i reverse them then the / works and not the /m/.
is it possible to have more then 1 route in the app\routing_dev.yml file? If not then how would this normally work?
Cheers

try adding
prefix: m_
To your routes from mobile routing.yml (after the defaults thing )

Related

Symfony2 routes redirecting to wrong controller

In my symfony2 application I want specific routes for my pages, to work well with my seo, but I receive some serious problems and I dont understand them..
EXAMPLE:
Two routes:
blog_article:
path: /blog/{slug}
defaults: {_controller: ApplicationEDBlogBundle:Blog:singleArticle}
product:
path: /{category}/{name}
defaults: { _controller: MpShopBundle:Product:view}
The product route works fine, but the blog_article route always redirects to product route..
In my understanding if i open blog: /blog/firstBlog/ by default it thinks that the blog is a category and firstBlog is the product name, because my product route is the last route.
But if in my twig i specificaly tell which route to go to, shouldnt it work?
For example: {{ path('blog_article', {slug: blog.slug}) }}. Shouldnt this look at the blog_article route and open the needed controller? Or it does not work like that?
If so, how to keep my pretty urls the way I want to?
change routing to
blog_article:
path: /blog/{slug}
defaults: {_controller: ApplicationEDBlogBundle:Blog:singleArticle}
product:
path: /cat/{category}/{name}
defaults: { _controller: MpShopBundle:Product:view}
and will be fine .
In your example {category} could be "blog" , so 1st route was matched .
It can also work if you change order ( product w'll be first). But it's not good solution (what if someone add category blog ?)
Nope, it doesn't work like that, i.e. your example path code doesn't mean that the routing should look for the blog_article route:
The twig path function just expands the route into the actual url (/blog/yourslug) and when actually accessing that url, the system makes the matching the other way around from the url to the route (matching to whichever happens to be the first of the above listed two route definitions).
If you have this kind of routes, the solution is to have them neatly in the correct order (most generic ones - the product in this case - being always the last), or if the ordering is not possible, you can try to solve this by placing some specific route requirements if applicable.

Generate appropriate urls in multiple sub-domain hostname in Symfony2

I am developing a multi sub-domain web application in Symfony 2.
Subdomains:
admin.example.com is the main hosting site.
member.example.com is another sub-domain pointing to same source code.
As mentioned in the documentation here, I configured routing as below :
Parent Routing for member.example.com:
my_app_member:
resource: "#member.yml"
prefix: /
host: "member.example.com"
Note : The above routing is the parent route config for all routes for member.example.com defined in member.yml.
**Now I have anoter route for admin.example.com : **
admin_user_mod:
path: /admin/new
defaults: { _controller: "somecontroller" }
However if I have to generate a full url for the route admin_user_mod using code :
$modLink = $this->get("router")->generate('admin_user_mod');
The generated route path is correct but the base url still stays as member.example.com which should be admin.example.com
Is there an way, or I am missing anything in above route configuration to get the desired results.
OR
Is there any symfony event listener to overwrite router's "generate()" method call?
All your inputs are highly appreciable.
Thank you
Router has getContext() method you can use:
$context = $this->getContainer()->get('router')->getContext();
$context->setHost('admin.example.com');
As it says in the Routing Documentation, passing true as the third parameter for the generate method will generate an absolute URL.
So in your case it would be...
$modLink = $this->get('router')->generate('admin_user_mod', [], true);
Just make sure you specify the host in the route configuration when doing this. So for example your route should be...
admin_user_mod:
path: /admin/new
host: admin.example.com
defaults: { _controller: "somecontroller" }

How to pass a variable to routing defaults in Symfony?

I have a Symfony2 project with multiple brandings (subdomains). Each subdomain should use the same site functions and the same routes (with different hostname). Therefor I want to make the subdomain in the route path to a variable. Now it looks like:
mybundle_route:
resource: "#MyProjectBundle/Controller/"
type: annotation
prefix: /
host: "{subdomain}.%base_host%"
requirements:
subdomain: sub1|sub2
and the routing itself works. But if I try to generate some routes (e.g. in twig), I get following error because no subdomain variable is set:
Some mandatory parameters are missing ("subdomain") to generate a URL for route "company_route".
I don't want to add the parameter to each path() function in the project.
It works perfekt for one subdomain if I add the defaults like this:
defaults:
subdomain: sub1
but the routes are wrong for the other subdomains. Also it doesn't work with
defaults:
subdomain: "%subdomain%"
All I need is to pass the %subdomain% variable to the defaults or to set it somewhere in the controller constructor. But I can't find the way how it is going. I would appreciate any help.
If there is no better solution you could try to set %subdomain% parameter inside configuration file:
Create subdomain_parameter.php inside config folder.
Inside this file you have access to ther $container so you can manually retrieve current subdomain from the url and pass it to the container.
Import subdomain_parameter.php in the config.yml

Symfony translating routes using BeSimple/BeSimpleI18nRoutingBundle duplicate routes

I'm creating a multi language website and I'm using BeSimple/BeSimpleI18nRoutingBundle for route translations. Route in my config looks like that:
about:
locales: { en: "/{_locale}/about-us", lt: "/{_locale}/apie-mus" }
defaults: { _controller: BaseBundle:Base:about }
However in this case I get duplicate route as if I enter /en/about-us or /lt/about-us it works in both ways. but in the second way it should be 404 error because the route for lt locale should be /lt/apie-mus which also works fine.
Is there any way of fixing that? Or maybe any better ways of translating routes?
However in this case I get duplicate route
There is one route per language:
$ app/console router:debug
about.en ANY ANY ANY /{_locale}/about-us
about.lt ANY ANY ANY /{_locale}/apie-mus
When you are on the URL /lt/about-us the UrlMatcher will match the en version:
$ app/console router:match /lt/about-us
Route "about.en" matches
[router] Route "about.en"
Name about.en
Path /{_locale}/about-us
Path Regex #^/(?P<_locale>[^/]++)/about\-us$#s
Host ANY
Host Regex
Scheme ANY
Method ANY
Class Symfony\Component\Routing\Route
Defaults _controller: AppBundle:Default:index
_locale: en
Requirements NO CUSTOM
Options compiler_class: Symfony\Component\Routing\RouteCompiler
In this case, the local will be en and not lt.
But I don't think that you should worry about it because the only way that a visitor will visite /lt/about-us is by editing the URL himself.
After battle with BeSimple I switched my project to JMSI18nRoutingBundle in 10 minutes. It works well and resolves problem with routes. Firstly: it containes multiple modes of prefixation (with locale) and secondly it allows to translate every route.

Silex add route with higher priority than existing routes

My Silex routes are defined in a routing.yml config file.
In my php code I would like to add some new routes dynamically and I want these routes to have a higher priority than the routes defined in routing.yml.
Currently I'm adding my routes as in the following example, but they are added to the bottom, i.e. with the lowest priority.
$this->app->match('/page/{slug}', array($this, 'record'))
->bind('extrapages')
->method('GET|POST');
This route for example never gets matched because there is a route in routing.yml that matches the following path:
path: /{contenttypeslug}/{slug}
How do add my new routes above the existing routes?
As an aside I'm using the Bolt CMS, which is built on Silex, and trying to add these new routes in a bolt extension. As this question is about Silex routing the fact that I'm using Bolt shouldn't make a great deal of difference.
It's not a very clean solution but whenever I've overruled an extension's route in my routing.yml, I just add the extension's route to routing.yml again, pointing it to my extension code. If you put it near the top, it will get used, because in Silex routes are parsed 'top down'. First match gets used.
sitemap:
path: /sitemap
defaults:
_controller: 'Bolt\Extension\Bolt\Sitemap\Extension::sitemap'
I apologize for the hackyness of this solution.

Categories