5

m routes.php ファイルで Mobile Detect を使用したいと考えています。パッケージを composer.json の必須として追加し、ベンダー ファイルにインストールしました。どうすれば今すぐ使えますか?

クラスが見つからなかったため、この回答を試してみましたが、うまくいきませんでした: Laravel 4 using vendor classes

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "require": {
        "laravel/framework": "4.2.*",
        "mobiledetect/mobiledetectlib": "*"
    },
    "autoload": {
        "classmap": [
            "app/commands",
            "app/controllers",
            "app/models",
            "app/database/migrations",
            "app/database/seeds",
            "app/tests/TestCase.php"
        ]
    },
    "scripts": {
        "post-install-cmd": [
            "php artisan clear-compiled",
            "php artisan optimize"
        ],
        "post-update-cmd": [
            "php artisan clear-compiled",
            "php artisan optimize"
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ]
    },
    "config": {
        "preferred-install": "dist"
    },
    "minimum-stability": "stable"
}

編集: https://github.com/jenssegers/Laravel-Agentを使用してみましたが、クラスが見つからないと言ってエイリアスが機能しませんでした。

4

2 に答える 2

8

このパッケージはPSR-0名前空間です。git リポジトリを見ると、Detection\MobileDetect これが実際に正しい名前空間であることを確認する必要があるようです。ファイルに適切な名前空間を追加しようとしましたroutes.phpか?

use Detection\MobileDetect as MobileDetect;

または、適切な名前空間をインラインで参照できます。次に例を示します。

$detect = new Detection\MobileDetect\Mobile_Detect;
$deviceType = ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer');

これがうまくいかない場合は、クラスマップに追加することでcomposer.json回避できる場合があります。

"autoload": {
    "classmap": ["/vendor/serbanghita/namespaced/"],
}

もちろん、適切なパスを入力してから、composer dump-auto.

于 2014-10-17T19:56:48.080 に答える