Function in PageController

Silverstripe Version: 4.3.0

Question:Why can not I show a function from PageController in all PageTypes?

In SS3, I often built a page type footer and loaded it into all pages via the page controller.

PageController.php:

<?php

namespace {

    use SilverStripe\CMS\Controllers\ContentController;

    class PageController extends ContentController
    {

        protected function init()
        {
            parent::init();
        }

        public function FooterStuff()
        {
            return FooterPage::get();
        }
    }
}

Footer.ss:

<footer class="footer"><p>My FooterContent:</p>
	<% loop $FooterStuff() %>
	$Content
	<% end_loop %>
</footer>

Thanks in advance for an answer

Do your other controller classes extend PageController? It’s a common mistake (for me at least) to accidentally extend ContentController instead, in which case you wouldn’t have access to FooterStuff().

Thank you JonoM
On the basis of the post: [Making a global function in PageController.php] (Making a global function in PageController.php)
I read your post forward and backward
Did I create a new SS installation without any additional controllers.
(If the page controller is expanded, this function works without problems - but I need the function in all page types)

Can it be another mistake with namespaces to use?

Can you post the code for a controller that is not working please and I’ll see if I can spot anything.

Directory content in > app > src > FooterPage.php / Page.php / PageController

FooterPage.php:
<?php

namespace SilverStripe\SSBootstrap;

use SilverStripe\Forms\DateField;
use SilverStripe\Forms\TextareaField;
use SilverStripe\Forms\TextField;
use Page;

class FooterPage extends Page
{
private static $db = array(
‘Date’ => ‘Date’,
‘SocialFacebookLink’ => ‘Text’,
‘SocialLinkedInLink’ => ‘Text’,
‘SocialTwitterLink’ => ‘Text’,
‘SocialYoutubeLink’ => ‘Text’,
‘SocialSoundcloudLink’ => ‘Text’,
);

private static $table_name = 'FooterPage';
private static $icon = "images/footer-page.png";
private static $description = 'Pagetyp for editing the footer';
private static $singular_name = 'Layout: Footer content';
private static $plural_name = 'Layout: Footer contents';


public function getCMSFields()
{
    $fields = parent::getCMSFields();
    
    $fields->addFieldToTab('Root.Main', DateField::create('Date','Datum in der Fusszeile'), 'Content');
    $fields->addFieldToTab('Root.Main', TextField::create('SocialFacebookLink','Link auf Facebook'),'Content');
    $fields->addFieldToTab('Root.Main', TextField::create('SocialLinkedInLink','Link auf LinkedIn'),'Content');
    $fields->addFieldToTab('Root.Main', TextField::create('SocialTwitterLink','Link auf Twitter'),'Content');
    $fields->addFieldToTab('Root.Main', TextField::create('SocialYoutubeLink','Link auf Youtube'),'Content');
    $fields->addFieldToTab('Root.Main', TextField::create('SocialSoundcloudLink','Link auf Soundcloud'),'Content');

    return $fields;
}

}

Page.php:
<?php

namespace {

use SilverStripe\CMS\Model\SiteTree;

class Page extends SiteTree
{
    private static $db = [];

    private static $has_one = [];
}

}

PageController.php:
<?php

namespace {

use SilverStripe\CMS\Controllers\ContentController;


class PageController extends ContentController
{

    private static $allowed_actions = [];

    protected function init()
    {
        parent::init();
    }

    public function FooterStuff()
    {
        return FooterPage::get();
    }
}

}

themes > simple > Includes > Footer.ss

My FooterContent:


<% loop $FooterStuff() %>
$Content
<% end_loop %>

So you have a FooterPage class, do you have a corresponding FooterPageController class that extends PageController?

This might not be the best setup for the functionality you want though, you should only use Pages when you want something to have its own URL and position in the site hierarchy.

I’d suggest you’d be better off putting those social links on your SiteConfig rather than a page type. Check out the docs:

https://docs.silverstripe.org/en/4/developer_guides/configuration/siteconfig/

Thanks for your patience, that’s a very good solution.
There is a lot to learn for me with Silverstripe 4 :slight_smile:

1 Like