controller icon indicating copy to clipboard operation
controller copied to clipboard

Controller not working on custom post type with underscores

Open eduardoarandah opened this issue 5 years ago • 3 comments

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) );

eduardoarandah avatar Apr 11 '19 23:04 eduardoarandah

@eduardoarandah I believe that is the best solution. https://github.com/soberwp/controller/issues/49#issuecomment-343627895

matbrady avatar Apr 12 '19 13:04 matbrady

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

eduardoarandah avatar Apr 12 '19 13:04 eduardoarandah

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:

https://github.com/soberwp/controller/blob/76a4181e2ceb47232cb5fe0062512ce97b543bf5/src/Controller.php#L63-L66

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: https://github.com/soberwp/controller/blob/76a4181e2ceb47232cb5fe0062512ce97b543bf5/src/Loader.php#L137

xaqrox avatar Apr 09 '20 16:04 xaqrox