Laravel の Request と Response

Laravel での Request と Response の使い方です。

<ol>
<li>プロジェクトの用意</li>

```bash
laravel new sample_project
cd sample_project
php artisan make:controller TestController
```

<li>app/Http/Controllers/TestController.php を次のものと置き換えます。</li>

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

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;


class TestController extends Controller
{
	public function index (Request $request,Response $response){
		$html = <<<EOF
<html>
<head>
<title>Test</title>
</head>
<body>
<h1>Test</h1>
<h2>Request</h2>
<pre>{$request}</pre><p />
<h2>Response</h2>
<pre>{$response}</pre><p />

Request<br />
<blockquote>
url<br />
<pre>{$request->url()}</pre><p />
fullUrl<br />
<pre>{$request->fullUrl()}</pre><p />
path<br />
<pre>{$request->path()}</pre><p />
</blockquote>
Response<br/>
<blockquote>
status<br />
<pre>{$response->status()}</pre><p />
</blockquote>

Jun/20/2018 AM 10:09<p />
</body>
</html>
EOF;


	$response->setContent($html);


		return $response;
	}
}
```

<li>routes/web.php を次のものと置き換えます。</li>
>最後の一行を加えるだけでもいいです。

```php: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('test', 'App\Http\Controllers\TestController@index');  // 追加
```

<li>サーバーの起動</li>

```bash
php artisan serve --host 0.0.0.0
```

<li>ブラウザーで、 http://localhost:8000/test にアクセスします。</li>

次のような表示がでます。

```text
Test
Request

GET /test HTTP/1.1
Accept:                    text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:           gzip, deflate
Accept-Language:           ja,en-US;q=0.7,en;q=0.3
Connection:                keep-alive
Cookie:                    XSRF-TOKEN=eyJpdiI6IjNVcFVxRm1TMkh6SElQQ3JUVmkvY0E9PSIsInZhbHVlIjoiaVFZT1ZSa2JDSlBRREprS3piWG1CNWJtdkxoWEQySzF0ZTkrZ0VMNzk5dkgrdGVNN0xxNHpnTnZuWXVqQjVYQXp5YWpIMERqVFZzdHFMdkRRS2k5VjFrL0NWeVlBc2FJaVk5SE1zRlU0ZHI1SlJFcHprSGRTUTBWdFZodXZIOEoiLCJtYWMiOiJmZTI0MmNiMzZlNTMwMGY1OWMxZjBlM2YzNGZkOTQ4NzBlMWVmYmIxMTQ2ZDI2Yjk5ZThjM2I3ZDVhMGU4ZjllIn0%3D; laravel_session=eyJpdiI6InVMRDBFU3MvVzBPUlp5MXF3RjZSN2c9PSIsInZhbHVlIjoicHNWQkJGRkozNzJOTGdYMVVRTHpqQ2xadkVwcnNrRHFadlkydFYvaGtIZDdIZUhLaVZTajJ5M2pSYUxkVGRXS3lXMUI3K2FaYzYxdEZWVEQ1UG56VC9IVnRObVpJcG1TWmdpc2k5Rjd2QXdMYzM2K3JZOVhOOGwvUnNIbTIxUFYiLCJtYWMiOiIzYmM4MjIzMWMwZmY2ZmNkZDg0MmFhNDE1YWFjMzYwMDA0ODUwNDYwMzljY2VmZDNkNDY3Yjc1N2Q3MWM0MWMwIn0%3D
Host:                      localhost:8000
Upgrade-Insecure-Requests: 1
User-Agent:                Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0
Cookie: XSRF-TOKEN=9fZLKbeajwusmez2F56NJVjjIopffP4UbEGNoo7x; laravel_session=2QOOgtRUzR7ewoEpu0U5JYhWJOZ0AHtJKhVmcl1O

Response

HTTP/1.0 200 OK
Cache-Control: no-cache, private
Date:          Sat, 15 May 2021 00:17:00 GMT

Request

    url

    http://localhost:8000/test

    fullUrl

    http://localhost:8000/test

    path

    test

Response

    status

    200

Jun/20/2018 AM 10:09
```

次のように引数をつけると、fullUrl が変わります。
http://localhost:8000/test?a=hello