PHPackages                             itcig/sagecontroller - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. itcig/sagecontroller

ActivePackage[Framework](/categories/framework)

itcig/sagecontroller
====================

WordPress package to enable a basic controller when using Timber/Twig with Sage 9

2.0.5(2y ago)21.5k11MITPHPPHP &gt;=5.6.0

Since Feb 17Pushed 1y ago2 watchersCompare

[ Source](https://github.com/itcig/sagecontroller)[ Packagist](https://packagist.org/packages/itcig/sagecontroller)[ Docs](https://github.com/itcig)[ RSS](/packages/itcig-sagecontroller/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (4)Versions (26)Used By (1)

Controller
==========

[](#controller)

WordPress package to enable a controller when using Timber/Twig with [Sage 9](https://roots.io/sage/). This also requires heavy modification of Sage 9 to use.

Installation
------------

[](#installation)

#### Composer:

[](#composer)

**Please note that Controller is no longer an mu-plugin and is now a Composer theme depedency.**

Browse into the Sage theme directory and run;

```
$ composer require itcig/sagecontroller:2.0.2
```

#### Requirements:

[](#requirements)

- [PHP](http://php.net/manual/en/install.php) &gt;= 7.0

Setup
-----

[](#setup)

By default, create folder `app/Controllers/` within your theme directory.

Alternatively, you can define a custom path using the filter below within your themes `resources/functions.php` file;

```
add_filter('sober/controller/path', function () {
    return dirname(get_template_directory()) . '/app/Custom-folder';
});
```

The controller will autoload PHP files within the above path and its subdirectories.

Usage
-----

[](#usage)

#### Creating a basic Controller:

[](#creating-a-basic-controller)

- Controller files follow the same hierarchy as WordPress.
- Extend the Controller Class— it is recommended that the class name matches the filename.
- Create methods within the Controller Class;
    - Use `public function` to expose the returned values to the Twig views/s.
    - Use `public static function` to use the function within your Twig view/s.
    - Use `protected function` for internal controller methods as only public methods are exposed to the view. You can run them within `__construct`.
- Return a value from the public methods which will be passed onto the Twig view.
    - **Important:** The method name is converted to snake case and becomes the variable name in the Twig view.
    - **Important:** If the same method name is declared twice, the latest instance will override the previous.

#### Examples:

[](#examples)

The following example will expose `$images` to `resources/views/single.twig`

**app/controllers/Single.php**

```
