1
0

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 1 year has passed since last update.

Laravel の Request と Response

Last updated at Posted at 2018-06-20

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

  1. プロジェクトの用意
  2. laravel new sample_project
    cd sample_project
    php artisan make:controller TestController
    
  3. app/Http/Controllers/TestController.php を次のものと置き換えます。
  4. 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;
    	}
    }
    
  5. routes/web.php を次のものと置き換えます。
  6. >最後の一行を加えるだけでもいいです。
    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');  // 追加
    
  7. サーバーの起動
  8. php artisan serve --host 0.0.0.0
    
  9. ブラウザーで、 http://localhost:8000/test にアクセスします。
  10. 次のような表示がでます。

    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

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?