Views 2464 Votes 0 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
26 System/OS [linux] iptables 초간단 세팅 스크립트 hooni 2017.09.26 1237
25 System/OS 무료로 HTTPS 적용하기 (Let's Encrypt) file hooni 2017.10.28 1401
24 System/OS OpenSSL로 ROOT CA 생성 및 SSL 인증서 발급하기 hooni 2017.10.28 1455
23 System/OS [mac] How to uninstall MySQL on Mac OS. hooni 2017.11.08 884
22 System/OS [linux] 초간단 Postfix, Covecot, SSL/TLS (SMTP) file hooni 2017.12.11 9323
21 System/OS [linux] 초간단 SquirrelMail 설치/설정 (다람쥐 메일) hooni 2017.12.11 4457
20 System/OS 콘솔에서 패스워드 걸린 zip 압축하는 명령 hooni 2018.03.02 937
19 System/OS [mac] 맥에서 기본 실행 앱 변경하기 file hooni 2018.03.02 1804
18 System/OS How to Setup an Email Server on CentOS 7 hooni 2018.04.05 2779
17 System/OS 서버 확장을 위한 두 가지 방법 file hooni 2018.08.29 2098
16 System/OS Mac에서 Node.js 설치하기 file hooni 2018.09.28 4187
15 System/OS [virtualbox] VirtualBox에 Ubuntu 설치하기 file hooni 2019.11.22 908
14 System/OS SSH Passwordless Login Using SSH Keygen in 5 Easy Steps file hooni 2019.11.22 1388
13 System/OS RPA란? 어디에 어떻게 쓰이고 누가 만드나? file hooni 2020.01.28 1355
12 System/OS Configure Postfix to Use Gmail SMTP on Ubuntu 18.04 file hooni 2020.02.07 1248
11 System/OS [linux] The Ultimate Wget Download Guide With 15 Awesome Examples hooni 2020.05.26 799
Board Pagination Prev 1 ... 9 10 11 12 13 Next
/ 13