Questions tagged [controller]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
8
votes
1answer
1k views

How do I handle named route variables passed to a controller?

I'm writing a plugin that uses the registerSiteRoutes hook to create some new routes, and passes those routes to some controllers. In these routes, I have a few pieces of the URL that I'm capturing as ...
7
votes
2answers
744 views

Best way to use custom enums in my plugin templates?

I am building a plugin that's uses an enum for custom statuses. The statuses are integrated within my plugin with the exception of changing the status in my template code. pluginname/enums/...
6
votes
1answer
620 views

Allow anonymous submission on a custom element type?

I have a custom element type that I want to allow anonymous submission from the front-end. I have a solid concept on how the form data is passed but I seem to be missing something. I have the ...
5
votes
3answers
649 views

How can I set the HTTP status code?

I'm trying to set a 404 HTTP status response code for an ajax controller action in a plugin. Yii has a response object where the code can be set, but I can't find a way to access it from craft.
5
votes
1answer
2k views

Rendering templates from plugin controller

Right now I'm trying to accomplish routing users through a controller action before rendering the plugin's CP template... [Plugin]Plugin.php // ... public function registerCpRoutes() { return [...
5
votes
1answer
1k views

How to return errors from a model back to the template?

I'm working on validation with Craft. I have a custom plugin that allows public submission. However, I want to make sure I can pass errors back to the template. The key is trying to make my code as "...
5
votes
1answer
939 views

Should my controllers actions 'return' when rendering templates?

