WordPress plugin to enable a basic controller when using Blade with Sage 9.
Recommended method; Roots Bedrock and WP-CLI
$ composer require soberwp/controller
$ wp plugin activate controller
- Download the zip file
- Unzip to your sites plugin folder
- Activate via WordPress
- PHP >= 5.6.x
By default, create folder controllers/
within your active theme directory.
Alternatively, you can define a custom path using the filter below within your themes functions.php
file;
add_filter('sober/controller/path', function () {
return get_stylesheet_directory() . '/your-custom-folder';
});
The controller will autoload PHP files within the above path and its subdirectories.
- Name the Controller file the same name as the template file or override the
$template
variable. - Extend the Controller Class—the class name does not have to match the template name.
- Create methods within the Controller Class;
- Use
public static function
to expose the returned values to the blade template/s. - Use
protected static function
for internal controller methods as only public methods are exposed to the template.
- Use
- Return a value from the public methods which will be passed onto the blade template.
- Important: the method name is converted to snake case and becomes the variable name in the blade template.
The following example will expose $images
to templates/single.blade.php
<?php
namespace App;
use Sober\Controller\Controller;
class Single extends Controller
{
/**
* Protected methods will not be passed to the template
*/
protected function hidden()
{
}
/**
* Return images from Advanced Custom Fields
*
* @return array
*/
public function images()
{
return get_field('images');
}
}
@if($images)
<ul>
@foreach($images as $image)
<li><img src="{{$image['sizes']['thumbnail']}}" alt="{{$image['alt']}}"></li>
@endforeach
</ul>
@endif
By default, the controller matches the template filename—but you can override the template to target by using;
-
To expose to single template;
public $template = 'single;
-
To expose to multiple templates;
public $template = ['single', 'page'];
-
To expose to all templates;
public $template = 'all';
This allows you to create controllers based around components rather than per template.
The following example will expose $images
to both templates/single.blade.php
and templates/page.blade.php
<?php
namespace App;
use Sober\Controller\Controller;
class Images extends Controller
{
public $template = ['single', 'page'];
/**
* Return images from Advanced Custom Fields
*
* @return array
*/
public function images()
{
return get_field('images');
}
}
class Images extends Controller
{
public $active = false;
}
For debugging purposes;
@debug(controller)
echos a list of variables available in the template.@debug(dump)
var_dumps a list of variables available in the template, including$post
.
- Change the composer.json version to ^1.0.0**
- Check CHANGELOG.md for any breaking changes before updating.
$ composer update
Includes support for github-updater to keep track on updates through the WordPress backend.
- Download github-updater
- Clone github-updater to your sites plugins/ folder
- Activate via WordPress
- Twitter @withjacoby