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

Naming with underscores #49

Closed
Closed

Description

@matbrady

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!

Activity

  1. darrenjacoby commented on Sep 10, 2017

    @darrenjacoby
    Member

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

  2. zacksmash commented on Oct 4, 2017

    @zacksmash

    Bump on this. Ran into the same issue.

  3. sulfo commented on Oct 10, 2017

    @sulfo

    Aaaaand the same here.

  4. darrenjacoby commented on Oct 10, 2017

    @darrenjacoby
    Member

    Just a note that I am looking into this.

  5. garethpbk commented on Oct 13, 2017

    @garethpbk

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

  6. webstractions commented on Nov 11, 2017

    @webstractions

    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';
    }
  7. zacksmash commented on Nov 11, 2017

    @zacksmash

    That looks great! Nice solution!

  8. darrenjacoby commented on Nov 11, 2017

    @darrenjacoby
    Member

    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

  9. webstractions commented on Nov 11, 2017

    @webstractions

    @darrenjacoby You're welcome.

  10. alwaysblank commented on Nov 22, 2017

    @alwaysblank

    @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!

  11. webstractions commented on Nov 23, 2017

    @webstractions

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

  12. darrenjacoby commented on Dec 29, 2017

    @darrenjacoby
    Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions