WordPress plugin to enable a basic controller when using Blade with Sage 9.
Important: If you're upgrading from beta to 1.0.0 and using use Sober\Controller\Tree;
please change to Sober\Controller\Module\Tree;
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 src/controllers/
within your 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.
- Controller files follow the same hierarchy as WordPress.
- You can view the controller hierarchy by using the Blade directive
@debug('hierarchy')
on any template or inspecting body classes ending with *-data.
- You can view the controller hierarchy by using the Blade directive
- Extend the Controller Class—the class name does not have to match the template name but it is recommended.
- 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.
- Important: If the same method name is declared twice, the latest instance will override the previous.
The following example will expose $images
to templates/single.blade.php
src/controllers/single.php
Note: You can also use camel case for Controller class file names (eg. Single.php)
<?php
namespace App;
use Sober\Controller\Controller;
class Single extends Controller
{
/**
* Return images from Advanced Custom Fields
*
* @return array
*/
public function images()
{
return get_field('images');
}
}
templates/single.blade.php
@if($images)
<ul>
@foreach($images as $image)
<li><img src="{{$image['sizes']['thumbnail']}}" alt="{{$image['alt']}}"></li>
@endforeach
</ul>
@endif
You can also create reusable components and include them in a template using PHP traits.
src/controllers/partials/images.php
<?php
namespace App;
trait Images
{
public function images()
{
return get_field('images');
}
}
You can now include the Images trait into any template to pass on variable $images;
src/controllers/single.php
<?php
namespace App;
use Sober\Controller\Controller;
class Single extends Controller
{
use Images;
}
By default, each Controller overrides its template heirarchy depending on the specificity of the Controller (the same way WordPress templates work).
You can inherit the data from less specific Controllers in the heirarchy by implementing the Tree.
For example, the following src/controllers/single.php
example will inherit methods from src/controllers/singular.php
;
src/controllers/single.php
<?php
namespace App;
use Sober\Controller\Controller;
use Sober\Controller\Module\Tree;
class Single extends Controller implements Tree
{
}
If you prefer you can also do this;
<?php
namespace App;
use Sober\Controller\Controller;
class Single extends Controller
{
protected $tree = true;
}
You can override a src/controllers/singular.php
method by declaring the same method name in src/controllers/single.php
;
Methods created in src/controllers/base.php
will be inherited by all templates and can not be disabled as templates/layouts/base.php
extends all templates.
src/controllers/base.php
<?php
namespace App;
use Sober\Controller\Controller;
class Base extends Controller
{
public function siteName()
{
return get_bloginfo('name');
}
}
protected $active = false;
In your Blade templates, you can use the following to assist with debugging;
@debug('hierarchy')
echos a list of the controller hierarchy for the current template.@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