Contents

조회 수 4677 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

# PC, Mobile, Tablet 기기를 인식하고 분기하는 방법

UserAgent에 따라 분기하는 방법은 여러 가지가 있겠지만 다음 예제는 URL Route 처리 시 UserAgent를 분석해 해당 디렉토리의 파일을 실행하도록 라우팅하는 방법이다.



# routes/web.php 파일 내용

use Jenssegers\Agent\Agent;

Route::get('/{foo?}', function ($foo = 'welcome') {
	$agent = new Agent();
	
	//return $foo;
	//return view($foo);
	return view( ($agent->isMobile() ? 'mobile' : 'desktop').".".$foo);
});



# resources/views 구조

├── views
|   ├── mobile
|   |   ├── home.blade.php
|   └── desktop
|       ├── home.blade.php



대부분 위와 같은 코드가 바로 동작하지 않을 것이다.

다음과 같이 관련 라이브러리를 설치하고 설정하는 과정을 따라하면 된다.



1. 라이브러리 설치

$ composer require jenssegers/agent



2. config/app.php 파일에 추가


'Agent' => Jenssegers\Agent\Facades\Agent::class,



3. 사용 예

use Jenssegers\Agent\Agent;

$agent = new Agent();

$agent->setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2');
$agent->setHttpHeaders($headers);

$agent->is('Windows');
$agent->is('Firefox');
$agent->is('iPhone');
$agent->is('OS X');

$agent->isAndroidOS();
$agent->isNexus();
$agent->isSafari();

$agent->isMobile();
$agent->isTablet();
$agent->isPhone();
$agent->isDesktop();

$languages = $agent->languages();
// ['nl-nl', 'nl', 'en-us', 'en']

$device = $agent->device();

$platform = $agent->platform();

$browser = $agent->browser();

$agent->isRobot();
$robot = $agent->robot();

$browser = $agent->browser();
$version = $agent->version($browser);
$platform = $agent->platform();
$version = $agent->version($platform);


[참고] https://github.com/jenssegers/agent

[참고] https://stackoverflow.com/questions/23779088/laravel-detect-mobile-tablet-and-load-correct-views




?

  1. [ios] 기본 네비게이션바의 타이틀, back버튼 위치와 속성 변경

    Date2016.05.16 CategoryDevelop Byhooni Views5180
    Read More
  2. [ios] Pod 특정 버전 설치하고 사용하기

    Date2022.05.28 CategoryDevelop Byhooni Views5163
    Read More
  3. [ios] DatePicker iOS 6.x 이하 디자인.

    Date2014.04.10 CategoryDevelop Byhooni Views5123
    Read More
  4. [c] Mac OS 에 gmp(gmp.h) 라이브러리 설치

    Date2016.10.03 CategoryDevelop Byhooni Views5064
    Read More
  5. [ios] Xcode를 사용해서 Static Library 만들기 (시뮬레이터 + 디바이스)

    Date2015.01.03 CategoryDevelop Byhooni Views5059
    Read More
  6. [ios] 아이폰 GPS 사용하기

    Date2014.05.24 CategoryDevelop Byhooni Views5039
    Read More
  7. [ios] How to set up clang formatter

    Date2015.09.17 CategoryDevelop Byhooni Views5032
    Read More
  8. [android] 코드에서 문자열로 Resource 가져오기

    Date2015.07.09 CategoryDevelop Byhooni Views5014
    Read More
  9. [ios] 애플 앱스토어 IDFA 리뷰 정책 변경 안내

    Date2014.05.07 CategoryDevelop Byhooni Views5005
    Read More
  10. [ios] UDID 사용 제한에 따른 대안들

    Date2014.03.13 CategoryDevelop Byhooni Views4979
    Read More
  11. [iOS] 시뮬레이터에 푸시 알림을 보내는 방법

    Date2021.10.13 CategoryDevelop Byhooni Views4808
    Read More
  12. [matlab] ZigZag-Scanning (2-D Array)

    Date2016.10.15 CategoryDevelop Byhooni Views4778
    Read More
Board Pagination Prev 1 ... 52 53 54 55 56 57 58 59 60 61 ... 71 Next
/ 71