Skip to content

WordPress plugin to enable a basic controller when using Blade with Sage 9

License

Notifications You must be signed in to change notification settings

crixlet/controller

 
 

Repository files navigation

Controller

WordPress plugin to enable a basic controller when using Blade with Sage 9.

Installation

Composer:

Recommended method; Roots Bedrock and WP-CLI

$ composer require soberwp/controller
$ wp plugin activate controller

Manual:

  • Download the zip file
  • Unzip to your sites plugin folder
  • Activate via WordPress

Requirements:

Setup

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.

Usage

Creating a Controller:

  • 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.
  • 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.

Example:

The following example will expose $images to templates/single.blade.php

controllers/single.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');
    }
}

templates/single.blade.php

@if($images)
  <ul>
    @foreach($images as $image)
      <li><img src="{{$image['sizes']['thumbnail']}}" alt="{{$image['alt']}}"></li>
    @endforeach
  </ul>
@endif

Template Option:

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

controllers/images.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');
    }
}

Disable Option;

class Images extends Controller
{
    public $active = false;
}

Blade Directives;

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.

Updates

Composer:

  • Change the composer.json version to ^1.0.0**
  • Check CHANGELOG.md for any breaking changes before updating.
$ composer update

WordPress:

Includes support for github-updater to keep track on updates through the WordPress backend.

Social

About

WordPress plugin to enable a basic controller when using Blade with Sage 9

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 97.6%
  • HTML 2.4%