Call function in App controller from another controller?

Using SoberWP controller and Sage 9 beta 4.

Is it possible to call a function in the App controller from another controller? If so, what’s the syntax?

I currently have some logic in the App controller (which seems like a logical place for it). But I need to use the results in the logic for some page template controllers too.

If it’s not possible to do, could someone suggest where would be a logical place to put the logic (e.g., MU plugin, somewhere else)?

Thanks.

1 Like

Make the function a static and call it with App::functionName()

In App.php

/**
 * Get Blog Name
 *
 * @return string
 */
public static function blogName()
{
    return get_bloginfo('name');
}

In your other Controller.php

public function example()
{
    return App::blogName();
}
3 Likes

Thanks. Will check it out.

Is there any way to call it if not static?

Why not use a trait? That’s what they’re used for, then you call it on the class itself.

2 Likes

Don’t call a method from another Controller… just create a Trait that has that method if it’s similar for both controllers or a helper function if it’s better

1 Like

Thx for the comments on Traits. Sounds like either the static fuction or the trait would be a good approach.

A static function, perhaps, but not from another controller, if you do that you’re basically #doingitwrong

1 Like