PHPackages                             chameleon2die4/sage9-components - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. chameleon2die4/sage9-components

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

chameleon2die4/sage9-components
===============================

1.0.2(4y ago)012MITPHPPHP ^7.2 || ^8.0

Since Apr 17Pushed 4y ago1 watchersCompare

[ Source](https://github.com/Chameleon2die4/sage9-components)[ Packagist](https://packagist.org/packages/chameleon2die4/sage9-components)[ RSS](/packages/chameleon2die4-sage9-components/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (3)Versions (4)Used By (0)

Sage9 Components
================

[](#sage9-components)

[![Latest Version](https://camo.githubusercontent.com/d096623b91cee85b4d74704b1f7826e36302876f631af59c4271e077176744e0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f7461672f4368616d656c656f6e32646965342f73616765392d636f6d706f6e656e74733f736f72743d73656d766572266c6162656c3d76657273696f6e)](https://github.com/Chameleon2die4/WP-Router/)[![Packagist](https://camo.githubusercontent.com/83f59dff40aae14175c7793779a928b7823073e95bc5f2c7739972f8fae30935/68747470733a2f2f62616467656e2e6e65742f7061636b61676973742f762f6368616d656c656f6e32646965342f73616765392d636f6d706f6e656e74732f6c6174657374)](https://packagist.org/packages/chameleon2die4/wp-router/)[![PHP Version Require](https://camo.githubusercontent.com/0d7ce4161c607f4ea3f548af118f1480c03f0a6a727d91841d7929c57d369921/68747470733a2f2f62616467656e2e6e65742f7061636b61676973742f7068702f6368616d656c656f6e32646965342f73616765392d636f6d706f6e656e74732f)](https://www.php.net/docs.php)[![License](https://camo.githubusercontent.com/5e25f857ea70d6e61023befad1d4195a9196c47ce3e73e4ef77a0d8e2e2e98f3/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d6d69742d626c75652e737667)](https://github.com/Chameleon2die4/WP-Router/blob/master/LICENSE.md)

Add Components controllers support to Sage 9.x.

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

[](#installation)

### Composer:

[](#composer)

Install with [Composer](https://getcomposer.org/). Run in shell:

```
$ composer require chameleon2die4/sage9-components
```

### Requirements:

[](#requirements)

- [PHP](http://php.net/manual/en/install.php) &gt;= 7.2
- [Sage](https://roots.io/sage/) ^9.x

Setup
-----

[](#setup)

By default Controllers uses namespace `Controllers\Components`.

Controller takes advantage of [PSR-4 autoloading](https://www.php-fig.org/psr/psr-4/). To change the namespace, use the filter below within `functions.php`

```
add_filter('sober/components/namespace', function () {
    return 'Data\Partials';
});
```

Usage
-----

[](#usage)

### Overview:

[](#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;

[](#basic-controller)

The following example will expose `$images` to `resources/views/partials/slider.blade.php`

**app/Controllers/Components/Slider.php**

```
