Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

Naming with underscores #49

Closed
matbrady opened this issue Sep 7, 2017 · 12 comments
Closed

Naming with underscores #49

matbrady opened this issue Sep 7, 2017 · 12 comments

Comments

@matbrady
Copy link

matbrady commented Sep 7, 2017

Sorry if this issue already exists. The only similar one I was able to find was #20.

Problem

Controller loading doesn't occur with custom post types using underscores.
Example: resource_collection

Details

I have a custom post type of resource_collection. It's relevant files include:

  • view resources/views/partials/content-single-resource_collection.blade.php
  • controller app/controllers/SingleResourceCollection.php
    <?php
    namespace App;
    use Sober\Controller\Controller;
    
    class SingleResourceCollection extends Controller
    {
    . . . 
    

Hierarchy Debugger:

controllers/app.php
controllers/index.php
controllers/singular.php
controllers/single.php
controllers/single-resource_collection.php

Thanks for any help/clarification!

@darrenjacoby
Copy link
Member

I will see what we can do here and let you know.

@zacksmash
Copy link

Bump on this. Ran into the same issue.

@sulfo
Copy link

sulfo commented Oct 10, 2017

Aaaaand the same here.

@darrenjacoby
Copy link
Member

Just a note that I am looking into this.

@garethpbk
Copy link

Same problem here, I'm using the Single.php controller for now. Thanks for looking into it!

@webstractions
Copy link

webstractions commented Nov 11, 2017

How about something like this?

Add $template property to Sober\Controller\Controller

<?php

namespace Sober\Controller;

class Controller
{
    protected $active = true;
    protected $tree = false;
    protected $data = [];
    
    // Default is null (not set)
    protected $template = false;

    ...

    public function getTemplate() {
        return $this->template;
    }
}

controller.php loader() function

function loader()
{
    $loader = new Loader();
    foreach ($loader->getData() as $template => $class) {

        $instance = new $class; // instantiate class...
        if ($instance->getTemplate()) { // ...so we can access its methods
            $template = $instance->getTemplate();
        }
        // Pass data filter
        add_filter('sage/template/' . $template . '-data/data', function ($data) use ($loader, $class) {
            $controller = new $class();
            $controller->__setup();
            return array_merge($loader->getAppData(), $loader->getPostData(), $controller->__setTreeData($data), $controller->__getData());
        });
        // Class alais
        class_alias($class, (new \ReflectionClass($class))->getShortName());
    }
}

Now you can over-ride the template name inside of your controllers.

class SingleResourceCollection
{
    protected $template = 'partials/content-single-resource_collection';
}

@zacksmash
Copy link

That looks great! Nice solution!

@darrenjacoby
Copy link
Member

darrenjacoby commented Nov 11, 2017

Earlier versions of Controller had the option to specify the template. I think that is the best solution for underscores. I'll add this to dev-master.

Thanks @webstractions

@webstractions
Copy link

@darrenjacoby You're welcome.

@alwaysblank
Copy link

@webstractions's solution works great! Had to make one tweak to get it there, though:

$instance = new $class; // instantiate class...
if ($instance->getTemplate()) { // ...so we can access its methods
      $template = $instance->getTemplate();
}

Without that, I got errors about trying to call a method on a string. Works like a charm now, though! Thanks!

@webstractions
Copy link

@alwaysblank Looks good! Updated loader() code above to match.

@darrenjacoby
Copy link
Member

I'll be adding this to dev-master soon.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants