Giter Site home page Giter Site logo

controller's Introduction

Controller

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

Installation

Important: If you're upgrading from beta to 1.0.0 and using use Sober\Controller\Tree; please change to Sober\Controller\Module\Tree;

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

Usage

Creating a basic Controller:

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

Examples:

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

Creating Components;

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;
}

Inheriting the Tree/Heirarchy;

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;

Creating Global Properties;

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');
    }
}

Disable Option;

protected $active = false;

Blade Debugging;

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.

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

controller's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.