1
1

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

【CakePHP3】Composerでは管理できない外部ライブラリを読み込む方法

Posted at

PHPは歴が浅いので間違ってたらごめんなさい。

基本外部ライブラリはComposer管理なのはわかってるんですが、Composerで管理されていないくて、かつ、Composerの規約に沿っていない外部ライブラリはどう記述すればいいか分散していてたのでまとめ。

やり方

1. ファイルを配置する

vendor フォルダの下に vendor/$author/$package の形式でファイル一式を置く。
vendor/web_client/src/com/hogehoge/・・・
みたいな感じ

2. composer.jsonにファイルにfileを配置した場所を追記する

ベンダーファイルの読込
ここに書いている通り。

"autoload": {
    "psr-4": {
        "App\\": "src/",
        "App\\Test\\": "tests/"
    },
    "classmap": [
        "vendor/utilites/src"
    ]
}

3. 実際にインポートして使うには

参考 How to import vendor files in CakePHP 3x

例えば、vendor/web_client/src/com/hogehoge/input/Request.php みたいなクラスを使いたいときはこんな感じで、require_onceで読み込んで、use で使うクラスを宣言?すれば良いっぽい。

FindById.php
require_once ROOT . DS . 'vendor' . DS . 'web_client' . DS . 'src' . DS . 'com' . DS . 'hogehoge' . DS . 'input' . DS . 'Request.php';

use Request;

    function search($id)
    {
        //Requestクラスをインスタンス化します
        $request = new Request();

毎回絶対パスで記述するのは辛いので、ある程度のパスをpaths.phpにdefineしちゃって使ってます。

ようやく仕組みがわかってきたなー。それでは。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?