Develop
2018.01.24 09:26
[php] Laravel Route에서 PC/Mobile 분기
Views 2666 Comment 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
No. | Category | Subject | Author | Date | Views |
---|---|---|---|---|---|
261 | Etc | 영어의 12 시제 (The twelve tenses of English) | hooni | 2013.07.12 | 15798 |
260 | Develop |
[ios] APNS 클라이언트 구현 (pdf)
![]() |
hooni | 2013.06.27 | 15833 |
259 | PPT |
[ppt] 웜 과제 진행사항(프레임워크 검증환경 구축) 보고
![]() |
hooni | 2013.04.23 | 15932 |
258 | System/OS |
라우팅 경로 결정 영향 요소 ㅋㅋ
![]() |
hooni | 2013.04.23 | 15942 |
257 | System/OS |
[owasp] 10대 웹어플리케이션 보안 취약
![]() |
hooni | 2013.04.23 | 15998 |
256 | Etc |
여기저기서 모은 VoIP(인터넷전화) 자료들~
![]() |
hooni | 2013.04.23 | 16032 |
255 | Etc | 티스토리 테이블 html,css 구문 | hooni | 2013.11.03 | 16048 |
254 | Etc |
[doc] 웜 프레임워크 검증환경 구축(작성중..)
![]() |
hooni | 2013.04.23 | 16058 |
253 | Etc | iOS 에서 쓸만한 오프라인 구글지도 찾기 | hooni | 2014.01.06 | 16097 |
252 | Develop |
[c] 네트워크 정보 알아보기
![]() |
hooni | 2013.04.23 | 16151 |
251 | Etc |
[flash] 페이지 이동 (액션스크립트)
![]() |
hooni | 2013.04.23 | 16288 |
250 | Develop |
[android] 안드로이드 어플 모음 ㅎㅎ
![]() |
hooni | 2013.04.23 | 16340 |