Hi I have a fully functioning Symfony2 application. Now as part of the SEO work I need to change the urls. I am able to change the last part of the url by changing in the routes file. But the string after domain name which is controller name, i can't change.
My current URL: http://example.com/logics/show
Updated URL should be http://example.com/logic/show
I have tried to rename the controller filename and class name. But its not worked.
Please guide me how to change the controller name throughout the application or in URL ?
The easiest way to do a redirect is changing your routing.yml file.
For SEO reasons, you must redirect your old public URLs with a 301, if change is temporal you can do 302, Symfony allow do this easiest, like this example:
redirect_logic_show:
path: /logics/show
defaults:
_controller: FrameworkBundle:Redirect:redirect
route: logic_show
permanent: true
For more info click here
Related
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.
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" }
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
My question is: How can I generate external url from routing.yml?
I want something similar to:
documentation_product1:
pattern: /my/documentation/product1.php
default: http://www.myothersite.com/product1
Is it possible or something similar?
It is possible to redirect within a controller to an external site
documentation_product1:
pattern: /my/documentation/product1.php
defaults: { _controller: AcmeBundle:Product:show }
// in controller
$this->redirect('http://www.myothersite.com/product1');
But it is not a good practise.
It is better to use the RedirectController from the FrameworkBundle.(mantioned by Paulpro)
Doku:
http://symfony.com/doc/current/cookbook/routing/redirect_in_config.html
https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/RedirectController.php
Symfony routes the requested url to a pair of controller and action to execute. It does only internal redirects.
For a redirect to an external URL you need .htaccess and mod_rewrite (if you use apache).
How can I redirect a static asset in Symfony2 (e.g: JS, CSS or PDF file) to an actual controller action?
I'm trying to redirect this request (this used to represent a static asset which doesn't exist anymore):
http://localsite.dev/bundles/mybundle/misc/attachment.pdf
to this one (local dev site, this request is handled by a controller):
http://localsite.dev/app_dev.php/downloads
or this (live site):
http://livesite.com/downloads
You should be able to just set up a route for it in your routing.yml like any other route,
eg:
my_pdf_route:
pattern: "/bundles/mybundle/misc/attachment.pdf"
defaults: { _controller: MyBundle:PdfController:index }