Skip to content

Template path resolution error when rendering plugin controller templates in CakePHP 5.3 #19180

@s-nakajima

Description

@s-nakajima

Description

When specifying a particular template from a controller as shown below, an error occurs.

$this->render('PluginName.PluginController/custom_file');

Error message:

ROOT/plugins/PluginName/templates/PluginController/PluginController/custom_file.php

It seems that this error is caused by a change in the template path resolution logic introduced in CakePHP 5.3.

The relevant code in CakePHP 5.3 is shown below:
https://github.com/cakephp/cakephp/blob/5.3.0/src/View/View.php#L1371-L1379

CakePHP 5.3

if (!str_contains($name, DIRECTORY_SEPARATOR) && $name !== '' && !str_starts_with($name, '.')) {
    $name = $templatePath . $subDir . $this->_inflectTemplateFileName($name);
} elseif (str_contains($name, DIRECTORY_SEPARATOR)) {
    if (str_starts_with($name, DIRECTORY_SEPARATOR) || $name[1] === ':') {
        $name = trim($name, DIRECTORY_SEPARATOR);
    } else {
        $name = $templatePath . $subDir . $name;
    }
}

As a test, I reverted the relevant part to the CakePHP 5.2 logic, and the issue no longer occurred.

CakePHP 5.2

if (!str_contains($name, DIRECTORY_SEPARATOR) && $name !== '' && !str_starts_with($name, '.')) {
    $name = $templatePath . $subDir . $this->_inflectTemplateFileName($name);
} elseif (str_contains($name, DIRECTORY_SEPARATOR)) {
    if (str_starts_with($name, DIRECTORY_SEPARATOR) || $name[1] === ':') {
        $name = trim($name, DIRECTORY_SEPARATOR);
    } elseif (!$plugin || $this->templatePath !== $this->name) {
        $name = $templatePath . $subDir . $name;
    } else {
        $name = $subDir . $name;
    }
}

Could you please review this behavior?

Thank you for your time and consideration.

CakePHP Version

5.3.0

PHP Version

8.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions