I am having an issue with my Routing.
When I try and visit domain.com/listing/add I get the error below
Parameter "id" for route "listing_edit" must match "[^/]++" ("" given) to generate a corresponding URL.
I understand with the edit route it will require domain.com/listing/1/edit but I thought having the listing/add route above the edit route I should still be able to visit domain.com/listing/add.
What am I doing wrong?
route.yml
listing_add:
pattern: listing/add
defaults: { _controller: Bundle:Listing:add }
listing_edit:
pattern: listing/{id}/edit
defaults: { _controller: Bundle:Listing:edit}
If you are using twig, you must use these kind of links:
YOUR LINK TO ADD
YOUR LINK TO EDIT
Related
I'm trying something with the Symfony router but I cannot achieve what I'm aiming to.
Indeed I want to add an optional text to this path, especially for the /page-{page}. So I'm asking if it's possible to have two possibilities for the same router path, like the following ones:
/topic/{id}
/topic/{id}/page-{page}
routing.yml:
utm_forum_forum:
path: /forum/{id}/page-{page}
defaults:
_controller: UTMForumBundle:Forum:forum
page: 1
requirements:
id: \d+
page: \d+
Thanks for reading, and if I'm not clear enough I can re-explain :)
You need to include routes for all possible configurations.
So, if your page slug is optional, your route table should look something like:
utm_forum_forum_default:
path: /forum/{id}
defaults:
_controller: UTMForumBundle:Forum:forum
page: 1
requirements:
id: \d+
utm_forum_forum:
path: /forum/{id}/page-{page}
defaults:
_controller: UTMForumBundle:Forum:forum
requirements:
id: \d+
page: \d+
That should provide a default value of page 1 when the page slug is omitted entirely, or require something in the form page-N after the ID.
utm_forum_forum:
path: /forum/{id}/page-{page}
defaults: { _controller: UTMForumBundle:Forum:forum, page: null }
so if you request for /forum/15/page- will give you null in {page} parameter.
Don't know if its what you need
I'm trying to define the routes of my site with {_locale} as a prefix so there will be routes like:
mysite.com/about-us
mysite.com/es/about-us
mysite.com/en/about-us
My first problem is that defining {_locale} as prefix makes it mandatory and the route mysite.com/about-us won't work. Currently defined this way:
#Acme/WebBundle/Resources/config/routing.yml
app_about_us:
path: /{_locale}/about-us
defaults: { _controller: AcmeWebBundle:Static:aboutUs, _locale: es}
requirements:
_locale: es|en
#app/config/routing.yml
Acme_web:
resource: "#AcmeWebBundle/Resources/config/routing.yml"
prefix: /
Also, I don't want my routes to display the {_locale}, so that if anyone types:
mysite.com/es/about-us it changes to *mysite.com
Finally if there is any way to set the prefix globally for all the site instead of putting it on every route would be awesome.
OK, first of all you can set default locale for a whole application
# app/config/config.yml
framework:
default_locale: en
Second - doing it this way, you can declare two routes:
app_about_us:
path: /{_locale}/about-us
defaults: { _controller: AcmeWebBundle:Static:aboutUs}
requirements:
_locale: es|en
app_about_us_locale:
path: /about-us
defaults: { _controller: AcmeWebBundle:Static:aboutUs, _locale: es}
Im not sure is it possible to do it different way without additional bundles, events etc...
Other way
You can set session locale - here is example to make locale sticky.
This way you can create normal routing, without params. So how set new session locale? Create special routing to setting new locale and redirect to your original page.
app_set_locale:
path: /{_locale}/
defaults: { _controller: AcmeWebBundle:Locale:setLocale}
inside controller set locale and redirect to previous page
Read Symfony documentation about Routing, Locale, Translate - maybe you will found something interesting :)
How can I point multiple URLs to one controller? I've tried this:
pattern: /
defaults: { _controller: myTestController:Intro:index }
pattern: /intro
defaults: { _controller: myTestController:Intro:index }
But the first rule seems to be ignored and only the second one is being read.
Each route must be defined separately. And don't use the same identifier, otherwise you'll override the first route definition.
myFirstRoute:
pattern: /
defaults: { _controller: airpaprFramesWebsiteBundle:Intro:index }
myDuplicateRoute:
pattern: /intro
defaults: { _controller: airpaprFramesWebsiteBundle:Intro:index }
This may also help > symfony2 use multiple url pattern for a single Controller Action using regular expression
Next time, try to use app/console router:debugcommand line to check your routes definition and figure out what's going wrong.
I've been googleing this question but I can't find anyone with my same problem... And I don't think I'm the only one here >.<
Let's see, I'm using translations in symfony2. I NEED to use twig for this...
The thing is that I need 3 links so people can change the site's language. The link has to redirect to the same page the user is, but changing the '_locale'.
I first thought in something like this:
// in routing.yml
bundleStuff_someUrl:
pattern: /{_locale}/aloha
defaults: { _controller: bundleStuff:Aloha:foo }
bundleStuff_fooUrl:
pattern: /{_locale}/foo/{fooParam}
defaults: { _controller: bundleStuff:Foo:foo }
// in view.html.twig
lang1
lang2
lang3
The problem becomes when (in this case) the _route is fooUrl... Is there a way to append every attribute I have in the current view to the path I'm looking for?
In other words referring to this example: is there a way so twig knows it has to add the 'fooParam' to the path if the current view is 'fooUrl'?
Thank's in advance!
Hope this post is useful! :D
_route_params request attribute holds the parameters of the current route. So the twig code would be,
{% set route = app.request.get('_route') %}
{% set route_params = app.request.get('_route_params') %}
lang1
lang2
lang3
For symfony 2.0 yo can get _locale variable in the controller and after send in a variable.
For example
Controller:
$language = $this->getRequest()->get('_locale');
$this->$this->redirect($this->generateUrl('bundleStuff_someUrl', array('language' => $language)))
and after in routing.yml
bundleStuff_someUrl:
pattern: /{language}/aloha
defaults: { _controller: bundleStuff:Aloha:foo }
I am looking to setup some routes in Symfony2, but I am struggling to setup a dynamic route for folders.
I am trying to setup a route that accepts the following: /department/sub-department/sub-sub-department/product-url.html
From that route all I need is the product-url and the rest is more for SEO. The problem I have is that a route may have many department levels in the URL, so I need to ignore everything before the product-url.
It seems like the "/" is the problem here, so is there a way to escape the slashes.
If I don't use any of the departments in the routing I can use this:
product:
pattern: /{url}.html
defaults: { _controller: CompanyBundle:System:pageRequest }
So, I basically need something like this:
product:
pattern: /{department}/{url}.html
defaults: { _controller: CompanyBundle:System:pageRequest }
Where the {department} can be one or more departments with forward slashes in.
Is that possible at all?
There's a nice article about it in the cookbook:
You must explicitly allow / to be part of your parameter by specifying a more permissive regex pattern.
In your case the route definition would have to be
product:
pattern: /{department}/{url}.html
defaults: { _controller: CompanyBundle:System:pageRequest }
requirements:
department: ".+"
product:
pattern: /{url}.html
defaults: { _controller: CompanyBundle:System:pageRequest, department: ~ }
product_department:
pattern: /{department}/{url}.html
defaults: { _controller: CompanyBundle:System:pageRequest }
requirements:
department: '[\w\d\/\-]+'