For anyone who is familiar with Sage 10, I am trying to get View Composers working outside of the default app/View/Composers
directory. I have another directory Composers
inside the parent directory of the Sage 10 directory and loaded it in correctly through PSR-4. This is working fine.
However, it does not seem to work. I wonder if I have to register my 'custom' Composer directory somewhere, but the documentation on Sage 10 is very shallow to non-existent.
So in the parent of my Sage dir, there is a Composers dir that gets loaded though PSR-4 containing an Application.php
which looks like this:
<?php
namespace Composers;
use \Roots\Acorn\View\Composer;
class Application extends Composer
{
/**
* List of views served by this composer.
*
* @var array
*/
protected static $views = [
'partials.header',
];
/**
* Data to be passed to view before rendering.
*
* @return array
*/
public function with()
{
return [
'someVar' => $this->sayHello(),
];
}
public function sayHello()
{
return 'Hello from Composer';
}
}
I have tried adding the view composer in the view config of Sage 10 by adding (without any luck)
'composers' => [
Composers\Application::class,
],
Does anyone know where to 'register' these custom Composers? I have to note that PSR-4 is autoloading the classes correctly.
Edit The Sage 10 repository can be found here: https://github.com/roots/sage
I was searching for this answer and figured it out. In my use case, I needed to extract data from my root
App
composer through a method I created. Similar to yoursayHello()
public function. I needed it in a PHP file outside of Sage 10's rootapp
directory. It was to overwrite a plugin's (The Events Calendar) template file.I realized, that this
App
composer was just a class object... and sage does a TON ofuse
statement imports. So at the top of my PHP file I added:This loaded the class into my PHP file with no issues. Next, I attempted to use it in the following way:
This worked! The data returned from my function was saved to
$globalSocialMediaNetworkLinks
. As a result, given your example... you could do the following:Hope this helps!