Skip to main content
8 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.
NxWebster's user avatar
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 ...
kant312's user avatar
  • 496
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 ...
MoritzLost's user avatar
  • 11.5k
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.
Fred Carlsen's user avatar
  • 2,997
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 ...
André Elvan's user avatar
  • 7,288
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. ...
Mats Mikkel Rummelhoff's user avatar
2 votes
Accepted

Retrieving an entry based on the request URI in a plugin controller

You can grab the current entry using the $uri = craft()->request->getRequestUri() which will return the URI in a format like so: /path/to/uri To grab the entry, its easiest to grab the ...
Jason McCallister's user avatar
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 ...
James Smith's user avatar
  • 5,344
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 ...
Johannes's user avatar
  • 740
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 ...
Mats Mikkel Rummelhoff's user avatar
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 ...
Josh Harrison's user avatar
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 ...
thomas's user avatar
  • 131
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 ...
Sylvain Tremblay's user avatar
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['...
Brad Bell's user avatar
  • 67.5k
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 ...
nitech's user avatar
  • 679
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 ...
Jason McCallister's user avatar
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 ...
Steven Thate's user avatar
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://...
Robin Schambach's user avatar
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 = ...
Ryan's user avatar
  • 1,952
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 ...
Brad Bell's user avatar
  • 67.5k
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 ...
Brad Bell's user avatar
  • 67.5k
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 ...
a-am's user avatar
  • 2,857
1 vote

CloudFlare Shared SSL certificate forbids CraftCMS control panel sign in

I'd give Patrol plugin a try, just activate secure connections.
Leo Leoncio's user avatar
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(""); ...
Tom De Smet's user avatar
  • 1,456

Only top scored, non community-wiki answers of a minimum length are eligible