View Composers issue in Sage 10

I’m currently trying to register a simple View Composer in Sage 10 called Name.php - its just a basic array:

namespace App\Composers\Partials;

use Roots\Acorn\View\Composer;

class Name extends Composer
{
    /**
     * List of views served by this composer.
     *
     * @var array
     */
    protected static $views = [
      '*',
  ];

  public function Names()
  {
    $names = array(
      ['forename' => 'John', 'surname' => "Smith"],
      ['forename' => 'Peter', 'surname' => "Jones"],
    );
    return $names;
  }
}

and in my view, passing the results via foreach loop:

<ul>
  @foreach ($names as $name)
    <li>{{ $name['forename'] }} {{ $name['surname'] }}</li>
  @endforeach
</ul>

I’ve also tried var dumping the array but getting ‘null’:

<?php
var_dump($names);
?>

Any solutions to why this wouldn’t return the data? Thanks

You aren’t passing the values to the view. You need to use with() or override():

public function with()
{
  return [
    'names' => $this->names(),
  ];
}
1 Like

and remove Partials from your namespace

1 Like

Thanks for the reply, I’ve tried this, but still not working as expected, and returning null:

namespace App\Composers;
use Roots\Acorn\View\Composer;

class Name extends Composer
{
  public static $views = [ '*' ];

  public function with()
  {
    $names = array(
      ['forename' => 'John', 'surname' => "Smith"],
      ['forename' => 'Peter', 'surname' => "Jones"],
    );
    return [
      'names' => $this->names(),
    ];
  }
}

This method (names()) doesn’t appear in your code now, so it won’t return anything. You create a $names variable. Why aren’t you returning that?

I’m really sorry for the basic queries on this, but if you have the time would you please be able to show me what that function would look like with a variable?

I had checked the docs on these composers but struggled to undertstand it fully.

I had previously used Controllers in Sage 9, but struggling with it in Sage 10.

Thanks in advance.

namespace App\Composers;
use Roots\Acorn\View\Composer;

class Name extends Composer
{
  public static $views = [ '*' ];

  public function with()
  {
    return [
      'names' => $this->names(),
    ];
  }

  public function names()
  {
    return [
      ['forename' => 'John', 'surname' => "Smith"],
      ['forename' => 'Peter', 'surname' => "Jones"],
    ];
  }
}

Or you could do this (they’re identical in functionality):

namespace App\Composers;
use Roots\Acorn\View\Composer;

class Name extends Composer
{
  public static $views = [ '*' ];

  public function with()
  {
    return [
      'names' =>  [
        ['forename' => 'John', 'surname' => "Smith"],
        ['forename' => 'Peter', 'surname' => "Jones"],
      ],
    ];
  }
}

Then in your blade:

@foreach($names as $name)
  {{ $name['forename'] }} {{ $name['surname'] }}
@endforeach
1 Like

Thanks for outlining the code but unfortunately still nothing in the view for either method, would there be somewhere I need to register that composer or is it automatic?

My mistake, I didn’t look at your namespace. Views are loaded as per PSR-4, so your namespace needs to be namespace App\View\Composers;

Thats it working. Thanks so much, really appreciate that.

Here is final code for anyone with same query:

<?php

namespace App\View\Composers;
use Roots\Acorn\View\Composer;

class Name extends Composer
{
  public static $views = [ '*' ];

  public function with()
  {
    return [
      'names' =>  [
        ['forename' => 'John', 'surname' => "Smith"],
        ['forename' => 'Peter', 'surname' => "Jones"],
      ],
    ];
  }
}

then in view:

@foreach($names as $name)
{{ $name['forename'] }} {{ $name['surname'] }}
@endforeach

Using the wp acorn make:composer WP-CLI command can help a lot with building composers.

1 Like

Thanks, will look into that. :+1:

This topic was automatically closed after 42 days. New replies are no longer allowed.