cakephpでページを新しく追加する

自分で作ったページを表示させる

hello worldてきなもの

次を追加、編集する。

  • config/routes.php
  • src/Controller/testController.php
  • src/Template/test/index.ctp

my_app_name/config/routes.phpの編集

    /**
     * Here, we are connecting '/' (base path) to a controller called 'Pages',
     * its action called 'display', and we pass a param to select the view file
     * to use (in this case, src/Template/Pages/home.ctp)...
     */
    $routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);

    /**
     * ...and connect the rest of 'Pages' controller's URLs.
     */
    $routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);

    /**
     * /testでアクセスした場合testController.phpのindexメソッドを呼ぶ設定
     *  'controller' => 'hoge' が/my_app_name/src/Controller/hogeController.phpになるよう
     *  'action' => 'index' がhogeController.php内のメソッド名のよう
     */
+    $routes->connect('/test', ['controller' => 'test', 'action' => 'index');

my_app_name/src/Controller/testController.phpを作成

とりあえず必要最低限のコード(たぶん)。
actionで指定したメソッド内でrenderを呼ぶだけで表示することができる。

<?php
namespace App\Controller;

use Cake\Core\Configure;
class testController extends AppController
{
    public function index()
    {
        $this->render();
    }
}

my_app_name/src/Template/test/index.ctpの作成

ここで記述したものが表示される。
テストコードなので

<?php
Hello Cakephp
?>

とかにしました。
中身は普通のPHPファイルです。

httpd.confの変更

wellcomeページが出来たから大丈夫だと思ってましたが、
私の方ではこのままでは表示できませんでした。

ガイドを見直して、httpd.confの設定を変更しました。

<Directory />
   Options FollowSymLinks
-     AllowOverride None
+    AllowOverride All
</Directory>

AllwOverrideをAllに変更

変更したあとはいつものサーバーを再起動させます。

service httpd restart

ブラウザから確認

192.168.33.10/test/ でアクセスして

Hello Cakephp が表示ができたら成功かな