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

Controller not working on custom post type with underscores #124

Open
eduardoarandah opened this issue Apr 11, 2019 · 3 comments
Open

Controller not working on custom post type with underscores #124

eduardoarandah opened this issue Apr 11, 2019 · 3 comments

Comments

@eduardoarandah
Copy link

Controllers don't work when dealing with CPT that has underscores.

My CPT is cnm_yacht

template:
single-cnm_yacht.blade.php works!

Controller:
SingleCnmYacht Controller doesn't work

imagen

Temporary solution is adding $template var to controller

but.. is there a better alternative?

if not... can we add this to instructions?

class SingleCnmYacht extends Controller {
  protected $template = "single-cnm_yacht";

  public function ok() {
    return "hola";
  }

}

This line is also a good helper to find out hierarchy:
put this one in your template:

$hierarchy = new Brain\Hierarchy\Hierarchy();
var_export( $hierarchy->getTemplates($wp_query) );
@matbrady
Copy link

@eduardoarandah I believe that is the best solution. #49 (comment)

@eduardoarandah
Copy link
Author

CPT with underscores is pretty standard, is there a reason for this not to be automatic ?
Can I help?

@xaqrox
Copy link

xaqrox commented Apr 9, 2020

Agreed that this is at least a minor bug that should be fixed. I don't have time right now to get to the bottom of it, but I tracked it to here:

// $this->template
if (!$this->template) {
$this->template = Utils::convertToKebabCase($this->class->getShortName());
}

This seems backwards to me. Instead using the class name to determine the correct template, we should use the names of the current template and its ancestors to generate a list of classes to load. Such a list of templates is already being generated in Loader.php:

$templates = $this->hierarchy->getTemplates($wp_query);

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

3 participants