Answers.Body: <p>Solution is to create a custom controller in a module that extends the cart controller.
  This gives the functionality to overwrite the <code>beforeAction</code> method, while keeping all other functionality.</p>
  
  <p>As an example:</p>
  
  <pre><code>&lt;?php
  
  namespace modules\controllers;
  
  use craft\commerce\controllers\CartController;
  
  class FooController extends CartController
  {
      public function beforeAction($action)
      {
          // Your overwritten logic here
          $something = 'test';
  
          return $something;
      }
  }
  </code></pre>
  
  <p>Instead of posting to <code>commerce/cart/update-cart</code> you would then (in the example above) post to  <code>my-module/foo/update-cart</code>. There is some useful documentation in the Craft docs on <a href="https://docs.craftcms.com/v3/extend/module-guide.html" rel="nofollow noreferrer">building a custom module</a> and how to create customer controllers and actions.</p>
  
  <p>[Answered by Nathaniel Hammond Pixel &amp; Tonic]</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-01T12:43:22.823Z
Answers.LastActivityDate: 2020-06-01T12:43:22.823Z
Answers.UserDisplayName: Jai Sandhu
Body: <p>How do you override the beforeAction method of Craft's base controller? I'm disabling CSRF for a specific route.</p>
  
  <pre><code>public function beforeAction($action): bool
  {
      $requestOrigin = \Craft::$app-&gt;getRequest()-&gt;headers-&gt;get('Origin');
      $origin = getenv('ENVIRONMENT') === 'dev' ? getenv('ORIGIN_DEV') :getenv('ORIGIN_PRODUCTION');
  
      / Disable CSRF validation for valid origins
      if ($requestOrigin === $origin) {
        $this-&gt;enableCsrfValidation = false;
      }
  
      if (!parent::beforeAction($action)) {
        return false;
      }
  
      return true;
  }
  </code></pre>
  
  <p>I know that there have been similar questions asked before on overriding plugin methods and there's also the blueprint inside the controller itself. Just struggling to understand how to override the <code>craft\web\Controller</code>'s method for this</p>
  
Tags: <plugin-development>
Title: How to overwrite base controller beforeAction method
ThreadText: # How to overwrite base controller beforeAction method
  
  (Asked by: username_0 on 2020-05-29)
  
  How do you override the beforeAction method of Craft's base controller? I'm disabling CSRF for a specific route.
  
  ```
  public function beforeAction($action): bool
  {
      $requestOrigin = \Craft::$app->getRequest()->headers->get('Origin');
      $origin = getenv('ENVIRONMENT') === 'dev' ? getenv('ORIGIN_DEV') :getenv('ORIGIN_PRODUCTION');
  
      / Disable CSRF validation for valid origins
      if ($requestOrigin === $origin) {
        $this->enableCsrfValidation = false;
      }
  
      if (!parent::beforeAction($action)) {
        return false;
      }
  
      return true;
  }
  
  ```
  
  I know that there have been similar questions asked before on overriding plugin methods and there's also the blueprint inside the controller itself. Just struggling to understand how to override the `craft\web\Controller`'s method for this
  
  **Tags:** plugin-development
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2020-06-01. Score: 1
  
  Solution is to create a custom controller in a module that extends the cart controller.
  This gives the functionality to overwrite the `beforeAction` method, while keeping all other functionality.
  
  As an example:
  
  ```
  <?php
  
  namespace modules\controllers;
  
  use craft\commerce\controllers\CartController;
  
  class FooController extends CartController
  {
      public function beforeAction($action)
      {
          // Your overwritten logic here
          $something = 'test';
  
          return $something;
      }
  }
  
  ```
  
  Instead of posting to `commerce/cart/update-cart` you would then (in the example above) post to `my-module/foo/update-cart`. There is some useful documentation in the Craft docs on [building a custom module](https://docs.craftcms.com/v3/extend/module-guide.html) and how to create customer controllers and actions.
  
  [Answered by Nathaniel Hammond Pixel & Tonic]
  
  
  ---
  
  