Hot answers tagged

8

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


6

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.returnUrl. Here is a link to the docs. Hope this helps!


5

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 {% endif %}


4

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 docs) like so: {% if craft.request.getParam('myparam') is not null %} My Parameter is included in the query string! {% endif %}


4

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 browser's console as well as the craft/storage/runtime/logs files. Also, as I understand it, every plugin is loaded on each request Installed and enabled ...


4

It can be done with some Twig code: {% set referrer = craft.request.urlReferrer %} {% if referrer %} {# Remove everything before the `?` #} {% set queryString = referrer|replace('/^.*\\?/', '') %} {# Split the query string into individual params #} {% set params = queryString|split('&') %} {% for param in params %} {# ...


4

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 it what you like ... {% set queryParams = queryParams | without(key) %} // Removes a param {% set queryParams = queryParams | merge([{ 'limit' : 10 }]) %} // ...


4

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, that's why I added lots of comments to the code (untested!). To work in your other parameters (size, etc.), simply add them to your links exactly how craft....


4

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.


3

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

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 not craft.request.isAjax %} {% set pagingJs %} $(function () { $('#myForm').on('submit', function (e) { e....


3

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. craft.request.getParam('foo'). The changelog notes for build 1.3.2465 does reference a couple of issues related to these methods, but nothing suggesting any ...


3

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.


3

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 MyPluginVariable { public function getHttpStatus() { return http_response_code(); } } ...and in your template: {{ craft.myPlugin....


3

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 headers = craft.app.request.getHeaders() %} {% set fooHeader = headers['foo'] %}


2

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 'useMemcached' => true,


2

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 couple options to fix this: Install Alternative PHP Cache (APC) Setup a multi-environment config and override the cacheMethod setting with another method that is ...


2

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. It's a bit convoluted, but what you could do is to wrap whatever code you're currently executing in your entries.onSaveEntry in a controller action method, and ...


2

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 element first, based on the URI, using craft()->elements->getElementByUri($uri). However, this will not work because of the beginning slash that is returned by ...


2

You could build it up using a couple of the request variables: {{ craft.request.serverName ~'/'~ craft.request.getSegments | join('/') }}


2

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 entry available in the template as the entry variable. That being said your solution is in this case: {% set currentPageId = entry.id %} {% set pages = craft....


2

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 \GuzzleHttp\Client([ 'base_uri' => 'https://www.example.com/api', 'http_errors' => false, 'timeout' => 10 ]); $data = ['some' => '...


1

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://docs.craftcms.com/v3/changes-in-craft-3.html#csrf-token-params {% set csrfTokenName = craft.app.config.general.csrfTokenName %} {% set csrfToken = ...


1

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 = craft.request.getSegment(2) %} {% set seg3 = craft.request.getSegment(3) %} So maybe something like this? The keyword in is Twig's way of checking in_array() ...


1

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 front-end input like this that ends up on the file system.


1

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 to the controller. You could make sure your themeId name inputs are unique by appending a unique string or some such to them, then parsing through all of the ...


1

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 option that is blank or it submits on select so that the other drop-down is not set. {% set query = "" %} {% if craft.request.getParam('country1') and craft....


1

I'd give Patrol plugin a try, just activate secure connections.


1

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(""); But .some-class was the parent class of my form, which means I probably erased all values of hidden fields that Craft adds on runtime too. Fields such as <...


1

Turns out this was a bug that only occurred if the existing URL didn't have an URI associated with it. i.e. http://craft.dev?foo=bar. It's been fixed and will be included in the next 2.5 beta release (or the first 2.5 public release if you don't want to swap to the beta track).


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