2014-10-17 2 views
3

m 개의 routes.php 파일에서 Mobile Detect를 사용하고 싶습니다. composer.json에서 패키지를 요구 사항으로 추가했으며 공급 업체 파일에 설치했습니다. 지금 어떻게 사용할 수 있습니까? 나는이 하나를 사용하여 시도 : Laravel 4 using vendor classesLaravel - 공급 업체 클래스를 사용하는 방법?

{ 
    "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을하지만, 별칭 클래스를 찾을 수 없습니다 말을 일한 적이.

+1

'php artisan dump-autoload','composer dump-autoload' 또는'php composer.phar dump-autoload'를 실행할 수 있습니까? – Sam

+0

나는 이것을했다. 이제 뭐? – jstudios

+0

그걸 고칠 기회가 있었는데 아직도 수업을 못 받고 있다고 가정하고 있습니까? 이름 공간 문제가 없는지 확인하십시오 (@MatthewBrown 참조). – Sam

답변

4

이 패키지의 이름 공간은 PSR-0입니다. git repo를 보면 Detection\MobileDetect인데 실제로 올바른 네임 스페이스인지 확인해야합니다. routes.php 파일에 적절한 네임 스페이스를 추가하려고 시도 했습니까?

use Detection\MobileDetect as MobileDetect; 

또는 올바른 네임 스페이스를 인라인으로 참조 할 수 있습니다. 물론

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

적절한를 입력 :이 당신을 위해 작동하지 않는 경우

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

, 당신은 당신의 composer.json classmap에 추가하여 멀리 얻을 수 있습니다 예를 들면 : 경로를 입력 한 다음 composer dump-auto을 실행하십시오.

+0

그 후에 어떻게 사용합니까? 그 아래에'$ detect = new MobileDetect();'를 시도하고'Class 'Detection \ MobileDetect'not found '를 찾았습니다. – jstudios

+0

답변의 클래스 맵 부분을 시도해보고 Mobile_Detect.php 폴더의 적절한 경로를 채우십시오 –

+0

나는 이것을 클래스 맵에 추가했다 :' "/vendor/mobiledetect/mobiledetectlib/namespaced/Detection/MobileDetect.php", 그리고'MobileDetect.php'는 네임 스페이스'Detection'을 가지고있다. 'MobileDetect로 탐지 사용 '을 시도했지만 여전히 클래스 오류가 발생합니다. – jstudios

0

나는 또한 Laravel의 모바일 탐지에 어려움을 겪고 있었지만 해결책을 찾았습니다! 당신의 Laravel 프로젝트 폴더에서 터미널

  • :

    $ composer require mobiledetect/mobiledetectlib 
    
  • 이동 검출을위한 미들웨어 파일 :

    use Mobile_Detect; 
    
    ... 
    
    $detect = new Mobile_Detect; 
    
    if ($detect->isMobile()) var_dump('is mobile'); 
    else var_dump('is not mobile'); 
    
이 (. 포함 설치) 처음부터입니다

그리고 준비 완료되었습니다.)

관련 문제