LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

【CakePHP3】Hello World表示

Posted at

CakePHP3のControllerを使って、画面上に[Hello World]を表示させます。

CakePHPで標準実装されているbakeコマンドで各種クラスを生成。

//Windowsでの実行時は[/]を[\]に置き換える
bin/cake bake controller XXX

//TestController.phpを作る
bin/cake bake controller Test

Controllerクラスがsrc/Controller直下に、Testクラスがtests配下に作られます。

画面上でHello Worldを表示させる

Controllerクラスにindexメソッドを定義。

namespace App\Controller;

use App\Controller\AppController;

/**
 * Test Controller
 */
class TestController extends AppController
{   
    public function index()
    {
        // 何もしない
    }
}

URLアクセス出来るようルーティングと紐付けます。

$routes->connect('/test', ['controller' => 'Test', 'action' => 'index']);

Controller経由で呼び出されるviewをTemplate配下に作成。

//Template/Test/index.ctp
Hello World

ブラウザ上で「ドキュメントルート(localhost)/test」を実行。

ShellでHello Worldを表示させる

CakePHP3のShellを使って、コンソール上に[Hello World]を表示。

bakeコマンドでshellクラス作成。

//Windowsでの実行時は[/]を[\]に置き換える. |
bin/cake bake shell XXX |

//TestShell.phpを作る.
bin/cake bake shell Test

Shellクラスがsrc/Shell直下に、Testクラスがtests配下に作られます。

作成したTestShellクラスに、以下コードを追記。

namespace App\Shell;

use Cake\Console\Shell;

/**
 * Test shell command.
 */
class TestShell extends Shell
{
    public function index()
    {        
        $this->out("Hello World");
    }
}

ドキュメントルート配下で、以下コマンドをコンソール実行。

bin/cake test index

//実行結果
C:\xampp\htdocs\Cakephp>bin\cake test index

Welcome to CakePHP v3.3.14 Console
----------------------------------------------------------
App : src
Path: C:\xampp\htdocs\Cakephp\src\
PHP : 7.1.1
----------------------------------------------------------
Hello World

コンソール上にHello Worldが表示されます。

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up