Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8373175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:36:52+00:00 2026-06-09T14:36:52+00:00

Is it possible (and how) to determine if a user is using a mobile

  • 0

Is it possible (and how) to

  • determine if a user is using a mobile device
  • force symfony 2 to load different template in that case
  • (and fall back the default html template)

What id like to do is, to load different templates without modifying any controller.

UPDATE

It wasn’t the detection part the real issue here, it’s really nothing to do with symfony. It can be done (load different template) on a controller level:

public function indexAction()
{
    $format = $this->isMobile() ? 'mob' : 'html';
    return $this->render('AcmeBlogBundle:Blog:index.'.$format.'.twig');
}

But can it be done globally? Like a service, or something that execute before every request, and make changes in the templating rules.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:36:53+00:00Added an answer on June 9, 2026 at 2:36 pm

    Ok, so I don’t have a full solution but a little more than where to look for one 🙂

    You can specify loaders (services) for templating item in app/config/config.yml

    framework:
        esi:             { enabled: true }
        #translator:     { fallback: %locale% }
        secret:          %secret%
        router:
            resource: "%kernel.root_dir%/config/routing.yml"
            strict_requirements: %kernel.debug%
        form:            true
        csrf_protection: true
        validation:      { enable_annotations: true }
        templating:       
            engines: 
               - twig 
            loaders:  [moby.loader]
        default_locale:  %locale%
        trust_proxy_headers: false
        session:         ~
    

    Then define the mentioned loader service:

    services:
        moby.loader:
            class: Acme\AppBundle\Twig\Loader\MobyFilesystemLoader
            arguments:    ["@templating.locator", "@service_container"]
    

    After that define your loader service class:

    namespace Acme\AppBundle\Twig\Loader;
    
    use Symfony\Bundle\FrameworkBundle\Templating\Loader\FilesystemLoader;
    use Symfony\Component\Templating\Storage\FileStorage;
    
    
    class MobyFilesystemLoader extends FilesystemLoader
    {
         protected $container;
    
         public function __construct($templatePathPatterns, $container) 
         {
             parent::__construct($templatePathPatterns);
             $this->container = $container;
         }
    
         public function load(\Symfony\Component\Templating\TemplateReferenceInterface $template)
         {
             // Here you can filter what you actually want to change from html
             // to mob format
             // ->get('controller') returns the name of a controller
             // ->get('name')  returns the name of the template
             if($template->get('bundle') == 'AcmeAppBundle') 
             {
                $request = $this->container->get('request');
                $format = $this->isMobile($request) ? 'mob' : 'html';
    
                $template->set('format', $format);
             }
    
             try {
                $file = $this->locator->locate($template);
             } catch (\InvalidArgumentException $e) {
                return false;
             }
    
             return new FileStorage($file);
          }
    
          /**
           * Implement your check to see if request is made from mobile platform
           */
           private function isMobile($request)
           {
               return true;
           }
     }
    

    As you can see this isn’t the full solution, but I hope that this, at least, points you to the right direction.

    EDIT: Just found out that there is a bundle with mobile detection capabilities, with custom twig engine that renders template file depending on a device that sent request
    ZenstruckMobileBundle, although I never used it so… 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to determine if a given SID is User or Group using
Is it possible for a user control to determine its context or its parent
Is it possible to determine a specific file's fragmentation status (that is, the amount
Is it possible to determine if the user has selected a file for a
is it possible using actionscript to determine what operating system the flash projector is
Is it possible to send mail asycronously using PHP while giving live user feedback
I have an IEnumerable full of objects that we are using to represent user
*I'm using Java. I have this thread, agent, that explores a room to determine
I am trying to determine the city in USA the user is in using
Using the Exchange Web Services API, is it possible to determine whether a mailbox/e-mail

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.