Develop
2018.01.24 09:26
[php] Laravel Route에서 PC/Mobile 분기
조회 수 2599 댓글 0
# 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
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
69 | Develop | [js] jQuery 치트 시트 | hooni | 2013.12.18 | 36292 |
68 | System/OS |
[mac] Mac에서 Mac으로 원격제어하기 (맥에서 맥으로)
![]() |
hooni | 2013.10.08 | 37206 |
67 | System/OS |
맥에서 파일공유 (윈도우,맥)
![]() |
hooni | 2013.04.25 | 37383 |
66 | Develop |
[ios] iOS 4.0 beta 에서 3.1.3으로 다운그레이드 하는 법
![]() |
hooni | 2013.04.23 | 37531 |
65 | Etc | [svn] 콘솔에서 svn 사용시 레티나용 이미지 add 안될 때.. | hooni | 2013.09.25 | 37572 |
64 | System/OS |
CentOS 6.5 USB 설치
6 ![]() |
hooni | 2013.12.18 | 37778 |
63 | PPT |
[android][ios] 알림(Notification) 기능에 대한 원리와 구현 방안 (APNS포함)
![]() |
hooni | 2013.04.23 | 37889 |
62 | System/OS | [linux] 처음 설치부터 APM 설치까지 (업데이트 할 것) | hooni | 2013.04.23 | 38329 |
61 | Develop | [js] jQuery 충돌 회피 | hooni | 2013.12.17 | 38361 |
60 | System/OS | [mac] 맥에서 APM(apache,php,mysql) 구성하기 | hooni | 2013.04.23 | 38772 |
59 | Develop |
[android] 개발 환경 세팅 따라하기 ㅋㅋ
![]() |
hooni | 2013.04.23 | 40446 |
58 | System/OS | [dos] 노트북 백업 스크립트 xcopy 명령 예제 | hooni | 2003.04.23 | 40855 |