# Overwrite url() in page model

**URL:** <https://forum.getkirby.com/t/overwrite-url-in-page-model/12974>\
**Category:** Questions\
**Tags:** v3\
**Created:** [February 20, 2019, 11:49am UTC](https://forum.getkirby.com/t/overwrite-url-in-page-model/12974 "2019-02-20T11:49:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![thguenther](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/thguenther/32/16708_2.png) [@thguenther](https://forum.getkirby.com/u/thguenther)\
**Post date:** [February 20, 2019, 11:49am UTC](https://forum.getkirby.com/t/overwrite-url-in-page-model/12974/1 "2019-02-20T11:49:39Z")

</div>

In Kirby 2 I often overwrote the `->url()` method in a page model like this:

```php
<?php
class SectionPage extends Page {
  public function url() {
    return $this->parent()->url() . '#' . $this->slug();
  }
}

```

I can’t get this to work in Kirby 3, though. Here’s the error I get:

```auto
Fatal error: Declaration of SectionPage::url() must be compatible with Kirby\Cms\Page::url($options = NULL)

```

What does “compatible” mean in this context?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 20, 2019, 11:58am UTC](https://forum.getkirby.com/t/overwrite-url-in-page-model/12974/2 "2019-02-20T11:58:11Z")

</div>

```auto

<?php
class SectionPage extends Page {
  public function url($options = null): string {
    return $this->parent()->url() . '#' . $this->slug();
  }
}

```

---

<div class="post-metadata">

**Author:** ![thguenther](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/thguenther/32/16708_2.png) [@thguenther](https://forum.getkirby.com/u/thguenther)\
**Post date:** [February 20, 2019, 12:01pm UTC](https://forum.getkirby.com/t/overwrite-url-in-page-model/12974/3 "2019-02-20T12:01:55Z")

</div>

Awesome, this works! Thank you, Sonja!

For anyone having pages inside a separate `sections` or `modules` container and stumbling over this thread, you can use this:

```php
<?php

class SectionPage extends Page {
	public function url($options = null): string {
		return $this->parents()->filterBy("intendedTemplate", "!=", "sections")->first()->url() . '#' . $this->slug();
	}
}

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 20, 2019, 12:06pm UTC](https://forum.getkirby.com/t/overwrite-url-in-page-model/12974/4 "2019-02-20T12:06:10Z")

</div>

You are welcome.

You always have to check out the original method’s parameters and return type hints when trying to overwrite native methods.

---

<div class="post-metadata">

**Author:** ![tobi](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/tobi/32/6521_2.png) [@tobi](https://forum.getkirby.com/u/tobi)\
**Post date:** [November 19, 2019, 2:27pm UTC](https://forum.getkirby.com/t/overwrite-url-in-page-model/12974/5 "2019-11-19T14:27:31Z")

</div>

I currently have the same problem. I modified the starterkit to only show articles (in subfolders) that are inside the “home” folder. I added a model/home.php with the code above, but the url stays the same -\> [site.com/subfolder-from-site/home/slug-of-the-article](http://site.com/subfolder-from-site/home/slug-of-the-article).

model/home.php

```
<?php
    class HomePage extends Page {
        public function url($options = null): string {
        return $this->slug();
    }
}

```

templates/home.php

```
<?php foreach ($page->children()->listed()->sortBy('date', 'desc') as $article): ?>
    <article class="note">
    <header class="note-header">
        <a href="<?= $article->url() ?>">
         <?= $article->title() ?>
         <time><?= $article->date()->toDate('d F Y') ?></time>
    </a>
    </header>
    </article>
<?php endforeach ?>
```

---

<div class="post-metadata">

**Author:** ![thguenther](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/thguenther/32/16708_2.png) [@thguenther](https://forum.getkirby.com/u/thguenther)\
**Post date:** [November 19, 2019, 2:31pm UTC](https://forum.getkirby.com/t/overwrite-url-in-page-model/12974/6 "2019-11-19T14:31:08Z")

</div>

The overwritten `url()` method only affects pages with the `home` template. If you want to overwrite the article urls, you need a `model/article.php` (depending on the name of your article template).

---

<div class="post-metadata">

**Author:** ![tobi](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/tobi/32/6521_2.png) [@tobi](https://forum.getkirby.com/u/tobi)\
**Post date:** [November 19, 2019, 2:32pm UTC](https://forum.getkirby.com/t/overwrite-url-in-page-model/12974/7 "2019-11-19T14:32:30Z")

</div>

Well, didn’t see this small detail. Now it works fine, thank you! 🙂
