Search code examples
wordpressroots-sage

Outputting A Custom Blade Partial from Sober Controller


So, for my template, I'm creating a block of code that runs custom "Modules". Basically, they are a custom post type, with fields built in ACF. Each of these modules has their own Blade template (e.g. /resources/views/modules/brandwindow.blade.php).

I've also created a Blade template for a custom post type called "Modular Page". On this template, I handle all the calls for each and any module called in a loop. I want to be able to handle this login in a controller, however. So, I created a Controller called "LoadModules" that will handle that request.

I'm having some difficulty calling a Blade template from a Controller, though. In a function GetModules(), I loop through the ACF repeater to see which module I need to load, and then supply all the relevant fields with another Controller specific to that Module (e.g. if module = 'bwindow', Call BrandWindow->BrandWindowFields). This returns an array which I can then pass into a View.

I've added the View class to the top of the Controller (like you would in Laravel), but I'm pretty sure I'm doing something wrong here. I'm getting the following error: Class 'View' not found in ..\app\Controllers\LoadModules.php on line 25

Here's my Controller code:

public static function GetModules()
{
    $brand_window_loader = new BrandWindow;
    $module_array = get_sub_field('content_type');
    if ($module_array == 'bwindow') {
        // Do Brand Window Stuff
        $windows = $brand_window_loader->GetBrandWindows();
        $output = '';
        foreach ( $windows as $window ){
            $feild_array = $brand_window_loader->BrandWindowFields($window);
            $output .= View::make('modules.brandwindow', $feild_array);
        }
        return $output;
    } elseif ($module_array == 'blogfeed') {
        // Do Blog Feed Stuff
        return $module_array;
    }
}

Line 25, is throwing the error which is:

$output .= View::make('modules.brandwindow', $feild_array);

The $brand_window_loader->BrandWindowFields($window) is just an array of keyed values it pulls from ACF using the ID of the post.

'title' => 'Title',
'sub_text' => 'Sub Text',
'url' => 'http://example.com'
ETC.

The error is obviously related to the "View" class not existing, so I'm wondering if I've namespaced or the View class does not exist with Sage 9/Bedrock. If that's true, what is the best way to include a Blade template from a Controller?

Thanks


Solution

  • You can try :

    $output .= App\template('modules.brandwindow', $feild_array)