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




?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
1153 System/OS [펌] 마이크로서비스, 모노리포, SRE, ... 덮어놓고 구글 따라하면 안 되는 기술들 file hooni 2020.10.15 3357
1152 Develop [js] Text 값을 클립보드에 복사하기 hooni 2020.10.10 2741
1151 System/OS Apache CORS 설정 1 hooni 2020.09.04 6145
1150 System/OS How to Install and Use wget on Mac file hooni 2020.09.03 5596
1149 System/OS [mac] VirtualBox 실행 스크립트와 bash_profile 설정 file hooni 2020.07.08 3348
1148 System/OS [linux] wget 명령 사용 예제 hooni 2020.05.26 4178
1147 System/OS [linux] The Ultimate Wget Download Guide With 15 Awesome Examples hooni 2020.05.26 3753
1146 Develop [sh] 쉘스크립트 if 비교 연산 hooni 2020.05.26 64358
1145 Develop [sh] html 안에 있는 img 다운 받는 쉘 스크립트 file hooni 2020.05.26 2573
1144 Develop [ios] Start developing your navigation app for CarPlay without enrollment file hooni 2020.02.22 132832
1143 System/OS Configure Postfix to Use Gmail SMTP on Ubuntu 18.04 file hooni 2020.02.07 12277
1142 System/OS RPA란? 어디에 어떻게 쓰이고 누가 만드나? file hooni 2020.01.28 3215
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 99 Next
/ 99