10 votes

How to render plugin templates in Craft CMS 3?

After becoming more familiar with Craft and Yii, I've found a more appropriate solution for my use case. I have a couple of ideal requirements: Templates are stored in a directory within my module or ...
Mike's user avatar
  • 277
9 votes
Accepted

Creating sessions in Twig

Go to https://pluginfactory.io/ to generate a boilerplate Craft plugin. API Version: Craft CMS version 2.5.x PluginName: Twig Session Description: Anything Version: 1.0.0 Plugin Author: Anything ...
Brad Bell's user avatar
  • 67.4k
8 votes
Accepted

Have a custom plugin do something on every page load?

Add a init() method to your plugin and as long as it's installed and enabled, it'll be called on every request. public function init() { parent::init(); /* Do your stuff */ } If you want it ...
Brad Bell's user avatar
  • 67.4k
8 votes
Accepted

How to render plugin templates in Craft CMS 3?

You don't specify, but it sounds like you're trying to have the plugin render its templates as a result of frontend requests? If so, you'd need to do: use Craft; use craft\web\View; $oldMode = \...
andrew.welch's user avatar
  • 11.6k
8 votes
Accepted

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

Here are the new constants that Craft 3.2 introduced: const ALLOW_ANONYMOUS_NEVER = 0; const ALLOW_ANONYMOUS_LIVE = 1; const ALLOW_ANONYMOUS_OFFLINE = 2; By default every controller will have the ...
Ben Croker's user avatar
  • 7,411
6 votes

How can I set the HTTP status code?

In Craft3, the correct way of setting HTTP status codes is Craft::$app->getResponse()->setStatusCode(404);
pabloroman's user avatar
6 votes
Accepted

renderTemplate() renders the twig file without any CSS

I sincerely appreciate the responses! I've since resolved this issue, below is the changed code. My controller was extending the Craft web controller (\craft\web\Controller) which has its own ...
Josh Kautz's user avatar
5 votes
Accepted

What should my controller return in a Craft.postActionRequest

If you're just looking to return true or an error message from your controller's action, then it should probably be something like: $this->returnJson(array('success' => true)); Or on error: $...
Brad Bell's user avatar
  • 67.4k
5 votes
Accepted

Is there a way to compare what has changed on an entry while it's being saved?

You're probably looking for Element::getDirtyAttributes() and Element::getDirtyFields()! The former is for native class properties/attributes (and is based on an ActiveRecord pattern in Yii, I believe....
August Miller's user avatar
4 votes

Call Craft controller with cron job

In answer to your second question: What is the best way to log and follow my controller actions in Craft CP side? You can log actions to a plugin-specific log file like this: ...
Steve Rowling's user avatar
4 votes
Accepted

Business Logic Controller for Validating Address in Commerce

You can move it all into a Service and then call it from your main plugin file if you find it is getting unwieldy. file: craft/plugins/businesslogic/BusinessLogicPlugin.php: class ...
Mike Pepper's user avatar
  • 4,391
4 votes
Accepted

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

Try it with a lowercase f in the middle... craft()->fulcrumDonation_fulcrumCyberSource->exampleService();
Lindsey D's user avatar
  • 24k
4 votes

Craft CMS plugin with AJAX gives 404 error

If you aren't currently logged in, you're apt to be running up against the "anonymous access" limitation. This section of the Controller documentation should help: By default, controller actions ...
August Miller's user avatar
4 votes

How to call Controller in Module?

You need to include your module handle to the route module-handle/site/say You also should use the correct namespace instead of app/controllers I suggest you to read the documentation You can as ...
Robin Schambach's user avatar
4 votes
Accepted

Craft 3 Controller giving 404

Craft 3 action routes are snake-case rather than camelCase thus your route needs to be plugin-handle/controller-handle/function-name instead of pluginHandle/controllerName/functionName Your ...
Robin Schambach's user avatar
4 votes
Accepted

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

There are several things you may need to dig into more to get the full picture here but here's a general idea of how you can approach this in Craft 3. Flash errors are useful to display a message in ...
Ben Parizek's user avatar
  • 13.4k
3 votes
Accepted

How do you get all the categories in a category group in a controller?

I found the answer. This might help others: $categories = craft()->elements->getCriteria(ElementType::Category)->group('handle');
Anya Dit's user avatar
  • 356
3 votes

How can I set the HTTP status code?

HeaderHelper::setHeader(array('status' => 404));
Brad's user avatar
  • 487
3 votes

How to render template instead of throwing an error?

You can see common patterns for this in Craft's own controllers. For example in https://github.com/craftcms/cms/blob/master/src/controllers/EntriesController.php#L499-L507, if there's a problem, you ...
Brad Bell's user avatar
  • 67.4k
3 votes
Accepted

"Template Not Found" error when trying to access a module controller action

You'll either need to use the action url http://localhost/actions/my-module/scrape/push since only those will route to controllers by default or create a custom route to your controller
Robin Schambach's user avatar
3 votes

How can I pass a variable defined in a Controller to my variables?

The shortest answer is "that's not possible," and the slightly longer answer is "that's not possible because Controllers and Variables have no reason to ever interact." If your Controller relies on a ...
Lindsey D's user avatar
  • 24k
3 votes

Get entries based on multiple fields in controller

I haven't tested it but it should give you a pretty good idea on how to set this up. public function actionTest() { $entryQuery = Entry::find()->section('mySection'); // All the element ...
tim-pixeldeluxe's user avatar
3 votes
Accepted

Can you call a controller method from within a plugin/module in Craft 3?

Based on your goal, I don't recommend taking this approach for the technical debt reasons in my comment, however your actual question is still a valid one. You can call any controller's method from a ...
James Smith's user avatar
  • 5,164
3 votes
Accepted

Prevent adding users with same email addresses

You're passing false as the second parameter of your saveElement call... that parameter means you're skipping the built-in validation which should catch this error for you. If you get rid of that you ...
James Smith's user avatar
  • 5,164
3 votes
Accepted

How to handle custom PHP logic in CraftCMS

The short answer is ... you don't. There will never be anything like controllers in your templates folder, because that folder should only contain template files. In order to use custom logic in Craft ...
Paul's user avatar
  • 6,338
2 votes

Controller action redirection when called via route

In case you need this for CraftCMS version 3, as I did today, the code slightly changed: $this->redirect(Craft::$app->getRequest()->referrer); This works fine with version 3.
Luke's user avatar
  • 521
2 votes

Dashboard Widget: Access widget settings from controller / service

I'm a bit late to the party here, but for future reference you can get the widget id by looking at the data-id attribute of the widget HTML, or by using $this->model->id in the widget code.
Gaz's user avatar
  • 201
2 votes
Accepted

How do I properly access plugin controllers from the front-end?

Found the solution here: Controller action works but still gives 404 I wasn't returning JSON in the controller, so Craft was trying to complete the request and getting a 404. I ended up removing the ...
GTerrill's user avatar
  • 311
2 votes

How do I properly access plugin controllers from the front-end?

Rather than submitting the form, try hitting it with the Craft JS object. Per the docs: var data = { // ... }; Craft.postActionRequest('cocktailRecipes/ingredients/saveIngredient', data, ...
Ben Wilkins's user avatar
2 votes

Does CSRF protection for controller actions require POST?

Chris is quite correct – CSRF does not work with GET requests (see this answer by Brad Bell for an explanation). I'm posting my workaround as an additional answer in case anyone else ever needs to ...
Mats Mikkel Rummelhoff's user avatar

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