twig path() function generating different outcome - php

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".

Related

laravel 6.5 shows an edit url with a question mark

In laravel 6.5 when using the route helper function and creating an edit link the output that i get is myurl/myroute?id=1 where before it was myurl/myroute/1/edit. How can i go back to the previous state?
this is my code, my routes are named.
Edit
If you use a route resource:
Route::resource('project', 'ProjectsController);
Then it uses {project} as the model binding so instead of id you should pass project.
{{ route('project.edit', $project) }}
Should work just fine and replace the wild card with the id of the model.
Running php artisan route:list will give you exactly the name of the parameter expected in the route :)

symfony 3 url issue

Hi I'm coming for a very simple question (I think) but i didn't found the answer or a similar case.
I'm using symfony 3 and trying to build a second menu for my administration pannel.
However, I have a problem about how I have to declare the relative url in my "href", For my main menu i used to do like this
{{ url ( 'admin' ) }}
and it worked. The fact is that now I have subfolders and many levels in my url.
The url i try to reach is myapp/admin/gameadmin, this url work when I'm going on it but when i try to put it in 'href' I have an error message which says that the route is not working.
i declared it like that ->
{{ url(admin/gameadmin) }}
I tried with different character -> admin:gameadmin, admin\gameadmin ... etc and with path instead of url i don't know if it's not the good way to declare it or if I have a problem with my controllers.
In my bundle it's organised like that :
->Controllers(folder)
->admin(folder) (You can also find my main controllers on this level)
->admingamecontroller (Where the page I try to reach is routed)
I hope i gave you all the informations, thank you for your help and sorry for my english !
The url parameter is not the the url per se (ie: admin/gameadmin), this is the route name, defined in your routing.yaml file, or in your controller annotation.
If your action is something like this:
/**
* #Route("/admin/gameadmin", name="gameadmin")
*/
public function gameAdminAction()
{
...
}
Then, to generate the route, you have to do this:
{{ url('gameadmin') }}
By doing this, all the links on your website will be up to date if you change the gameadmin url, as long as you don't change the route name.
I suggest you to read this documentation on the Symfony website: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html
Edit: As pointed by user254319, if you're not using annotations, you'll have to edit your routing.yaml config file.
gameadmin:
path: /admin/gameadmin
controller: App\Controller\Admin\AdminGameController::gameadminAction
The route name is the yaml key: gameadmin.
Related Symfony documentation: https://symfony.com/doc/current/routing.html

Hypertext to route not working

Hi I have a hyperlink from a page:
<h3>hitest</h3>
the route:
Route::get('hitest', function(){ return 'hitest message';});
There is an error:
No query results for model [App\User2].
hyper link is from a page with this url
/userpage/1
the 1 is a model object.
Shouldn't the hyperlink route to /hitest ?
Please see my other post: Strange behavior with routing and hypertext.
I'm new at web development. Is there configurations for routing? The app is hosted (not local).
As bytesarelife already mentioned, you can use the url()-function, like so:
{{ url('your/url/') }}
A better way in terms of maintainability would be to give your routes names, so you would not have to replace every url in every template once you want to change it. You can do so, by adding the name in your routes:
Route::get('hitest', function(){ return 'hitest message';})->name('getHittest');
And then you can use the route function in your view:
{{ route('getHittest') }}

Symfony2 Routing annotation gets url encoded

I've playing around with annotations in Symfony2.5 and am struggling to output a nice clean url like this:
.../display&type=foo&id=1
What I always get is:
.../display%26type=foo%26id=1
Why is Symfony2 url encoding this?
My controller looks like this:
/**
* Displays content
*
* #Route("/display/type={type}&id={id}", name="content_display")
* #Template(...)
*/
public function displayAction($type, $id = null) {
...
}
my twig template has:
{{ entity.id }}. {{ entity.name }}<br />
So I've tried to add |raw and autoescape off tags described here. But no luck so far, any ideas?
Thanks!
/display/type={type}&id={id}
...is not a valid URL... you'd have to replace that second slash with a ?:
/display?type={type}&id={id}
That said, the route is meant for path replacement -- not parameter replacement.
I'm pretty certain that if you change that to simply /display, when you try to render the URL with your key-value map, then Symfony will add those as parameters, since it can't find those keys in the path itself.
Edit:
Confirmed via the documentation:
The generate method takes an array of wildcard values to generate the
URI. But if you pass extra ones, they will be added to the URI as a
query string.

Symfony2 routing: How to request that the route will be taken from only one template

I heve an embedded controller in my base template. It's a search bar.
For the search bar controller, I have a route "myProject/search".
What I would like is that this route will be taken only when the template where I am embedding the controller (base.html.twig) will call it, and not when i manually put in the browser: "myproject/search".
Any idea on how to do that.
I think, since some time you can't do it:
http://symfony.com/doc/current/book/templating.html#embedding-controllers
quote from the docs:
Even though this controller will only be used internally, you'll need
to create a route that points to the controller
(...)
Since Symfony 2.0.20/2.1.5, the Twig render tag now takes an absolute
url instead of a controller logical path. This fixes an important
security issue (CVE-2012-6431) reported on the official blog. If your
application uses an older version of Symfony or still uses the
previous render tag syntax, you should upgrade as soon as possible.
Anyway, I guess, you can try do it yourself by passing some "secret" argument to search action when you call it from your template. Next in the action you check if the argument was passed to it, and if not you throw 404.
Another way to achieve your goal is use .htaccess file.
You can restrict your route to a certain method by _method option in your routing configuration:
your_rote:
pattern: /myProject/search
defaults: { _controller: YourBundle:YourController:YourAction }
requirements:
_method: POST

Categories