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と表示されます。