Sober controller - Notice: Undefined variable

Hi all

I’m trying to create a custom controller to display at least something in the footer, and it doesn’t bother me, constantly this error - Notice : Undefined variable: exp_name

this is version of Sober “soberwp/controller”: “~2.1.0”,

create file in app/Controllers/Footer.php

<?php

    namespace App\Controllers;

    use Sober\Controller\Controller;

    class Footer extends Controller
    {
    	public function expName()
    	{
    		return get_home_url('/');
    	}
    }

And then I am get var in footer.blade.php

{{ $exp_name }}

Controller matches templates, not filenames. In this case, footer.blade.php is a partial, not a template. The templates that Controller matches are the ones in the WordPress template hierarchy.

If you are comfortable with more involved troubleshooting and want to match filenames, you could try using Composers in the Sage 10 dev branch. Be warned if you do, though, that Sage 10 is still under development, which means it may break and will probably change

1 Like

@alwaysblank

Ok. Thanks

And, if i have custom template, such as template-about-us.blade.php, can I create AboutUs.php controller?

You can create Controllers for custom templates, yes. You controller name will have to match a template name that WordPress recognizes. you can usually figure out what these are by looking at the template-related classes that WordPress adds to the <body>. Controller names will need to be CamelCase, so page-template-page-collaborate would be PageTemplatePageCollaborate for instance.

1 Like

@alwaysblank

Thank a lot!!

This topic was automatically closed after 42 days. New replies are no longer allowed.