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 ...
user avatar
  • 66.4k
9 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 ...
user avatar
  • 267
8 votes
Accepted

Controller action works but still gives 404

You're definitely on the right track, just missing out on a couple of key details! The actionUrl function generates a normal, full URL... it's not (by default) an AJAX call. So your browser is ...
user avatar
  • 23.6k
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 = \...
user avatar
  • 10.9k
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 ...
user avatar
  • 6,146
7 votes
Accepted

Cannot post data to a plugin controller action

There's alot of different things that could be going on here. First, if your action is named actionPayWithPaypal (as in the code above) the action url should be paypalPayments/payment/payWithPaypal I ...
user avatar
  • 7,228
7 votes
Accepted

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

To quote @Brandon from here: Yep you just need to include a $variables argument in your controller function, typeset to an array: public function actionArchive(array $variables = array()) { $...
user avatar
7 votes
Accepted

Is the "registerSiteRoutes" hook screwing up my template path?

Craft will only check for templates in your plugin’s templates/ folder during CP and action requests. When you start routing to your controller action via a site-facing route, it’s no longer either ...
user avatar
7 votes
Accepted

Rendering templates from plugin controller

Figured it out! I went ahead and took at look at how Craft's renderTemplate method works which cleared it up for me. A few things... Craft caches CSS/JS resources inside of getHeadHtml() and ...
user avatar
  • 345
6 votes

How can I set the HTTP status code?

I believe HeaderHelper::setHeader() is what you're looking for in the context of a plugin.
user avatar
  • 66.4k
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);
user avatar
6 votes
Accepted

How to tell if user exists

Something like this should work: public function add($user) { $existingUser = craft()->users->getUserByUsernameOrEmail($user->username); if ($existingUser) { // They ...
user avatar
  • 66.4k
6 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 ...
user avatar
  • 66.4k
5 votes
Accepted

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

If you use $model->getErrors() instead, it should return a multidimensional array of errors indexed by attribute name like so: array( 'email' => array( 'First email error.', ...
user avatar
  • 66.4k
5 votes
Accepted

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

actionUrl() and UrlHelper::getActionUrl() always force the script name to show up despite the omitScriptNameInUrls config setting for two reasons: Action URLs are generally not actually visible to ...
user avatar
5 votes
Accepted

Constructor in custom controller?

I'm not a huge fan of it but for this situation it might be more appropriate to use init(), Looking at Craft\BaseController: /** * Initializes the controller. This method is called by the Craft ...
user avatar
  • 4,361
5 votes
Accepted

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

Just an idea, but are you Allowing Anonymous Access to Actions? Maybe you are logged in on your Desktop, but not on your iPhone. Example: protected $allowAnonymous = true;
user avatar
  • 8,316
5 votes
Accepted

Should my controllers actions 'return' when rendering templates?

There’s no reason for your controller to return anything – it will never be checked. Many requests call more than one controller action, so it would be pretty presumptuous for Craft to think that it ...
user avatar
5 votes
Accepted

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

You are currently performing a SELECTinstead of an UPDATE so no data will be updated. I'm guessing that entryId is the primary key of the record you are trying to update? If so, I would suggest you ...
user avatar
  • 1,623
5 votes
Accepted

Best way to use custom enums in my plugin templates?

Right now that's probably the best way to do it. For Craft 2.2, I just added a static getConstants() method to BaseEnum, so you can do can do a PluginStatus::getConstants() call that will return an ...
user avatar
  • 66.4k
5 votes

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

From what I've seen so far of Craft and plugins developed for it, the requests are typically forwarded directly to Twig templates and those templates are responsible for analysing query strings and ...
user avatar
  • 66.4k
5 votes
Accepted

Assets indexing from plugin controller

I guess you would need to have your own method in your controller to fire up the indexing tool. From what it looks like, you need a few things to get going: The classname of the tool i.e AssetIndex ...
user avatar
  • 4,509
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: $...
user avatar
  • 66.4k
5 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 ...
user avatar
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....
user avatar
4 votes
Accepted

Direct access to controller

As that page in the docs says, dedicated controller action URLs will look like: http://example.com/actions/pluginHandle/actionName If your plugin’s name is “CocktailRecipes”, and you have a ...
user avatar
4 votes

Direct access to controller

The docs explain how to generate links to controller actions. Assuming your action trigger is "actions" (as it is by default) then your URL will look as follows: http://www.domain.com/actions/...
user avatar
  • 6,146
4 votes

How do I access AJAX data within a controller method?

Any properties of the object you pass into Craft.postActionRequest()’s second argument will be available to Craft as POST parameters, so you can get to them with HttpRequestService::getPost(): $sData ...
user avatar
4 votes

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

My question is, is this a typical and recommended approach to using Craft, or should I be using controllers to handle tasks such as filtering and fetching data and passing it to a view? This is a ...
user avatar
4 votes

Call Controller Method with Plugin

Something like this should work: `craft()->runController('pluginHandle/controllerId/action');` Although I'd question the decisions you've made to get to the point where you had to ask this ...
user avatar
  • 66.4k

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