Answers.Body: <p>I think you should use <code>{{ actionUrl('myplugin/products/get-products') }}</code>.</p>
  
  <p>But in a form, it's maybe even better/easier to use:</p>
  
  <pre><code>&lt;input type="hidden" name="action" value="myplugin/products/get-products" /&gt;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-04T12:49:39.680Z
Answers.Text: Unfortunately neither of those worked initially.  When performing that request from the front-end it works OK. But I have worked out what is wrong and can get it working using the latter suggestion.
Answers.UserDisplayName: foamcow
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-04T09:28:10.853Z
Answers.LastActivityDate: 2018-12-04T09:28:10.853Z
Answers.UserDisplayName: Paul
Answers.Body: <p>The problem was two-fold and partly related to my plugin requiring POST data.</p>
  
  <p>If I set the URL to <code>myplugin/products/get-products</code> then it hits the correct controller.</p>
  
  <p>I then set the form method to <code>post</code> and added the necessary <code>{{ csrfInput() }}</code> tag to generate the CSRF fields in the form.</p>
  
  <p>Using <code>{{ actionUrl() }}</code> still doesn't work but I figured that's because it creates a GET request.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-04T12:58:28.347Z
Answers.Text: ActionUrl() does not create a get request. It creates a URL. If you'd use that and set the form's method to POST, it would work.
Answers.UserDisplayName: Paul
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-04T18:39:45.993Z
Answers.Text: I used the incorrect terminology. It creates a url with querystring parameters such as index.php?p=admin/actions/myplugin/products/get-products. Adding a method parameter of POST to the form didn't seem to make a difference to that generated URL.
Answers.UserDisplayName: foamcow
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-04T12:54:21.067Z
Answers.LastActivityDate: 2018-12-04T12:54:21.067Z
Answers.UserDisplayName: foamcow
Body: <p>I'm working on my first Craft 3 plugin and want to add some buttons to my plugin's control panel section which post form data to a controller.</p>
  
  <p>My plugin is called 'myplugin', the controller I wish to post to is called 'products' and the method in that controller is 'get-products'. I figured I should therefore create the URL on the forms as </p>
  
  <pre><code>{{ actionUrl('products/get-products') }}
  </code></pre>
  
  <p>However this yields a 404 error</p>
  
  <pre><code>HTTP 404 – Not Found – yii\web\NotFoundHttpException
  Page not found.
  ↵
  Caused by: Invalid Route – yii\base\InvalidRouteException
  Unable to resolve the request "https:/local.mydomain.co.uk/index.php?p=admin/actions/products/get-products".
  in /Users/pete/projects/myclient/dev/build/vendor/yiisoft/yii2/base/Module.php at line 537
  </code></pre>
  
  <p>If I call the URL directly by using <code>https://local.mydomain.co.uk/admin/actions/myplugin/products/get-products</code> then it behaves as expected.</p>
  
  <p>Obviously I'm missing some really important bit of knowledge here and would love if someone could help me understand what's going on.</p>
  
Tags: <plugin-development><craft3><error><controller>
Title: Action URLs from CP to a Craft 3 plugin controller
ThreadText: # Action URLs from CP to a Craft 3 plugin controller
  
  (Asked by: username_0 on 2018-12-04)
  
  I'm working on my first Craft 3 plugin and want to add some buttons to my plugin's control panel section which post form data to a controller.
  
  My plugin is called 'myplugin', the controller I wish to post to is called 'products' and the method in that controller is 'get-products'. I figured I should therefore create the URL on the forms as
  
  ```
  {{ actionUrl('products/get-products') }}
  
  ```
  
  However this yields a 404 error
  
  ```
  HTTP 404 – Not Found – yii\web\NotFoundHttpException
  Page not found.
  ↵
  Caused by: Invalid Route – yii\base\InvalidRouteException
  Unable to resolve the request "https:/local.mydomain.co.uk/index.php?p=admin/actions/products/get-products".
  in /Users/pete/projects/myclient/dev/build/vendor/yiisoft/yii2/base/Module.php at line 537
  
  ```
  
  If I call the URL directly by using `https://local.mydomain.co.uk/admin/actions/myplugin/products/get-products` then it behaves as expected.
  
  Obviously I'm missing some really important bit of knowledge here and would love if someone could help me understand what's going on.
  
  **Tags:** plugin-development, craft3, error, controller
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2018-12-04. Score: 2
  
  I think you should use `{{ actionUrl('myplugin/products/get-products') }}`.
  
  But in a form, it's maybe even better/easier to use:
  
  ```
  <input type="hidden" name="action" value="myplugin/products/get-products" />
  
  ```
  
  > **Comments:**
  > **username_0**: Unfortunately neither of those worked initially.  When performing that request from the front-end it works OK. But I have worked out what is wrong and can get it working using the latter suggestion.  
  
  ---
  
  ### Answer by username_0 on 2018-12-04. Score: 2
  
  The problem was two-fold and partly related to my plugin requiring POST data.
  
  If I set the URL to `myplugin/products/get-products` then it hits the correct controller.
  
  I then set the form method to `post` and added the necessary `{{ csrfInput() }}` tag to generate the CSRF fields in the form.
  
  Using `{{ actionUrl() }}` still doesn't work but I figured that's because it creates a GET request.
  
  > **Comments:**
  > **username_1**: ActionUrl() does not create a get request. It creates a URL. If you'd use that and set the form's method to POST, it would work.  
  > **username_0**: I used the incorrect terminology. It creates a url with querystring parameters such as index.php?p=admin/actions/myplugin/products/get-products. Adding a method parameter of POST to the form didn't seem to make a difference to that generated URL.  
  
  ---
  
  