LoginSignup
3
1

More than 3 years have passed since last update.

[Laravel][Homestead] ウェルカムページをいじってみる

Last updated at Posted at 2019-07-08

Laravelのインストールが終わったので、ウェルカムページをいろいろ触ってみます。

welcomeページの内容を変更してみる

Laravelのルーティングの設定はroutes/web.phpに記述されています。
この場合、http://homestead.test/へアクセスされた時にwelcomeビューを表示するということです。

routes/web.php
<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

ウェルカムページのビューファイルはresources/views/配下のwelcome.blade.phpです。
試しにwelcome.blade.phpのbodyタグ内を変更して、http://homestead.test/へアクセスすると内容が変更されているはずです。

ウェルカムページのURLを変更する

web.phpを次のように変更してみます。

routes/web.php
Route::get('/welcome', function () {
    return view('welcome');
});

すると、http://homestead.test/welcome/でアクセスした場合はウェルカムページが表示され、http://homestead.test/へアクセスするとページが見つかりませんと表示されます。

Controllerを使用したwelcomeページの表示

プロジェクトの直下で以下のコマンドを実行して、コントローラを作成します。

$ php artisan make:controller WelcomeController
Controller created successfully.

作成されたコントローラを次のように編集します。

app/Http/Controllers/WelcomeController.php

<?php

namespace App\Http\Controller;

use Illuminate\Http\Request;

class WelcomeController extends Controller
{
  // ここから追加
  public function index()
  {
    // 指定されたビューを返す
    return view('welcome');
  }
  // ここまで追加
}

ルーティングの設定をクロージャーからコントローラに変更します。

routes/web.php
Route::get('/', 'WelcomeController@index');

http://homestead.test/へアクセスするとwelcomeビューの内容が表示されます。

ディレクトリ構成

./
├ app
│ └Http
│  └Controllers ... コントローラを格納する
├ resources
│ └views ... ビューを格納する
└ routes ... ルーティングを格納する

参考

初めてのLARAVEL 5.6 : (1) ページの表示フロー

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