Search code examples
phphelperconcrete5

Concrete5 package helper - How to fix Fatal error: Call to undefined method NavigationHelper::getURLSegment() error?


Sorry, we've looked at a million answers but we either don't understand them or they don't apply.


Navigation helper:

We have a concrete5 package and have created a helper file at: /packages/package-name/helpers/navigation.php

It contains the following:

<?php defined('C5_EXECUTE') or die('ACCESS DENIED') ?>

<?php

class NavigationHelper {

    // Get URL segments
    public function getURLSegments() {
        return explode("/", parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
    }

    // Get a specific URL segment
    public function getURLSegment($n) {
        $segs = $this->getURLSegments();
        return count($segs) > 0 && count($segs) >= ($n-1)?$segs[$n]:'';
    }
}

Loading the helper

Then we have an element file where we load the package helper like this:

$navHelper = Loader::helper('navigation','package-name');

Calling a method

But when we try to call a method like this:

if ($navHelper->getURLSegment(1) == 'whats-on') {
    echo "What's on";
}

We see:

Fatal error: Call to undefined method NavigationHelper::getURLSegment()

We've been starring at the method and package names for an hour so are obviously missing something basic - what are we missing?

Any help would be much appreciated.

Cheers

Ben


Solution

  • For anyone else having this problem, custom tool files can't have the same file name as C5 tools.

    navigation.php was already being used by C5 so changing the file name of my custom file made this all work.