chameleon2die4/sage9-components

1.0.2 2022-04-28 09:04 UTC

This package is auto-updated.

Last update: 2024-04-28 13:45:48 UTC


README

Latest Version Packagist PHP Version Require License

Add Components controllers support to Sage 9.x.

Installation

Composer:

Install with Composer. Run in shell:

$ composer require chameleon2die4/sage9-components

Requirements:

Setup

By default Controllers uses namespace Controllers\Components.

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

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

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/partials/slider.blade.php

app/Controllers/Components/Slider.php

<?php

namespace App\Controllers\Components;

use Chameleon2die4\Components\Component;

class Slider extends Component
{
    /**
     * Return images from Advanced Custom Fields
     *
     * @return array
     */
    public function images()
    {
        return get_field('images');
    }
}

resources/views/partials/slider.blade.php

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

Parent:

In parent template use directive

@includePart( string $template, array $data = [] ) 

resources/views/single.blade.php

@extends('layouts.app')

@section('content')
  @while(have_posts()) @php the_post() @endphp
    @include('partials.page-header')
    
    @includePart('partials.slider')
  @endwhile
@endsection

Lifecycles;

Controller Classes come with two lifecycle hooks for greater control.

public function __before()
{
    // runs after this->data is set up, but before the class methods are run
}

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