0
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 3 years have passed since last update.

Laravel で Ruby on Rails チュートリアル【4章】文法

Last updated at Posted at 2020-11-14

環境

  • Laravel 8.6
  • PHP 8.0
  • Bootstrap 5.1

4.1動機

4.1.1 組み込みヘルパー

4.1.2 カスタムヘルパー

ヘルパーを定義

/app/helpers.php
<?php

if (!function_exists('full_title')) {
    function full_title($page_title = '')
    {
        $base_title = "Ruby on Rails Tutorial Sample App";
        if (empty($page_title)) {
            return $base_title;
        } else {
            return $page_title . " | " . $base_title;
        }
    }
}

autoloadにヘルパークラスを追加

composer.json
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        },
        "files": [
            "app/helpers.php"
        ]
    },

Composerコマンドを実行

sail composer dump-autoload

ヘルパーを使用した処理に書き換え

/resources/views/layouts/application.blade.php
    <title>{{ full_title($__env->yieldContent('title')) }}</title>

ホームのテストを修正

/tests/Unit/StaticsPagesControllerTest.php
$this->assertSame($this->base_title, $dom->filter("title")->text());

homeのビューからtitleの定義を削除

/resources/views/statics_pages/home.blade.php
- @section('title', 'Home')

4.2 文字列とメソッド

phpのrais consoleみたいなもの

php artisan tinker

4.2.1 コメント

4.2.2 文字列

4.2.3 オブジェクトとメッセージの受け渡し

4.2.4 メソッドの定義

4.2.5 titleヘルパー、再び

4.3 他のデータ構造

4.3.1 配列と範囲演算子

4.3.2 ブロック

4.3.3 ハッシュとシンボル

4.3.4 CSS、再び

4.4 Rubyにおけるクラス

4.4.1 コンストラクタ

4.4.2 クラス継承

4.4.3 組み込みクラスの変更

4.4.4 コントローラクラス

4.4.5 ユーザークラス

4.5 最後に

参照

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?