Sage 10 No Controller Help

One thing a I loved about Sage 9 was having a controller that I could use to call functions anywhere just by using App::WhatTheFunc().

I know this process has changed in Sage 10 but where would I add functions that I would want to call now in Sage 10 and ideally globally.

functions.php?

Your question doesn’t specify why you wan to do this. Why do you want to do this? What is your use case? Any function can be called anywhere. Are you looking to namespace things for organizational purposes? You can do that with namespaces, no need for controllers. The purpose of a controller is to pass data to a view, not contain functions.

1 Like

Not sure if it’s ideal, but you can do this with composers by passing $this inside of with().

The following would let you do $app->example() globally within all of your views:

<?php

namespace App\View\Composers;

use Roots\Acorn\View\Composer;

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

    /**
     * Data to be passed to view before rendering.
     *
     * @return array
     */
    public function with()
    {
        return [
            'app' => $this,
        ];
    }

    /**
     * An example method.
     * 
     * @param  string $value
     * @return string
     */
    public function example($value = 'hello world')
    {
        return $value;
    }
}
3 Likes

I was thinking about this. And took a look back at some of my Sage 9 theme sites.
It appears that in all the cases I was using methods on the controllers to build out information for partials, or items of the query on the page or section.

I believe all of that could be rewritten with composers now, because the composer targets the template, including partials, not just the page or page template.

You can access the view data with $this->data[] so you can handle the data passed to partials. And for things like site wide navigation or breadcrumbs you can use a composer that targets just those partials. Then anywhere they are used it will provide the data.

If you just want to have organized logic for anywhere including outside of the blade templates then general namespacing and classes should work for that.