Laravel detect mobile/tablet and load correct views

前端 未结 4 1305
有刺的猬
有刺的猬 2020-12-24 00:52

I have read how to add different paths or namespaces for views, but I think this is not a proper solution for me. What I would like to do it\'s to set a view base path for

4条回答
  •  情话喂你
    2020-12-24 01:24

    As suggested in a comment on the accepted answer (include mobile view path only on mobile and fallback to 'default' view):

    isMobile()) {
        array_unshift($viewsPaths, $viewBasePath.'/mobile');
    }
    
    return [
        'paths' => $viewsPaths
        ...
    

    This way you only override what you need. This may come in handy for emails and when you have several partial views that have the same html regardless of device category.

    Note: Usage in controller doesn't change.

    Example views:

    ├── views
    |   ├── home.blade.php
    |   ├── posts.blade.php
    |   ├── post.blade.php
    |   ├── emails
    |   |   └── subscription.blade.php
    |   └── partials
    |   |   ├── posts-popular.blade.php
    |   |   ├── banner-ad.blade.php
    |   |   ├── post-comment.blade.php
    |   ├── mobile
    |   |   ├── home.blade.php
    |   |   ├── partials
    |   |       └── posts-popular.blade.php
    

提交回复
热议问题