How to have twig path() with silex slug? - php

In silex I have something like
$controllers->get('/{id}', 'Controllers\\Login::index')->bind('login');
when in twig I try to get path('login') I get exception
("Some mandatory parameters are missing ("id") to generate a URL for route "login"."). ?
I know this is because of {id} and I have to pass a second parameter to path() but how should it look like?

In order to pass parameters to twig path, use the following syntax:
{{ path('login', {'id': 'your-id-here'}) }}
you can have a look at the documentation here:
Path() function documentation, symfony
You can pass several parameters as explained here:
SO: several paremeters in twig

Related

Issues while trying to access url parameters with Twig [duplicate]

I have a url: http://myserver.com/mypage?vid=1234
I want to output 1234 on to the page. I have tried this but it is not working:
{{ app.request.get('vid') }}
what am I doing wrong or missing?
You can do this by using the "query" object :
{{ app.request.query.get('vid') }}
But you should get this value in the controller and then pass it to twig.
EDIT : This answer is based on the asumption that you are using Symfony Framework

How to pass url parameters to views with Route::view method in Laravel?

This is my code in Laravel :`
Route::view('/welcome/{name}','welcome',['name'=>'name']);
I want to know is it possible get and pass {name} parameter to view by Route::view or not?
I know that we can do it with Route::get :
Route::get('/welcome/{name}', function($name){
return view('welcome',['name'=>$name]);
});
Instead of trying to pass parameter use the segment. Try this in your view:
{{ Request::segment(2) }}

Pass two parameters as one to TWIG url

I'm using a framework (specifically EC-Cube3, if interested) that uses a Silex application with TWIG templates.
It has a navigation sidebar that is configured with PHP object arrays. I can inject an item array that looks something like this:
$item = array(
"id" => "item_id"
"url" => "named_url"
);
The url is created in typical Silex fashion:
$app->match('/item', 'Controller::method')->bind('named_url');
The framework eventually uses the url field and evaluates it in TWIG like so:
{{ url(item.url) }}
And this is usually fine! But the problem comes when I have a URL that includes a parameter, like this:
$app->match('/item/{id}', 'Controller::method')->bind('named_url'); // Needs 'id' to be evaluated.
In this scenario, setting the URL by the same array won't work because TWIG's url function needs the id for the second argument.
Since I can't change the application's TWIG files (because then I can't safely update the application), I need to be able to set the item's url to include the id as well.
After investigating, I don't think this is possible. Is it true that this is impossible? Or is there some method that I'm unaware of?
You can use mutiple vars like this
{{ path('path_in_route', {'id': article.id,'slug': article.slug}) }}
And
path_in_route
must be in your router file.
Hope that helps you.
Can you try the following in your Twig file:
{{ url(item.url,{'id': id}) }}
I believe that should work, but not certain.
EDIT #2
What if you use something like this:
$app->match('/item', 'Controller::method($id)')->bind('named_url');
Where $id is the id you need to pass in. I'm not really familiar with Silex, but maybe it's something like that?

twig path() function generating different outcome

I am trying to add dynamic links to my twig template by calling path() with a parameter.
{{ path('single_sale_submit_page', {'id': book['id']}) }}
I use annotation in my controller:
#Route("/book/{id}", name="single_sale_submit_page")
This results in the following url: ../book/?id=123456789. I keep getting the error that my controller needs a mandatory parameter, which is of course true cause the generated url has a different syntax(?).
How can I set up twig in a way that the generated url from path() corresponds to
../book/123456789
and not
../book/?id=123456789
EDIT:
This question has somewhat the same question as I have.
Add a default value in the annotation to the controller:
#Route("/boek/{id}", defaults={"id" = 1}, name="single_sale_submit_page")
Clear the cache with:
app/console cache:clear
After reloading the paths generated by path() will correspond to:
../book/123456789
and not:
../book/?id=123456789
I was having the same problem and my mistake was in param name, it was "struct-id", and I needed to remove "-" or change it to "struct_id".

how to pass all request query parameters to embedded controllers in twig symfony 2?

{{ render(controller("SomeBundle:Foo:Bar", {HERE I WANT TO PASS ALL query parameters app.request.query.all}) }}
So can I access all master request query parameters in sub request and the subrequest should also run independently?
Try this:
{{ render(controller("SomeBundle:Foo:bar", {'all': app.request.query.all}) }}
and in action store it in $all variable
public function barAction($all) {
// other your code
}
From your controller:
array_merge($request->query->all(), $request->get('_route_params'));
//query->all : get all query string parameters
//_route_params : get current route parameters
From your twig template must be sth like:
app.request.query.all|merge(app.request.attributes.get('_route_params'))
I've never used this in twig templates, so first test it ;)
Then you can use that functions however you want to build the variables you'll pass to your subrequest
To just pass in what is in app.request.query.all:
{{ render(controller("SomeBundle:Foo:Bar", app.request.query.all)
To merge something extra in:
{{ render(controller("SomeBundle:Foo:Bar", { something: 'extra' }|merge(app.request.query.all))
Tested in Symfony 3.3.10 and Twig 1.35.0

Categories