LoginSignup
1

More than 3 years have passed since last update.

Laravel目的別逆引き(予定)

Last updated at Posted at 2020-10-07

Railsでポートフォリオ作ってたけど転職先候補でLaravelを求められそうだったので学習中です。

CRUDを作成したい

ビューを作りたい

Bladeについて

統一感のあるデザインでレイアウトするための機能として

継承と

セクションがあります。

継承

継承元にはない要素を用意するだけで良くなる

セクション

レイアウト内に用意されている区画

ディレクティブ
@section( 名前 )
    /*表示内容*/
@endsection
@yield( 名前 )

@yieldは配置場所を示している

コンポーネント

ヘッダーやフッターなど部品化

componentsフォルダに入れる

ディレクティブ
@component( 名前 )
{{--コンポーネントの表示内容--}}
@endcomponent
スロット

コンポーネントの変数に値を入れるなど

@slot( 名前 )
{{--ここに変数に入れる値を書く--}}
@endslot
resources/views/layouts/sample.blade.php
<html>
    <head><title>@yield('titile')</title></head>
    <body>
        <h1>@yield('title')</h1>
        @section('menubar')
        <h2>menu</h2>
        <ul>
            <li>@show</li> {{--土台となるレイアウトの場合は@endsectionを使わない--}}
        </ul>
        <hr size="1">
        <div>@yield('content')</div>
        <div>@yield('footer')</div>
    </body>
</html>
resources/views/components/sample.blade.php
<div>
    <p>{{$component_sample}}</p>
</div>
resources/views/sample/index.blade.php
@extends('layouts.sample')  {{--継承元を指定--}}

@section('title','Index')   {{--文字列や数値を表示させるだけなら引数に記入する--}}

@section('menubar')
    @parent {{--継承元に@yieldがない場合はセクションの上書き--}}
    インデックスページ
@endsection

@section('content')
    <p>Sample#Index</p>
    <p>Sample#Index</p>
    <p>Sample#Index</p>
    <p>Sample#Index</p>
    @component('components.sample')
        @slot('component_sample')
        サンプルサンプルサンプル
        @endslot
    @endcomponent
@endsection

@section('footer')
footer
@endsection
resources/views/直下にフォルダを作成する
% mkdir resources/views/sample
% touch resources/views/sample/index.blade.php
直接ビューをルーティング
Route::get('sample',function(){
    return view(sample.index);/*フォルダ名.ファイル名*/
});
/*viewが返すのはあくまでも、Responseインスタンス*/
コントローラーを介してビューを表示する
routes/web.php
Route::get('sample/{id?}','SampleController@index');
app/Http/Controllers/SampleControllers.php
public function index($id='no number') {
    $data = [
        'msg'=>'メッセージ',/*連想配列*/
        'id'=>$id,
    ];
    return view(sample.index, $data);/*フォルダ名.ファイル名, 配列*/
}
クエリー文字列の利用
routes/web.php
Route::get('sample','SapmleController@index');
app/Http/Controllers/SampleControllers.php
public function index(Request $request) {
    $data = ['id'=>$request->id];
    return view('sample.index',$data);
}
Bladeでは、受け取ったmodelデータを表示する際には、下記のようにHTMLテンプレート内に入れる。
<p>{{$msg}}</p>

コントローラーを作りたい

通常
% php artisan make:controller [コントローラー名]
Route::get('/sample/{id?}','SampleController@index');
コントローラーにルートパラメーターを渡したい
app/Http/controllers/SampleControllers.php
class SampleController extends Controller
{
    public function index($id='no name') {
        /*return ...*/
    }
}
シングルアクションコントローラー
app/Http/controllers/SampleControllers.php
class SampleController extends Controller
{
    public function __invoke($id='no name') { /*__invoke:PHPのマジックメソッド*/
        /*return ...*/
    }
}
Route::get('/sample/{id?}','SampleController');

ルーティングを設定したい

ルート情報(GET)
Route::get('アドレス','関数やコントローラーなど');
/*例*/
Route::get('/',function(){
    return view('welcome');
});/*無名関数:クロージャー*/
パラメーターを与えたい
1.必須パラメーター
Route::get('/**/{パラメーター}',function($受け取る引数){});
/*例*/
Route::get('/hello/{msg}',function($msg){
    <p>{$msg}</p>
});

/*
http://localhost/hello/こんにちは

=>こんにちは
*/

2.任意パラメーター
Route::get('/hello/{msg?}',function($msg='no message'){
    <p>{$msg}</p>
});

/*
パラメーターに?をつける
クロージャーの引数にデフォルト値を与える
*/

トラブルシューティング

ルーティングが変

docker/Apache関連

ここ

その他

リクエストとレスポンス
use Illuminate/Http/Request;
use Illuminate/Http/Response;
/*
アクション引数に
Request $Request
Response $Response
を追加する。
*/

/*
メソッドインジェクション:
アクションメソッドに引数を追加すると、
サービスコンテナによって対応するクラスのインスタンスが
引数に渡されること

Laravel意外だと、
DI(:DependencyInjection)と呼ばれる
*/
csrf

Cross Site Request Forgery
php
<form method="POST" action="/sample">
@csrf /*Bladeディレクティブ:テンプレートに決まったコードを生成して書き出す働きをしてくれる*/
/* ... */
</form>

namespace

クラスを階層的に整理する仕組み

use文

クラスのインポート

extends

クラスの継承

route書き直したら
% php artisan serve
ヒアドキュメントを書きたい
$html = <<<EOF
<html>
<title>Test</title>
<body>
Hello World!
</body>
</html>
EFO;

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

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