Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?
@JJJJJJJJ

【CakePHP3.8】CakePHP3で自作コンポーネントを作成、利用

コード

コンポーネントの作成

App/Controller/component/HogeComponent.php
<?php
namespace App\Controller\Component;
use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;
class HogeComponent extends Component{
    function init() {
        return $this;
    }
    public function hoge($itemName){
        //処理内容
    }
}

呼び出したいcontrollerで以下のようにする

class YobidashitaiController extends AppController
{
    public $components = [
        "Hoge" => [],
    ];
    public $hoge;
}

実際に使うactionで以下のようにする

public function jissainiTukauAction()
{
    $hoge = $this->hoge->init();
    $hoge->hoge(処理を加える文字列など);
}

最後に

実行せず思い出しながらなので、
間違ってるかもしれません・・・
備忘録としてメモ。

0
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
JJJJJJJJ
Webエンジニア
it-geinin
技術を使って日々面白いことに挑戦し、Qiitaに投稿します。技術を楽しむ会です。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
0
Help us understand the problem. What is going on with this article?