Contents

Views 2468 Comment 0
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print

# 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




?

List of Articles
No. Category Subject Author Date Views
1161 Develop GCM 사용하기 3 (JSP로 GCM 푸시 서버 만들기) 4 file hooni 2013.07.06 25315
1160 Develop git 브런치 배우기 (링크) hooni 2013.07.09 20571
1159 Develop GPL, AGPL, MPL,.. 한눈에 보는 오픈소스SW 라이선스 file hooni 2014.10.14 1146
1158 Etc GSM에서 음성이 실리는 과정 요약.. hooni 2013.04.23 17518
1157 Etc How to completely Uninstall Coda hooni 2017.10.24 2161
1156 System/OS How to Install and Use wget on Mac file hooni 2020.09.03 1279
1155 System/OS How to Setup an Email Server on CentOS 7 hooni 2018.04.05 2785
1154 Develop How to Test SMTP AUTH using Telnet hooni 2018.04.05 1335
1153 System/OS HTTP 프로토콜 (브라우저와 웹서버 간의 통신) hooni 2003.04.23 48250
1152 System/OS HTTPS와 SSL 인증서 file hooni 2014.03.11 7546
1151 Etc iOS 에서 쓸만한 오프라인 구글지도 찾기 hooni 2014.01.06 16035
1150 Etc IoT가 만드는 미래와 플랫폼 경쟁력 secret hooni 2014.09.23 0
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 98 Next
/ 98