0
0

More than 3 years have passed since last update.

【Laravel】Laravel routing設定&Controller・View作成

Last updated at Posted at 2020-05-05

①routing設定

エディタで下記のファイルを開く

routes/web.php

routing設定ファイルに下記のコードを追加

routes/web.php
// 公式
Route::get(ルート, 'コントローラ名@メソッド名');
// 例
Route::get('tests/test', 'TestController@index');

②Controller作成

▪︎ディレクトリ移動

公式)
$ cd /Applications/MAMP/htdocs/プロジェクト名
例)
$ cd /Applications/MAMP/htdocs/laravel_test

▪︎Controller作成

(公式)
$ php artisan make:controller [コントローラ名]Controller
(例)
$ php artisan make:controller TestController

▪︎Controllerファイル確認
エディタで下記のファイルがあることを確認

app/Http/Controllers/TestController.php

▪︎Controllerにメソッド追加

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

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
    //
    public function index() {
        return view('tests.test');
    }
}

③View作成

▪︎Viewファイル作成
下記の通りViewファイルを作成

(公式)
resources/views/[view名].blade.php
(例)
resources/views/tests/test.blade.php

▪︎Viewファイル編集

resources/views/tests/test.blade.php
test

④サーバ起動

▪︎ディレクトリ移動

公式)
$ cd /Applications/MAMP/htdocs/プロジェクト名
例)
$ cd /Applications/MAMP/htdocs/laravel_test

▪︎サーバ起動

$ php artisan serve

▪︎アクセス
下記にアクセスする
http://127.0.0.1:8000/tests/test

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