0
1

More than 3 years have passed since last update.

Laravelで何か作ってみる〜Hello World〜

Last updated at Posted at 2020-03-12

概要

前回前々回と設定したMac用のLaravel開発環境にて、とりあえず「Hello World」と表示するサイトを作る

参考:公式サイト
https://readouble.com/laravel/6.x/ja/routing.html

前提

環境

Laravel 7.0.8
Valet 2.8.1

構成

Laravelルート [ parkコマンド実行ディレクトリ ]
 ~/DIR/
サイトルート [ プロジェクトディレクトリ ]
 ~/DIR/blog/

設定

ルーティング

サイトにアクセスしたURLによって行う動作を指定

今回デフォルトでは http://blog.test にアクセスすると、ビューからwelcomeを表示する記述あり(実行ファイルは resources/views/welcome.blade.php

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

http://blog.test/user にアクセスしてページを表示するには、次のルーティングを追加。コントローラー UserControllerindex() メソッドの処理実行を意味する。

routes/web.php
//add
Route::get('/user', 'UserController@index');

indexメソッドはコントローラーが処理するURIのルートに対応します。
https://readouble.com/laravel/4.2/ja/controllers.html

コントローラー

ルーティングを受けて処理する制御を定義。artisanコマンドにて、コントローラーにファイルを作成。サイトルートディレクトリにて実行。

$ php artisan make:controller UserController
Controller created successfully.

UserControllerファイルが作成されたことを確認

$ ls app/Http/Controllers/
Controller.php     UserController.php

ファイルを開く

UserController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
  //
}

UserControllerクラスに index() メソッドを追加

UserController.php
  //add index
  public function index()
  {
    return view('user');
  }

ビュー

コントローラーで指定したuserビューファイルを作成。Laravelでは *.blade.phpを使用。新規ファイルを作成。

resources/views/user.blade.php
<html>
<head>
<meta charset='utf-8'>
</head>
<body>
Hello World!
</body>
</html>

サイトが表示されればOK。

hello.png

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