LoginSignup
1
0

More than 5 years have passed since last update.

Laravel コマンドでHelloControllerの作成し、index を表示させる

Last updated at Posted at 2017-11-17

コントローラー作成

artisan(アーティザン)コマンドを使用して作成する

コントローラーの格納場所
/app/Http/Controllers

php artisan make:controller コントローラ名

今回はHelloControllerを作成

php artisan make:controller HelloController
/app/Http/Controller/HelloController.php
//デフォルト作成ファイル
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HelloController extends Controller
{
    //
}

/app/Http/Controller/HelloController.php
//記述後 indexアクションの追加
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HelloController extends Controller
{
    //
    public function index() {
    //パラメーターを渡す場合 
    //public function index($id='no name',$pass='unknown') {
        return <<<EOF
    <html>
    <head>
    <title>Hello/index</title>
    <style>
    body {
        font-size:16pt; 
        color:#999;
    }
    h1 {
        font-size:100pt;
        text-align: right;
        color:#eee;
        margin:-40px 0 -50px 0;
    }
    </style>
    </head>
    <body>
        <h1>index</h1>
        <p>こちらはHelloコントローラーのindexページです</p>
    </body>
    </html>        
EOF;

    }
}

これを書いただけでは表示されないので
下記ディレクトリに追加

/routes/web.php
//HelloController.php と連携 indexアクションの表示
//第二引数:コントローラ名@アクション名 という風に記述する
Route::get('hello','HelloController@index');

//パラメーターを渡す場合の記述例
Route::get('hello/{id?}/{pass?}','HelloController@index');
1
0
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
1
0