実現したいこと
Cakephpで/testにアクセスした際に、正しくViewが表示されるようにしたい。
発生している問題・分からないこと
Docker+CakePHP5.0の環境を構築し、lacalohost:8080にアクセスすると、Welcome to CakePHPのページが表示される状況です。
下記コマンドでコントローラーとVIEWを作成しました。
$ bin/cake bake template Test index
$ bin/cake bake controller Test
そして以下のファイルが作成されました。
src/Controller/TestController.php
templates/Test/index.php
以下コマンドでルートを確認すると/testでコントローラーが呼ばれるように感じているのですが、/testでアクセスしてもThe requested URL was not found on this server.が表示されます。
$ bin/cake routes
+-------------------------------------+-------------------------------------------+----------+--------+-------------+-------------------+-----------+
| Route name | URI template | Plugin | Prefix | Controller | Action | Method(s) |
+-------------------------------------+-------------------------------------------+----------+--------+-------------+-------------------+-----------+
| test:index | /test | | | Test | index | |
PHPは触ったばかりでうまく説明ができず大変申し訳ないのですが、ご教示いただけますと幸いです。
よろしくお願いいたします。
まずコントローラが呼ばれているかも分からない状況ですので、そこを通ったかどうかのチェックなどのデバッグ方法も合わせてご教示いただけますと助かります。
エラーメッセージ
error
1Not Found 2The requested URL was not found on this server.
該当のソースコード
routes.php
1<?php 2/** 3 * Routes configuration. 4 * 5 * In this file, you set up routes to your controllers and their actions. 6 * Routes are very important mechanism that allows you to freely connect 7 * different URLs to chosen controllers and their actions (functions). 8 * 9 * It's loaded within the context of `Application::routes()` method which 10 * receives a `RouteBuilder` instance `$routes` as method argument. 11 * 12 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org) 13 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) 14 * 15 * Licensed under The MIT License 16 * For full copyright and license information, please see the LICENSE.txt 17 * Redistributions of files must retain the above copyright notice. 18 * 19 * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) 20 * @link https://cakephp.org CakePHP(tm) Project 21 * @license https://opensource.org/licenses/mit-license.php MIT License 22 */ 23 24use Cake\Routing\Route\DashedRoute; 25use Cake\Routing\RouteBuilder; 26 27/* 28 * This file is loaded in the context of the `Application` class. 29 * So you can use `$this` to reference the application class instance 30 * if required. 31 */ 32return function (RouteBuilder $routes): void { 33 /* 34 * The default class to use for all routes 35 * 36 * The following route classes are supplied with CakePHP and are appropriate 37 * to set as the default: 38 * 39 * - Route 40 * - InflectedRoute 41 * - DashedRoute 42 * 43 * If no call is made to `Router::defaultRouteClass()`, the class used is 44 * `Route` (`Cake\Routing\Route\Route`) 45 * 46 * Note that `Route` does not do any inflections on URLs which will result in 47 * inconsistently cased URLs when used with `{plugin}`, `{controller}` and 48 * `{action}` markers. 49 */ 50 $routes->setRouteClass(DashedRoute::class); 51 $routes->connect('/test', ['controller' => 'Test', 'action' => 'index']); 52 53 54 $routes->scope('/', function (RouteBuilder $builder): void { 55 /* 56 * Here, we are connecting '/' (base path) to a controller called 'Pages', 57 * its action called 'display', and we pass a param to select the view file 58 * to use (in this case, templates/Pages/home.php)... 59 */ 60 $builder->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']); 61 62 /* 63 * ...and connect the rest of 'Pages' controller's URLs. 64 */ 65 $builder->connect('/pages/*', 'Pages::display'); 66 67 // $builder->connect('/test', ['controller' => 'Test', 'action' => 'index']); 68 69 70 /* 71 * Connect catchall routes for all controllers. 72 * 73 * The `fallbacks` method is a shortcut for 74 * 75 * ``` 76 * $builder->connect('/{controller}', ['action' => 'index']); 77 * $builder->connect('/{controller}/{action}/*', []); 78 * ``` 79 * 80 * You can remove these routes once you've connected the 81 * routes you want in your application. 82 */ 83 $builder->fallbacks(); 84 }); 85 86 87 /* 88 * If you need a different set of middleware or none at all, 89 * open new scope and define routes there. 90 * 91 * ``` 92 * $routes->scope('/api', function (RouteBuilder $builder): void { 93 * // No $builder->applyMiddleware() here. 94 * 95 * // Parse specified extensions from URLs 96 * // $builder->setExtensions(['json', 'xml']); 97 * 98 * // Connect API actions here. 99 * }); 100 * ``` 101 */ 102}; 103
TestController.php
1<?php 2declare(strict_types=1); 3 4namespace App\Controller; 5 6/** 7 * Test Controller 8 * 9 */ 10class TestController extends AppController 11{ 12 /** 13 * Index method 14 * 15 * @return \Cake\Http\Response|null|void Renders view 16 */ 17 public function index() 18 { 19 // $query = $this->Test->find(); 20 // $test = $this->paginate($query); 21 // $this->set(compact('test')); 22 } 23 24 /** 25 * View method 26 * 27 * @param string|null $id Test id. 28 * @return \Cake\Http\Response|null|void Renders view 29 * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. 30 */ 31 public function view($id = null) 32 { 33 $test = $this->Test->get($id, contain: []); 34 $this->set(compact('test')); 35 } 36 37 /** 38 * Add method 39 * 40 * @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise. 41 */ 42 public function add() 43 { 44 $test = $this->Test->newEmptyEntity(); 45 if ($this->request->is('post')) { 46 $test = $this->Test->patchEntity($test, $this->request->getData()); 47 if ($this->Test->save($test)) { 48 $this->Flash->success(__('The test has been saved.')); 49 50 return $this->redirect(['action' => 'index']); 51 } 52 $this->Flash->error(__('The test could not be saved. Please, try again.')); 53 } 54 $this->set(compact('test')); 55 } 56 57 /** 58 * Edit method 59 * 60 * @param string|null $id Test id. 61 * @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise. 62 * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. 63 */ 64 public function edit($id = null) 65 { 66 $test = $this->Test->get($id, contain: []); 67 if ($this->request->is(['patch', 'post', 'put'])) { 68 $test = $this->Test->patchEntity($test, $this->request->getData()); 69 if ($this->Test->save($test)) { 70 $this->Flash->success(__('The test has been saved.')); 71 72 return $this->redirect(['action' => 'index']); 73 } 74 $this->Flash->error(__('The test could not be saved. Please, try again.')); 75 } 76 $this->set(compact('test')); 77 } 78 79 /** 80 * Delete method 81 * 82 * @param string|null $id Test id. 83 * @return \Cake\Http\Response|null Redirects to index. 84 * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. 85 */ 86 public function delete($id = null) 87 { 88 $this->request->allowMethod(['post', 'delete']); 89 $test = $this->Test->get($id); 90 if ($this->Test->delete($test)) { 91 $this->Flash->success(__('The test has been deleted.')); 92 } else { 93 $this->Flash->error(__('The test could not be deleted. Please, try again.')); 94 } 95 96 return $this->redirect(['action' => 'index']); 97 } 98} 99
index.php
1<h1>Hello, CakePHP 5.0!</h1>
試したこと・調べたこと
上記の詳細・結果
PHPに基本的なルールを学び、上記コントローラーと対応するVIEW、ルートに書かれている内容的には正しく動きそうに思えている。
追記
以下のように'/'部分のコントローラをtestに変更すると、Testコントローラーのindexが呼ばれ、Viewは正しく表示されます。
/xxxxのようなルーティングができていないことになりそうです。
// $builder->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
↓
$builder->connect('/', ['controller' => 'Test', 'action' => 'index']);
補足
特になし
0 コメント