匿名質問者

Cakephp3 + composerについてです。

composerを使ってdatepickerをインストールしたところ、
アプリ本体/componentsディレクトリに配置されました。その配置されたjsやcssをアプリケーション内で利用したいのですが、どのようにすれば使えるのでしょうか?

因みにインストール時に実行したコマンドは以下です。
composer require eternicode/bootstrap-datepicker

回答の条件
  • 1人5回まで
  • 登録:
  • 終了:2016/07/07 16:30:03

回答1件)

匿名回答1号 No.1

普段使っていませんので、簡単に試して分かった内容の回答になります。

eternicode/bootstrap-datepicker がおそらくインストールのために利用している robloach/component-installer のページに jQuery と normalize.css の使用例が載っています。

robloach/component-installer

Using the Component

The easiest approach is to use the Component statically. Just reference the Components manually using a script or link tag:

<script src="components/jquery/jquery.js"></script>
<link href="components/normalize/normalize.css" rel="stylesheet">


script や link タグを直接記述するか、Html ヘルパーを利用して記述する事もできます。

http://book.cakephp.org/3.0/en/views/helpers/html.html
「Linking to CSS Files」と「Linking to Javascript」に記載あり。

日本語訳ドキュメントのページが未翻訳なので英語ですが、分からなければ 2.x の日本語訳も参考にしてください。

http://book.cakephp.org/2.0/ja/core-libraries/helpers/html.html


プラグインのファイルは、 components ディレクトリごと webroot ディレクトリにコピーして置くか、 eternicode/bootstrap-datepicker をインストールし直してください。
robloach/component-installer の設定でインストールするディレクトリを指定する事ができます。
インストールし直す場合は以下の作業を実行してください。

1.CakePHP をインストールし直します。
composer.json と composer.lock はそのまま残します。

2.CakePHP に影響を与えない(Composerの情報を残す)為に eternicode/bootstrap-datepicker 用のディレクトリを作成します。

3.作成したディレクトリに移動して、 eternicode/bootstrap-datepicker をインストールします。
同じく composer.json と composer.lock はそのまま残します。

4.composer.json を開いてconfig を追加します。

Configuration

There are a number of ways to alter how Components are installed and used.

Installation Directory

It is possible to switch where Components are installed by changing the component-dir option in your root composer.json's config. The following will install jQuery to public/jquery rather than components/jquery:


Base URL

While component-dir depicts where the Components will be installed, component-baseurl tells RequireJS the base path that will use when attempting to load the scripts in the web browser. It is important to make sure the component-baseurl points to the component-dir when loaded externally. See more about baseUrl in the RequireJS documentation.

{
    "require": {
        "components/jquery": "*"
    },
    "config": {
        "component-dir": "public/assets",
        "component-baseurl": "/assets"
    }
}

Defaults to components.


component-dir にはプラグインファイルを作成するディレクトリを相対パスで指定します。
CakePHPのインストールディレクトリ直下に eternicode/bootstrap-datepicker のディレクトリを作成した場合は、"../webroot/example" とします。
component-baseurl には、ウェブ公開用ディレクトリルートからの絶対パスを指定します。
CakePHPの公開用ディレクトリが webroot ですから、webroot を起点として絶対パスで指定します。上記のようにした場合は "/example" となります。

5.eternicode/bootstrap-datepicker をインストールします。

匿名回答1号

eternicode/bootstrap-datepicker のインストール後も composer.json と composer.lock に CakePHP インストール後の内容も残っていますか?
もし残っていたら、手順4だけ実行しても大丈夫だと思います。
config は robloach/component-installer が参照している設定なので、同じディレクトリにインストールしても問題ない可能性があります。

CakePHPからインストールし直す場合にも、手順2、3をスキップして、4を実行してみてください。
この時に require に eternicode/bootstrap-datepicker も追加します。

    "require": {
        "eternicode/bootstrap-datepicker": "^1.6",
        "aaa/bbb-ccc": "*",
        "aaa/bbb-ddd": "^2.0",
        ...
    },

その後で eternicode/bootstrap-datepicker をインストールします。

2016/07/02 12:56:05

コメントはまだありません

この質問への反応(ブックマークコメント)

「あの人に答えてほしい」「この質問はあの人が答えられそう」というときに、回答リクエストを送ってみてましょう。

これ以上回答リクエストを送信することはできません。制限について

回答リクエストを送信したユーザーはいません