Twig - dynamically replace GET parameter's value - symfony

Is there a way to replace a GET parameter value from twig?
For example, I have a page at this address:
http://localhost/app_dev.php/test/?param1=40&sort=name
And in my twig I want to build 3 links like this:
http://localhost/app_dev.php/test/?param1=40&sort=name
http://localhost/app_dev.php/test/?param1=40&sort=address
http://localhost/app_dev.php/test/?param1=40&sort=code
For now I added the "&sort" parameter once again at the end on the URL, but this solution is actually a "patch" and it sucks!
address
In this example I only have 2 parameters, but in reality I have around 6 parameters, because the link that's generated it's obtained by submitting a .

This should solve your problem:
{{ path(app.request.attributes.get('_route'),
app.request.query.all|merge({'sort': 'address'})) }}
It gets the current route and all query parameters which are merged with the one you like to update before they are appended.

Symfony/Twig path function accept optional params. If these params are part of the route, they're handled by router but if they're not, they are passed as GET parameters.
So, if your corresponding route is, for example, my_route :
address

Related

Define parameters as param request instad of endpoint url in fasthttprouter

I am using golangĀ“s fasthttprouter and have followed the examples and defined a router like this:
router.GET("/customer/account/detail/:accountId", myHandler.customerAccountDetailHandler)
Then I call to my service as http://locahost:9296/customer/account/detail/2
But I realised that I do not want to have the parameters as part of the endpoint , I rather prefer to use normal parameters by calling my service like this:
http://locahost:9296/customer/account/detail?accountId=2&sort=1
Is it possible to be done with fasthttprouter? How?
Thanks in advance
J
The query parameter should be accessible from the request context.
You should have a handler that takes a *fasthttp.RequestCtx argument. This RequestCtx can access the URI and the query params on that URI. That should look something like this:
ctx.URI().QueryArgs().Peek("accountId")
You'll have to update your handler to use this query parameter instead of the route param you were previously using. The same would also apply for the sort param.
Also, your router would have to be updated to route /customer/account/detail to your updated handler (i.e. you'll want to remove /:accountId from your route).
Your questions is similar to this one:
Get a request parameter key-value in fasthttp
You can retrieve the parameters of the request in this way:
token = string(ctx.FormValue("token"))
Have a look at my complete response here
https://stackoverflow.com/a/57740178/9361998
Documentation: https://godoc.org/github.com/valyala/fasthttp#RequestCtx.FormValue

Passing variables in wordpress

I am having trouble with passing variables in my wordpress url. When i pass the variable and the value to the url, all is well
i.e.
mysite.com/product-part/?part=1/
but what i want is for the variable to be passed as follows:
mysite.com/product-part/1
In php, the normal way to pass variables to a url is:
mysite.com/?id=1
In wordpress, the above would look like this:
mysite.com/1
How can I achieve the above?
The Rewrite API lets you add create custom rewrite rules inside WordPress. You can call add_rewrite_rule() inside the "init" hook and give it a regular expression to translate into a query string. Something like:
function setup_rewrite_rules() {
add_rewrite_rule('^store/([0-9A-Za-z]+)/([0-9]+)/?', 'index.php?product_slug=$matches[1]&part=$matches[2]', 'top');
}
add_action('init', 'setup_rewrite_rules');
Note that the URL isn't an exact match for the existing product URLs because you need something that matches this regular expression.
You'll probably need to use a template_redirect handler to detect when these variables are set and show the normal product page since you're not using the product's normal permalink.
This is a very, very bad way to pass a variable. Wordpress uses "re-write" rules to determine what query to run. These "permalinks" identify, for instance, what post your are going to. In your example, using an integer such as "1", you could pass a variable by writing a re-write rule that said something like "all integers are a variable", or "all slugs that start with an integer are a variable" but you would soon get into conflicts with post names. What about posts that start with numbers, for instance? Also, many plugins would use permalinks to send you to certain pages, and you could come into conflict there. Better to use any of these things to pass variables:
get variables
post variables
hidden post variables
session variables
nonces
Wordpress meta-data like user meta data
Good luck

how to get query parameter in lua or nginx?

I am trying to implement this-
https://gist.github.com/MendelGusmao/2356310
Lua,nginx based URL shortener,The only change i want to implement is when some query string parameter comes with shortened URL i need to take that parameter and insert into the long URL.
e.g.
http://google.com?test=2 will be like http://abc.in/abc
while hitting on http://abc.in/abc?test=3 I get redirected to - http://google.com?test=3.
For that i need to take query string parameters from $request_URI, can any one help with some code?
You should be able to use ngx.var.arg_name where name is the name of the query parameter you want to access. See Variables with Infinite Names section in this tutorial for details on query parameter handling; you may also check my blog post for Lua nginx/openresty examples.
As an alternative, you can use ngx.req.get_uri_args() to retrieve all query parameters as one table. See this section in the same tutorial for the brief comparison between these methods.
You can also use ngx.var.QUERY_STRING to access the query string and unescape and parse it.
You can obtain the query parameter with just nginx by using $arg_test, test is the name of the query parameter in this example.
This is documented in http://nginx.org/en/docs/http/ngx_http_core_module.html#var_arg_.

Pager numbers do not match URI string in drupal 7?

How can I make the pager numbers match the URI string?
For example : When I click on number 3 in pager to turn to page 3 but the the URI is "?page=2" instead of "?page=3". This pagination isn't normal as any other pagination I've met before. How can I fix it?
Thanks!!!
You could do it by implementing hook_url_outbound_alter(), and hook_url_inbound_alter().
With the first hook, you alter the links that are being output from Drupal; you would increase the value of $options['query']['page'].
With the second hook, you alter the links being received from Drupal; you should find the '?page=' string in the URL, and decrease the number after that string.
Keep in mind that $_GET['page'] can be a string like '1,2,4,5,6' if the page has more than one pager. See the code of pager_find_page().
Also, hook_url_outbound_alter() receive the query parameters in $options, while hook_url_inbound_alter() receives just a string for the URL, which also contains the query part.
The page attribute in the URL is used directly in the query, so if ommitted, page will get 0 as a value. It's normal because Drupal do pagin things like this.
Now if you need to change this, you have to work arround query alters for every query, to add +1 to the limit() or range() methods.

Extended use of replace filter in twig?

i checked out the documentation for the replace filter in twig. my problem is that, suppose i have a variable say contvariable, and the content passed through that variable from the controller is dynamic
return $this->render('RodasysFormstudyBundle:Default:addclientname.html.twig', array('contvariable' =>$sometext));
this $sometext variable will contain texts like
$sometext='%Sun% rises in the East';
the text inside the %% should be displayed as an input field in the browser. I did not find any examples in the web like to replace the content inside the %% (what ever the content be whether its sun or the moon). Is this possible to do this using replace filter or should i follow some other method such as to replace the content in controller before sending it to twig..
please help..
You could do something like that (with the 'raw' filter) :
{{ "%foo% rises in the East"|replace({'%foo%': "<input type='text' name='"~foo~"' value='"~foo~"'/>"})|raw }}
foo is a variable sent by your controller, with the value of your choice.

Resources