LoginSignup
1
1

More than 3 years have passed since last update.

【自分用】Laravel さまざまな画面表示パターン

Last updated at Posted at 2017-11-16

会社でLaravelで開発をするそうなので、学んだことを議事録形式で書くことにする

ルートの基本情報(GETアクセス)

Route::get(アドレス,関数など);

//
第一引数:アドレス
第二引数にはそれによって呼び出される処理を用意(関数やコントローラなどを指定する)

ヒアドキュメントを使用して、表示

Laravelをインストールしたドキュメントルート/hello でアクセスできる

(※アドレス指定を特に行っていない場合 Laravelをインストールしたドキュメントルート/public/hello)となる

/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',function(){
   return '<html><body><h1>Hello</h1><p>This is sample page</p></body></html>';
});
*/

$html = <<<EOF
<html>
<head>
<title>Hello</title>
<style>
body {
    font-size:16pt; 
    color:#999;
}
h1 {
    font-size:100pt;
    text-align: right;
    color:#eee;
    margin:-40px 0 -50px 0;
}
</style>    
</head>
<body>
    <h1>Hello</h1>
    <p>This is sample page</p>
    <p>これはサンプルでつくったページです</p>
</body>
</html>
EOF;


Route::get('hello',function () use($html){
   return $html;
});

パラメーターを利用した表示

GETパラメータを利用して表示する
例:Laravelをインストールしたドキュメントルート/hello/test とすると
$msg にtestが入る

//パラメータが必須なパターン
//何も入力されていないとエラーとなる
Route::get('hello/{msg}',function ($msg){

//パラメーターが任意のパターン
//何も入力されていなかった場合 $msg = no messageが入る
Route::get('hello/{msg?}',function ($msg='no message'){
Route::get('hello/{msg?}',function ($msg='no message'){
<?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',function(){
   return '<html><body><h1>Hello</h1><p>This is sample page</p></body></html>';
});
*/

Route::get('hello/{msg}',function ($msg){

    $html = <<<EOF
    <html>
    <head>
    <title>Hello</title>
    <style>
    body {
        font-size:16pt; 
        color:#999;
    }
    h1 {
        font-size:100pt;
        text-align: right;
        color:#eee;
        margin:-40px 0 -50px 0;
    }
    </style>    
    </head>
    <body>
        <h1>Hello</h1>
        <p>{$msg}</p> // Laravelをインストールしたドキュメントルート/hello /testにアクセスすると testが入る
        <p>これはサンプルでつくったページです</p> 
    </body>
    </html>
EOF;



    return $html;
});



1
1
1

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
1