Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.

Hot answers tagged

9 votes
Accepted

Access POST data to conditionally display form

I think you want this: {% set success = craft.request.getPost('test') %} {% if success %}form was just submitted{% endif %} Pass a string to getPost()
user avatar
6 votes

Access POST data to conditionally display form

If you just want to test if something is in POST, and don't care what it is, then this should work: {% set success = craft.request.getPost() | length %} {% if success %} form was just submitted {...
user avatar
  • 66.4k
6 votes
Accepted

Redirect user to the page they wanted after login

If you are using {% requireLogin %} tags, then the Craft UserSession service has a getReturnUrl() method which should give you what you want. It is accessible in your templates via craft.session....
user avatar
  • 608
6 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.
user avatar
4 votes

Get query string param that has no value

A parameter with no value set returns an empty string '' for craft.request.getParam(). But if the whole parameter is not included at all, it returns null. This means you can test for null (→ see Twig ...
user avatar
  • 35.8k
4 votes
Accepted

How can I time-profile the page load for a Craft request?

If you enable devMode, then database profiling information (number of queries, SQL executed, execution time, etc.) as well as PHP script benchmarking information for the request will be output to your ...
user avatar
  • 66.4k
4 votes
Accepted

Get parameters from referrer

It can be done with some Twig code: {% set referrer = craft.request.urlReferrer %} {% if referrer %} {# Remove everything before the `?` #} {% set queryString = referrer|replace('/^.*\\?/', '...
user avatar
4 votes

Update and add POST URL parameters

If i'm not mistaking, craft.request.getQuery() returns an array filled with all $_GET key-value pairs, so in your case: {% set queryParams = craft.request.getQuery() %} will let you do: // Do with ...
user avatar
  • 6,288
4 votes
Accepted

Compose URL POST parameters to filter entries by category?

I'd split the parameter string into an array and then loop through the categories and use a conditional to check if the value already exists in the parameter. It's not so easy to describe in detail, ...
user avatar
  • 35.8k
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 ...
user avatar
  • 496
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.
user avatar
  • 2,947
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 ...
user avatar
  • 7,228
3 votes
Accepted

Empty .getParam() does not return an array with dump()

The behaviour you're seeing is by design – craft.request.getQuery() will return all GET variables if it's called without arguments, whereas craft.request.getParam() requires the name argument – i.e. ...
user avatar
3 votes
Accepted

Translated entry page can not be found (404)

The issue appeared to be the nginx config. I updated it according to an example from here (Locales sub directories on Nginx) and it started working.
user avatar
  • 163
3 votes
Accepted

Can you get the HTTP status code in twig templates?

No, the craft.request (HttpRequestVariable) class doesn't expose the HTTP status code. It'd be super easy to add it to Twig as a variable method in a custom plugin, though: namespace Craft; class ...
user avatar
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 ...
user avatar
  • 6,070
2 votes

Undefined property: Craft\WebApp::$request

I had the same issue. But I was using memcache. It turns out it was acually memcached which is a slightly different extension. Enable the setting in the memcache.php config file with '...
user avatar
2 votes
Accepted

Undefined property: Craft\WebApp::$request

This issue turned out to be caused by a config setting that had been added to general.php 'cacheMethod' => 'apc' The server had APC installed but my local environment did not. There were a ...
user avatar
  • 13.2k
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. ...
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 ...
user avatar
2 votes
Accepted

Spitting out the URL of the entry

You could build it up using a couple of the request variables: {{ craft.request.serverName ~'/'~ craft.request.getSegments | join('/') }}
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 ...
user avatar
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 ...
user avatar
  • 718
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 ...
user avatar
  • 3,829
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 ...
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://...
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 = ...
user avatar
  • 1,887
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 ...
user avatar
  • 66.4k
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 ...
user avatar
  • 66.4k
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 ...
user avatar
  • 2,707

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