1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

LaravelでHello worldを表示させてみた

Last updated at Posted at 2021-04-04

LaravelでHello worldを表示

Laravelの環境構築が終わったので
タイトルにもある通りHello worldを表示させてみた。

プロジェクトの作成

ターミナル
composer create-project laravel/laravel helloworld

これでプロジェクトが作成できたのでhelloworld
ディレクトリへ移動

ターミナル
cd helloworld

ここまでできたらエディタを開きコードを編集していきます。

web.phpの編集

僕はVscodeを使っているのでVscodeを開きます。
そしてhelloworld\routes\web.phpを開いて編集していきます。

helloworld\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');
});

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

コントローラーの作成

続いてコントローラーの作成。

ターミナル
php artisan make:controller HelloController

作成したコントローラーを編集していきます。

helloworld\app\Http\Controllers\HelloController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HelloController extends Controller
{
    //
  public function index()
  {
    return view('hello');
  }
}

hello.blade.phpの作成

resources/viewsの直下に
hello.blade.phpファイルを作成します。

続いてhello.blade.phpファイルの編集。

resources/views/hello.blade.php
<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <title>My First Page</title>
</head>
<body>
  <h2>Hello World</h2>
</body>
</html>  

サーバーの起動

ここまで終わったらサーバーを起動させます。

ターミナル
php artisan serve

下記へアクセス。
http://127.0.0.1:8000/hello

これでHello Worldと表示されます。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?