missing_controller.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 0.10.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. * @var string $controller
  15. */
  16. use Cake\Core\Configure;
  17. use Cake\Core\Plugin;
  18. use Cake\Utility\Inflector;
  19. use function Cake\Core\h;
  20. $pluginDot = empty($plugin) ? null : $plugin . '.';
  21. $namespace = Configure::read('App.namespace');
  22. $prefixNs = $prefixPath = '';
  23. $controller = (string)$controller;
  24. $incompleteInflection = (str_contains($controller, '_') || str_contains($controller, '-'));
  25. $originalClass = $controller;
  26. $class = Inflector::camelize($controller);
  27. if (!empty($prefix)) {
  28. $prefix = array_map('Cake\Utility\Inflector::camelize', explode('/', $prefix));
  29. $prefixNs = '\\' . implode('\\', $prefix);
  30. $prefixPath = implode(DIRECTORY_SEPARATOR, $prefix) . DIRECTORY_SEPARATOR;
  31. }
  32. if (!empty($plugin)) {
  33. $namespace = str_replace('/', '\\', $plugin);
  34. }
  35. $filePath = 'Controller' . DIRECTORY_SEPARATOR . $prefixPath . h($class) . 'Controller.php';
  36. if (empty($plugin)) {
  37. $path = APP_DIR . DIRECTORY_SEPARATOR . $filePath;
  38. } else {
  39. $path = Plugin::classPath($plugin) . $filePath;
  40. }
  41. $this->layout = 'dev_error';
  42. $this->assign('title', 'Missing Controller');
  43. $this->assign('templateName', 'missing_controller.php');
  44. ?>
  45. <?php $this->start('subheading');?>
  46. <strong>Error</strong>
  47. <?php if ($incompleteInflection): ?>
  48. Your routing resulted in <em><?= h($originalClass) ?></em> as a controller name.
  49. <?php else: ?>
  50. <em><?= h($pluginDot . $class) ?>Controller</em> could not be found.
  51. <?php endif; ?>
  52. <?php $this->end() ?>
  53. <?php $this->start('file'); ?>
  54. <?php if ($incompleteInflection): ?>
  55. <p>The controller name <em><?= h($originalClass) ?></em> has not been properly inflected, and
  56. could not be resolved to a controller that exists in your application.</p>
  57. <p>Ensure that your URL <strong><?= h($this->request->getUri()->getPath()) ?></strong> is
  58. using the same inflection style as your routes do. By default applications use <code>DashedRoute</code>
  59. and URLs should use <strong>-</strong> to separate multi-word controller names.</p>
  60. <?php else: ?>
  61. <p>
  62. In the case you tried to access a plugin controller make sure you added it to your composer file or you use the autoload option for the plugin.
  63. </p>
  64. <p class="error">
  65. <strong>Suggestion</strong>
  66. Create the class <em><?= h($class) ?>Controller</em> below in file: <?= h($path) ?>
  67. </p>
  68. <?php
  69. $code = <<<PHP
  70. <?php
  71. namespace {$namespace}\Controller{$prefixNs};
  72. use {$namespace}\Controller\AppController;
  73. class {$class}Controller extends AppController
  74. {
  75. }
  76. PHP;
  77. ?>
  78. <div class="code-dump"><?php highlight_string($code); ?></div>
  79. <?php endif; ?>
  80. <?php $this->end(); ?>