When developing a plugin, I am registering routes to controller actions. Other frameworks, read that as Laravel, typically return the controllers response. public function showProfile($id) { $...
5
votes
1answer
214 views

Plugin routing casing: camel, lower, Pascal

Recently got tripped up with plugin routing due to casing. It seems when you're in the control panel, your "plugin handle" is lower-cased in routes, though both seem to work: /cp/settings/plugins/...
4
votes
2answers
210 views

Is it typical that data is fetched from within templates rather than in controllers?

I'm assessing Craft's suitability for my next project, coming from using HMVC in CodeIgniter with a custom CMS. In CI all requests go via a controller action which is responsible for inspecting ...
4
votes
1answer
984 views

Creating sessions in Twig

I am trying to create the PHP $_SESSION equivalent in Craft CMS using Twig. I have searched very long and I can't find how you can create sessions in Twig. In previous posts I have found that I ...
4
votes
1answer
2k views

Cannot post data to a plugin controller action

I have a form in one of my templates <form method="post" action="" accept-charset="UTF-8"> <input type="hidden" name="action" value="paypalPayments/payment/pay"> [...] </...
4
votes
1answer
987 views

Is the “registerSiteRoutes” hook screwing up my template path?

This may be a bug... I have a controller action which has been working great. It's a simple action with a single purpose... load a template with an internal value. public function actionMyAction() { ...
4
votes
1answer
154 views

Assets indexing from plugin controller

I am making a dashboard widget to trigger the assets indexing action. I got the controller ready but I do not understand then how to trigger the action. Should I go with AssetIndexTool? Then ...
4
votes
2answers
799 views

Twig Filter in Plugin/PHP?

Is it possible to access twig filters in a plugin? I want to be able to preview how text will look in a textarea using Markdown for a discussion board, it'd be awesome to use ajax to send the content ...
4
votes
1answer
790 views

How can I ensure using actionUrl in my template does not show script name in the generated URL?

I have an controller action in my custom plugin that processes Paypal IPN requests. How it works is that you provide a URL for Paypal to send it's notification requests to and then process them and ...
4
votes
1answer
86 views

Store categories in array by level in controller

I'm making a plugin and I need to get an array from a certain category group where the levels of the array are the same as the level of the categories in the group. So I used the following code to ...
3
votes
1answer
2k views

Controller action works but still gives 404

I'm writing my first plugin and I'm slowly getting there, but I hit on a problem: I have a controller class in which I have a function that sets a cookie. I call on this function from within a ...
3
votes
2answers
2k views

Direct access to controller

I'm trying to create a plugin with a controller, but I don't want to render any view but run a script from a cronjob. I can't get into the controller with an url and I'm not sure why. I already ...
3
votes
1answer
502 views

How to tell if user exists

I am automatically creating users in a service, is there a way to check if user exists? I want to log in id user exist or save a new user if it doesn't, I have the following so far which will add a ...
3
votes
1answer
990 views

How do I update a column value in my custom plugin with a simple controller?

I have a custom element type with its own table 'pluginname_entries'. I am trying to find a way to "dummy proof" changing the plugins status. So I came up, aka stole from the Events element type, with ...
3
votes
1answer
101 views

Where do I begin troubleshooting a Form in Craft that is redirecting to /login on iPad and iPhone?

I'm not sure how to ask this question. I have a form. It works just fine on desktops. However when the form is submitted on iPhone or iPad it never makes it to my controller and resolves to the URL /...
3
votes
1answer
55 views

How to re-populate form with old values when submitted from a controller?

I have a form in a template which makes a request to the specified action (a controller). The controller decides that there is an issue with the inputs and needs to provide error output for the user. ...
3
votes
2answers
992 views

Controller action redirection when called via route

I have a route set up in routes.php that calls a controller action using something similar to what is described here: https://straightupcraft.com/articles/how-do-i-setup-a-route-to-point-to-a-...
3
votes
1answer
452 views

can i used craft.entries.section into my Controller

I am new in Craft CMS and this is my first plugin. Below is my code to get products. {% craft.entries.section('products').limit(9) %} This code is working fine but can I use this code in my ...
3
votes
1answer
901 views

How do I access AJAX data within a controller method?

I have a JavaScript file which sends an array of data to a controller: Craft.postActionRequest('wineNotes/wineSpreadSheet/saveWines', data, ...) The structure of the data variable is as follows: ...
3
votes
2answers
329 views

Query entries relatedTo multiple categories in controller

I have an AJAX GET call that should count all the products matching three different taxonomies. Product families Sectors of application Liquids suitable for the product so in the form the user ...
3
votes
1answer
872 views

Access data when posting to controller from front-end

I'm writing a plugin to notify authors when a Disqus comment is posted. There is a Disqus JS callback you can use to hook into this, so I'm doing the following on the front-end: this.callbacks....
2
votes
2answers
812 views

How to render plugin templates in Craft CMS 3?

I cannot find a way to render templates from a plugin controller in Craft 3. I feel as though I've tried every variation advised online but for some reason this still isn't working. <?php ...
2
votes
2answers
794 views

Constructor in custom controller?

I'm implementing a custom controller as part of a plugin I'm building. There's some set up work that I'd like to do in the controller's constructor. I created a __construct method, and it seems to ...
2
votes
1answer
209 views

What are the different scenarios to plan for when using $allowAnonymous in a controller?

Craft 3.2 updates how the allowAnonymous behavior works. I have things working in our plugins but I don't understand the different syntaxes needed across our different allowAnonymous use cases. For ...
2
votes
1answer
671 views

Craft 3 Controller giving 404

Updating a plugin to Craft 3 (also changed its name). I know this is probably something very simple, like capitalization, but can anyone point out what's wrong? Firefox console shows a 404 for http://...
2
votes
1answer
1k views

What should my controller return in a Craft.postActionRequest

Simple question: I made a button in my plugin settings that creates a Craft.postActionRequest but what should the controller return? It does not need to return anything else than true (successful) or ...
2
votes
1answer
296 views

Business Logic Controller for Validating Address in Commerce

I am putting together my Business Logic plugin, as I want to validate the address fields throughout the checkout process of my Cart. I understand in order to do this I need to create a plugin and us ...
2
votes
1answer
930 views

How do I call a service from my controller in a plugin?

I'm writing a plugin, and used plugin.io to generate my scaffolding. In my controller I am attempting to call a service, and getting an error. This is my service code from my generated plugin it ...
2
votes
1answer
772 views

Controller Action 404

Ok once again I'm having an issue with a plugin controller. It was working once upon a time but stopped sometime in the past year. I have stripped the plugin down to bare, so there is no reason it ...
2
votes
2answers
234 views

Dashboard Widget: Access widget settings from controller / service

i wrote a plugin with an additional dashboard widget. i have to refresh the widget data every hour with ajax. so i added a plugin controller in my plugin directory with the idea of re-rendering the ...
2
votes
1answer
518 views

How to catch errors in a service and return to a controller?

How do you handle “catching” errors in a plugin service and return the error to the plugins controller? To be more specific... I am sending an Ajax request to a controller, that controller is ...
2
votes
1answer
150 views

Issues with returnJson not returning html?

Im running in to issues with returnJson not returning html or just returning and empty object: $userHtml = craft()->logic->renderSiteMacro('_macros/elements', 'listing', $args); $this->...
2
votes
2answers
1k views

How to create a basic plugin front-end controller?

I'm trying to create a very simple controller from a "foo plugin", but it seems there is something i did not understand because i can't make it work. (404 error) I looked at contactForm plugin as an ...
2
votes
2answers
428 views

Passing data back to form on invalid submission

I currently have a form that is being displayed to the user via a custom route. The form submits to a controller. The first thing the controller does is populate a custom model and validates it. If ...
2
votes
1answer
35 views

Finding the correct actions url

struggling to decipher how to link a plugins controller to an action route. The docs say to use "https://pluginfactory.io/" to scaffold one, but what it generates is a different structure to what the ...
2
votes
1answer
46 views

Update a lightswitch field in Craft CMS 2

How can I edit a lightswitch field in an entries field layout via PHP? This is my current approach public function actionSponsoredEntriesPriority() { $this->requireAdmin(); $this->...
2
votes
1answer
53 views

Some (but not all) controller action routes return 404 on staging server

I've created a module for a project that has several actions, all of which are called through AJAX. Each is a post request that sends over any required params plus the CSRF token using the method ...
2
votes
2answers
133 views

Add a button to the entry edit screen via plugin

I would like to add a button to the edit entry screen, only for already saved entries, alongside the "Share" button. When clicked, I'd like to call my plugin's controller action and post the entry ID....
2
votes
1answer
156 views

Send an asset from a plugin controller

I have a controller which fetches an asset from an entry (this is the service method that provides the asset): public function findPoster($entryId) { $criteria = craft()->elements->...
2
votes
1answer
197 views

Insert action in front of template, while maintaining entry slug

We are attempting to implement a number of Pardot forms within Craft templates. Since Pardot's form actions do not allow us to dynamically specify a return URL, our best choice is to use the form's ...
2
votes
1answer
1k views

Using the required field validation in custom element types/plugins?

I am finishing up the last few details on my custom element type. I have custom statuses and public submissions working. The last detail is providing validation on custom fields set through the field ...
2
votes
0answers
457 views

Craft 3 Plugin - Only default action works

I'm working on a Cloudflare integration plugin for Craft 3. I set up the plugin with settings, which works fine so far. Now I intended to verify the Cloudflare credentials using an AJAX call. I set ...
1
vote
3answers
502 views

Craft CMS plugin with AJAX gives 404 error

I have created a small craft cms plugin to pull some information from the DB, and everytime I execute the plugin I get a 404. Here is the controller namespace Craft; class ...
1
vote
2answers
78 views

renderTemplate() renders the twig file without any CSS

I've set up the routing for a specific URL in my plugin to trigger the actionIndex controller method. I after logging a message to the Craft logs, I want to return the rendered twig file as the view, ...