Composer package to enable a controller when using Blade with Sage 9
Composer package to enable a controller when using Blade with Sage 9
WordPress package to enable a controller when using Blade with Sage 9 (Please note, Sage 10 uses Composers and not this package.)
Sage ships with Controller. However, should you need to install, browse into the Sage theme directory and run;
$ composer require soberwp/controller:2.1.2Please note that versions 2.x.x are newer releases than 9.x.x-beta. The 9 was used to match Sage 9 versioning at the time.
Controller 2.x.x uses PSR4 autoloading to load Controller classes. This is considered best practice. You will need to update the following files from 9.0.0-beta versions.
Folder controllers/ changes to Controllers/, class file names changes to camelcase App.php and FrontPage.php. Controller namespaces changes to namespace App\Controllers;
By default Controller uses namespace Controllers.
Controller takes advantage of PSR-4 autoloading. To change the namespace, use the filter below within functions.php
add_filter('sober/controller/namespace', function () {
return 'Data';
});App.php should define class as class App extends Controllerpublic function to return data to the Blade views/spublic function ExampleForUser in the Controller becomes $example_for_user in the Blade templatepublic static function to use run the method from your Blade template which returns data. This is useful for loopspublic static function Example in App.php can be run in Blade using App::Example()protected function for internal methods. These will not be exposed to Blade. You can run them within __construct__constructThe above may sound complicated on first read, so let's take a look at some examples to see how simple Controller is to use.
The following example will expose $images to resources/views/single.blade.php
app/Controllers/Single.php
post_title;
}
}resources/views/archive.php
@extends('layouts.app')
@section('content')
@while (have_posts()) @php the_post() @endphp
{{ Archive::title() }}
@endwhile
@endsectionYou can also create reusable components and include them in any Controller class using PHP traits.
app/Controllers/Partials/Images.php
data is set up, but before the class methods are run
}
public function __after()
{
// runs after all the class methods have run
}protected $active = false;In your Blade views, resources/views, you can use the following to assist with debugging;
@debug@dump(__var__)In your Blade views, resources/views, you can use the following to assist with jump-starting coding;
@code@code('__name of variable as string__')To wrap the code in if statements, use @codeif
@codeif@codeif('__name of variable as string__')$ composer updateNo open issues yet, or sync has not completed.