LoginSignup
2
1

More than 5 years have passed since last update.

【Laravel】コントローラの作成と編集

Last updated at Posted at 2019-04-05

前提知識

MVCアーキテクチャ
- Model:データ処理。DBに関係。
- View:テンプレートのレンダリング。
- Controller:全体制御。<-ここの話

コントローラ作成

ターミナルにて下記コマンド。

php artisan make:controller HelloController

app/Http/Controllersフォルダ内にHellorController.phpが作成される。
これはApp/Http/Controllersという名前空間にクラスとして配置される。

コントローラにアクションを追加・ルーティング

HellorController.phpを開いてみるとデフォルトでいろいろ書いてある。

  • namespace~:名前空間
  • use~:Illuminate/Httpパッケージ内のRequestを使える状態にしている。
  • class~:HelloControllerはControllerというクラスを継承。
  • この下にアクションを追加していく。 なお、ルートパラメーターとして$id$unknownを渡す。
<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HelloController extends Controller
{
    public function index($id='noname', $pass='unknown'){
        return <<< EOF

<html>
<head>
<title>Hello/Index</title>
<meta chraset="UTF-8">
<style>
body{font-size:16pt;}
h1{font-size:100px; color:#eee;}
</style>
</head>
<body>
<h1>Index</h1>
<p>これはHelloコントーラのindexアクションです</p>
<ul>
<li>ID: {$id}</li>
<li>PASS: {$pass}</li>
</ul>
</body>
</html>
EOF;

    }
}
?>

次にルートを編集。
routes/web.phpにて

Route::get('hello/{id?}/{pass?}', 'HelloController@index');

これでOK。

コントローラに複数のアクションを持たせる場合

(1)コントローラを記述

app/Http/Controllers/HelloController.phpを編集。
まず名前空間とRequestを設定(namespace~/use~)。
変数と関数の定義(global~/function~)。
そしてからクラス定義。
クラスの中にアクションを二つ以上持たせることで、コントーラに複数のアクションを持たせる。

<?php
// 名前空間とRequestの使用可能を設定。
namespace App\Http\Controllers;
use Illuminate\Http\Request;

// コントーラクラス作成時に必要な関数を自作
global $head, $head, $style, $body, $end;
$head = '<html><head>';
$style = <<<EOF
<style>
body{font-size:16pt;}
h1{font-size:100px; color:#eee;}
</style>
EOF;
$body = '</head><body>';
$end = '</body></html>';

function tag($tag, $txt){
    return "<{$tag}>".$txt."</{$tag}>";
}

// コントーラクラスの作成
class HelloController extends Controller
{
    // 第一アクション
    public function index(){
        global $head, $head, $style, $body, $end;

        $html=$head.tag('title', 'Hello/Index')
        .$style
        .$body.tag('h1', 'Index').tag('p', 'this is Index page').'<a href="/hello/other">go to Other page</a>'
        .$end;

        return $html;
    }

    // 第二アクション
    public function other(){
        global $head, $head, $style, $body, $end;

        $html=$head.tag('title', 'Hello/Other')
        .$style
        .$body.tag('h1', 'other').tag('p', 'this is Other page')
        .$end;

        return $html;
    }
}
?>

(2)ルート情報を編集

routes/web.phpを編集。
Route:getの第二引数でコントーラとアクション名を指定。

Route::get('hello', 'HelloController@index');
Route::get('hello/other', 'HelloController@other');

これでコントローラに複数のアクションを持たせることができた。

シングルアクションコントーラ

namespace,useは省略。
クラスにつきアクションを一つのみ持つ。
アクション定義部に__invoke()を足す。

class HelloControllerSingle extends Controller
{
    public function __invoke(){
    return <<<EOF

<html>
<head>
<meta charset="UTF-8">
<title>Hello/Single</title>
<style>body{font-size:16pt;} h1{font-size:100px; color:#eee;}</style>
</head>
<body>
<h1>Single Action</h1>
<p>これはシングルアクションコントーラのアクションです。</p>
</body>
</html>
EOF;
    }
}

参考書籍

PHPフレームワークLaravel入門
Chapter2-2 Pp.39-51

2
1
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
2
1