Adding attributes to the path request with twig in symfony2 - symfony

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 }

Related

Symfony 2: Way to store app configuration for templates and controller

I am new to symfony and looking for a way store (and read) some informations, which I want to use in controller and templates.
Basically I want to access this sample structure:
project:
name: "My cool Project"
cdn: "http://www.example.com"
paths:
"images": "/images",
"pdf": "/pdf"
...
I have already tried to add this to my parameters.yml. But is it the correct place and how to access it in template AND controller?
In controller, I can do:
$this->getParameter("project")
Is there a way to directly access project.name? Something like:
$this->getParameter("project.name")
How to access it in template?
Just pass the parameter from the controller to the view:
In the controller class:
return [
'variable' => $this->getContainer()->getParameter('variable');
];
In the twig template, to print it:
{{ variable }}
If you want to pass a parameter to the templates without passing it in every controller, use the twig.globals configuration:
twig:
globals:
variable: %variable%
Then print it the same way as above.
$this->getParameter('project')['name'];
EDIT:
For the view, have a look to global Variables in Twig:
http://symfony.com/doc/current/cookbook/templating/global_variables.html

Symfony: Routing issue add/ {id}/edit

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

Custom views (or routes) in FOSRestController

I am using FOSRest Bundle to implement Rest API's.
I have the function getTermsAction() which returns all the terms from the DB.
I have a template listTerms.html.twig which shows the list of terms.
I want to implement another function listTermsAction() which will render this template and return it.
Something like this (I added this to my controller)
/**
* #Route("/listTerms",name="listTerms")
*/
public function listTermsAction()
{
$view = $this->view(null,200)
->setTemplate("TermsBundle:Default:listTerms.html.twig");
return $this->handleView($view);
}
I tried to this by adding the following code in routing.yml file
list_terms:
pattern: /terms/listTerms
defaults: {_controller:TermsBundle:Terms:listTermsAction}
But it doesn't work!
It just shows me "null" in the browser
The Solution i have given works but that snippet should be above the rest route in my routing.yml
That is routing.yml should be like
list_term:
pattern: /terms/listTerms
defaults: {_controller:TermsBundle:Terms:listTerms}
terms:
resource: Madhuri\TermsBundle\Controller\TermsController
type: rest
prefix: /
list_term route should be above terms route

Symfony2: Exception - Unable to find the controller for path

I added a new action to my controller, created the twig file and added the corresponding route to the routing.yml file. However I can't make it work. I keep getting:
Unable to find the controller for path /route/1/change
What am I missing?
# app/config/routing.yml
engineering_change:
pattern: /engineering/{id}/change
defaults: { controller: MgmtBundle:Engineering:change }
I generate the url in my template like this:
{{ path('engineering_change', { 'id': entities.id }) }}
It should read _controller instead of controller in your routing.yml.
-> Routing in Action

Get routing requirement variable in Symfony2

in order to create a navigation for me webinterface I'd like to get a variable from the routing config of my bundle. I define the available pages in mybundle/Resources/config/routing.yml.
mybundle_homepage:
pattern: /{_locale}/{branch}/{page}
defaults: { _controller: mybundle:mycontroller:index, _locale: de, branch: x.x.x, page: start }
requirements:
_locale: de|en
page: start|functions|events|constants|styleguide
Now I had a look at the Symfony2 YAML Parser and I have to provide a filepath to it's static method parse: http://symfony.com/doc/2.0/reference/YAML.html
mycontroller.php
use Symfony\Component\Yaml\Yaml;
class mycontroller extends Controller
{
public function indexAction($_locale, $branch, $page)
{
$routing = Yaml::parse('../Resources/config/routing.yml');
var_dump($routing);
}
}
I thought I could do it that way because the folder hirarchy looks like that:
mybundle
Controller
mycontroller.php
Rescources
config
routing.yml
But it's not working. Any ideas or maybe another way to get the requirements.page array from the routing file?
Regards, Ben
You should be able to access the router service inside a class that's DI container aware. So, you can write something like:
$routes = $this->container->get('router')->getRouteCollection();
$route = $routes->get('my_route_name');
print_r($route->getRequirements());

Resources