Answers.Body: <p>I was able to get it to use my class via the following in config/app.php</p>
  <pre class="lang-php prettyprint-override"><code>    'container' =&gt; [
          'definitions' =&gt; [
              \craft\elements\Entry::class =&gt; \modules\Entry::class
          ],
      ],
  </code></pre>
  <p>But it doesnt seem like Craft is able to properly load data into it and I get runtime errors. When debugging, the class is instantiated and it uses things like overridden functions correctly, but there is no entry data loaded into it, so it doesnt have an id, section, etc.</p>
  <p>I did this based on the docs at <a href="https://www.yiiframework.com/doc/guide/2.0/en/concept-configurations#application-configurations" rel="nofollow noreferrer">https://www.yiiframework.com/doc/guide/2.0/en/concept-configurations#application-configurations</a> and <a href="https://www.yiiframework.com/doc/guide/2.0/en/concept-di-container" rel="nofollow noreferrer">https://www.yiiframework.com/doc/guide/2.0/en/concept-di-container</a>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-09-03T23:44:14.853Z
Answers.LastActivityDate: 2021-09-03T23:44:14.853Z
Answers.UserDisplayName: Josh Coady
Body: <p>For certain entries I would like to be able to override how Craft generates the URLs to them.
  So I create an Entry Model in my module extending from \craft\elements\Entry and override the getUrl() method there.</p>
  <p>However I did not manage to replace Craft's class with mine.</p>
  <p>How can I achieve this? I found solutions like <code>Yii::$container-&gt;set('craft\elements\Entry', '\modules\Entry');</code> or the same with <code>Craft::</code></p>
  <p>I placed it in my <code>index.php</code>, I tried it in the head of <code>/config/app.php</code>, but nothing happened.</p>
  <p>Any suggestions?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-08-02T13:03:47.070Z
Comments.Text: Did you find a solution?
Comments.UserDisplayName: Volker Andres
Tags: <entry><models>
Title: Override entry.url / entry.getUrl() Method
ThreadText: # Override entry.url / entry.getUrl() Method
  
  (Asked by: username_0 on 2021-03-24)
  
  For certain entries I would like to be able to override how Craft generates the URLs to them.
  So I create an Entry Model in my module extending from \craft\elements\Entry and override the getUrl() method there.
  
  However I did not manage to replace Craft's class with mine.
  
  How can I achieve this? I found solutions like `Yii::$container->set('craft\elements\Entry', '\modules\Entry');` or the same with `Craft::`
  
  I placed it in my `index.php`, I tried it in the head of `/config/app.php`, but nothing happened.
  
  Any suggestions?
  
  **Tags:** entry, models
  
  > **Comments:**
  > **username_1**: Did you find a solution?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2021-09-03. Score: 1
  
  I was able to get it to use my class via the following in config/app.php
  
  ```php
      'container' => [
          'definitions' => [
              \craft\elements\Entry::class => \modules\Entry::class
          ],
      ],
  
  ```
  
  But it doesnt seem like Craft is able to properly load data into it and I get runtime errors. When debugging, the class is instantiated and it uses things like overridden functions correctly, but there is no entry data loaded into it, so it doesnt have an id, section, etc.
  
  I did this based on the docs at <https://www.yiiframework.com/doc/guide/2.0/en/concept-configurations#application-configurations> and <https://www.yiiframework.com/doc/guide/2.0/en/concept-di-container>.
  
  
  ---
  
  