9
votes
Can you get the HTTP status code in twig templates?
Since this is the top hit in Google, I'll guess it's handy to mention that craft.app.response.statusCode will display the statuscode in Craft v3 and up.
4
votes
From a Twig template, how to retrieve the content of a HTTP header in the current request?
By checking the code, I could find a method named getHeaders() that will return all the headers of the current request.
So if you want to retrieve the content of the foo header, just use:
{% set ...
3
votes
Accepted
How does Craft CMS gets data from a 3rd part API?
Unless there's a community plugin specifically built for connecting to this API, you're gonna have to build your own. But a simple module to connect to a third-party API and get some basic data is not ...
3
votes
Accepted
Read POSTed JSON data from the request body
Yes, you can use getRestParams() like so: $input = craft()->request->getRestParams()
A bit confusing naming there, which is why it's changing to getBodyParams() in Yii 2.
3
votes
Accepted
Ajax Function on a search form not working in Safari & IE
Try attaching the listener on the forms submit event instead, and prevent that instead of just the click on the button. Something like this (make sure you change the selector for your form):
{% if ...
2
votes
From a Twig template, how to retrieve the content of a HTTP header in the current request?
craft.app.request.getHeaders() returns an instance of yii\web\HeaderCollection, which has a number of public methods you can use from a Twig template.
For example, to see if an isAjax header is ...
2
votes
Accepted
How can I force a request type (HttpRequestService::isCpRequest vs. HttpRequestService::isSiteRequest)?
There's no setRequestType() method or the like, no. The HttpRequestService will consider any request with the cpTrigger in its URL a control panel request, and I don't see any easy way around that.
...
2
votes
What is the best way to handle HTTP requests within a plugin using Yii or Craft?
Craft includes Guzzle which is very full-featured... I'd use that.
Sorry I don't have time for converting your example but here's a simplified version of how to use it in a Craft plugin:
$client = new ...
2
votes
Accepted
Show a list of pages but hide the current page from the list
To me it looks like the entries in the section with the handle pages have their own URLs. This means that if you visit a URL that belongs to an entry in that section Craft will automatically make that ...
2
votes
Accepted
Disable template cache globally based on condition
I can't think of a different way to conditionally enable template caching (globally, i.e. not per {% cache %} tag) other than to conditionally set the enableTemplateCaching config setting to either ...
1
vote
Is there a way to determine an element by its path or full url instead of the uri?
You can always use the search function -> https://craftcms.com/docs/4.x/searching.html which works excellent, especially via GraphQl. If you need to query for full Urls I would probably strip it to ...
1
vote
request (Get and Post) problems
All right, after a good night of sleep starter to put trace all over the place to see what's going on... So I share, just so if it can help anyone...
I found out the variable was there on the header ...
1
vote
how to get request url?
This isn't really an answer, but my investigating was too long for a comment.
Ultimately, craft.app.request.url gets routed through Yii's resolveRequestUri() and likely gets pulled from $_SERVER['...
1
vote
Accepted
List products related to currently active category (url)
The same way as entry is available if you hit an entry, the variable category is available if you hit a category.
Thus the following code:
{% if category is defined %}
{# Specific category ...
1
vote
Accepted
Calling Craft Console Commands from Plugin?
Since Yii treats console commands like requests, you need to perform the following steps to execute a Craft console command from within a plugin:
Within your plugin, set the namespace to match the ...
1
vote
Accepted
How do I correctly send an external request to a module controller? runAction() is returning null
I solved my issue, but unfortunately at this point in time I don't fully understand why this resolves the problem.
My controller, its behaviors method, and my frontend request were all correct. The ...
1
vote
How should I fix an odd action URL?
In Craft 3 the csrf token validation in the general.php config is set to true by default. In order to request your controller with post requests you have to turn it off or include the token
https://...
1
vote
Accepted
How to write a segment that is not two other segments
I personally try to avoid inlining too many conditionals because I feel like it leaves room for logical loopholes. I also think using variables for segments keeps things easier to read.
{% set seg2 = ...
1
vote
Accepted
Use REQUEST variable as dynamic upload location
That field accepts any valid Twig code, so something like this should work:
challenges/{{ craft.request.getPost('challenge_id') }}/
Fair warning, though... I'd also be wary of accepting untrusted ...
1
vote
Accepted
How to get corresponding Post in Craft request?
That makes sense, because getPost always gets the last post (I think).
If you've got multiple form inputs with the same name, then the last one in is probably the value that will get sent over POST ...
1
vote
Accepted
Multiple dropdown filters interfering with each other
If your goal is to filter by one drop-down value at a time then you could use an if statement to check if the passed value is set for each drop-down. This will only work if each drop-down has an ...
1
vote
CloudFlare Shared SSL certificate forbids CraftCMS control panel sign in
I'd give Patrol plugin a try, just activate secure connections.
1
vote
Accepted
Bad Request with weird params
Allright, after a lot of digging into my code I finally found out what went wrong.
I used this code to clear the placeholder in my input when clicking inside them:
$(".some-class input").val("");
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
request × 45plugin-development × 10
url × 7
craft3 × 6
templating × 6
ajax × 3
controller × 3
modules × 3
https × 3
querystring × 3
post × 3
plugins × 2
search × 2
php × 2
cache × 2
error × 2
login × 2
category × 2
server × 2
csrf × 2
params × 2
entries × 1
localization × 1
database × 1
form × 1