Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
C

controller

> 编程语言
Open source

Composer package to enable a controller when using Blade with Sage 9

365 stars0 likes0 views
WebsiteGitHub

About

Composer package to enable a controller when using Blade with Sage 9

Controller

WordPress package to enable a controller when using Blade with Sage 9 (Please note, Sage 10 uses Composers and not this package.)

  • Installation
  • Setup
  • Usage
    • Overview
    • Basic Controller
    • Using Functions
    • Using Components
    • Inheriting the Tree/Hierarchy
    • Creating Global Properties
    • Advanced Custom Fields Module
    • Template Override Option
    • Lifecycles
    • Disable Option
  • Blade Debugger
  • Blade Coder

Installation

Composer:

Sage ships with Controller. However, should you need to install, browse into the Sage theme directory and run;

bash
$ composer require soberwp/controller:2.1.2

Upgrading to 2.x.x:

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

Requirements:

  • PHP >= 7.0

Setup

By default Controller uses namespace Controllers.

Controller takes advantage of PSR-4 autoloading. To change the namespace, use the filter below within functions.php

php

add_filter('sober/controller/namespace', function () {
    return 'Data';
});

Usage

Overview:

  • Controller class names follow the same hierarchy as WordPress.
  • The Controller class name should match the filename
    • For example App.php should define class as class App extends Controller
  • Create methods within the Controller Class;
    • Use public function to return data to the Blade views/s
      • The method name becomes the variable name in Blade
      • Camel case is converted to snake case. public function ExampleForUser in the Controller becomes $example_for_user in the Blade template
      • If the same method name is declared twice, the latest instance will override the previous
    • Use public static function to use run the method from your Blade template which returns data. This is useful for loops
      • The method name is not converted to snake case
      • You access the method using the class name, followed by the method. public static function Example in App.php can be run in Blade using App::Example()
      • If the same method name is declared twice, the latest instance will override the previous
    • Use protected function for internal methods. These will not be exposed to Blade. You can run them within __construct
      • Dependency injection with type hinting is available through __construct

The above may sound complicated on first read, so let's take a look at some examples to see how simple Controller is to use.

Basic Controller;

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

app/Controllers/Single.php

php
post_title;
    }
}

resources/views/archive.php

php
@extends('layouts.app')

@section('content')

  @while (have_posts()) @php the_post() @endphp
    {{ Archive::title() }}
  @endwhile

@endsection

Using Components;

You can also create reusable components and include them in any Controller class using PHP traits.

app/Controllers/Partials/Images.php

php
data is set up, but before the class methods are run
}

public function __after()
{
    // runs after all the class methods have run
}

Disable Option;

php
protected $active = false;

Blade Debugger;

In your Blade views, resources/views, you can use the following to assist with debugging;

  • @debug
  • @dump(__var__)

Blade Coder;

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__')

Support

  • Follow @withjacoby on Twitter
  • Buy me a beer or pay my rent, paypal.me/darrenjacoby

Updates

  • Change the composer.json version to 2.1.2
  • Check CHANGELOG.md for any breaking changes before updating.
bash
$ composer update

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPbladeblade-templatecontrollerphp

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言