LoginSignup
0
0

More than 3 years have passed since last update.

LaravelでMVCを利用してとりあえず表示させてみる

Last updated at Posted at 2020-10-12

【概要】

1.ルーティングの設定

2.コントローラーの設定

3.ビューの設定

4.開発環境

1.ルーティングの設定

routes/web.php
Route::get('hoge', 'App\Http\Controllers\HogeController@index' );

ここで、Route::get('hoge', 'HogeController@index' );とすると、
”Target class [HogeController] does not exist”となってしまうので、最初からパスをコーディングしました。


2.コントローラーの設定

app/Http/Controllers/HogeControllers.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HogeController extends Controller
{
    public function index() #---❶
    {
        return view('hoge.index'); #---❷
    }
}

❶:ここでindexアクションを定義しています。
❷:hogeフォルダのindex.phpファイルに返り値としてレンダリングするようにしています。


3.ビューの設定

resources/views/hoge/index.php
<html>
<head>
  <title>Hoge/Index</title>
</head>
<body>
  <h1>Index</h1>
  <p>HOGEHOGE</p>
</body>
</html>

簡単なHTML文しか書いていません。
「blade」テンプレートエンジンはまた後日説明します。

1~3を通じて下記が表記できます。
http://localhost:8000/hoge

スクリーンショット 2020-10-13 0.32.30.png

4.開発環境

PHP 7.4.10
Laravel 8.9.0
Apache 2.4.41

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