Detect browser when site is accessed - symfony

Is there anyway to detect browser version in symfony prior to loading any pages and then run a corresponding action? I was thinking of using modernizr, but I was hoping to not have to rewrite all my views.
Basically what I was hoping I could accomplish is that before the framework attempts to match the url to a route it would first check to see if the user has a particular browser version and if not run another controller instead of the the controller defined in the route.

I think what your suggesting is not a "best practice". I'm assuming you want to route a mobile request to a unique Action method in a Controller. You really shouldn't have a separate controller based client device. This is the job of the (V)view in MVC.
Modernizr is a client side solution with css,js but if youre using symfony, i would prefer server side and that requires the title of this question.. "Detecting browser on request".
While you can custom configure your app to detect the device(and its not to hard), there's also an easier option. Try this bundle. https://github.com/suncat2000/MobileDetectBundle . It does everything and allows for you to make both controller modifications or make twig aware of the browser so you can make smaller changes.

Related

How to use/integrat the Piwik tracking API in Symfony 2 without JavaScript

I would like to track the users on my Symfony 2.7 page using Piwik.
Piwiks default tracking option is the integration of a small JavaScript into the webpage, e.g. with the page footer. Of course this could easily be done by adding the code to the base Twig template.
However I would like to avoid the usage of JavaScript. My Symfony page and the Piwik page both run on the same server. Actually I am surprised that letting the client communicate with Piwik (over Javascript) is the recommended option instead of a direct way to let the webpage server talk to Piwik.
I think the Tracking HTTP API and the PHP Client are what I am looking for. However I am not sure on how to integrate this into Symfony.
I managed to connect my Symfony page with Piwik, however I wonder if this the right/best way to do it:
Used composer require "piwik/piwik-php-tracker" to add the PHPTracker to my Symfony project
Added a new Service that is responsible to create and manage a PiwikTracker object. The Service makes different methods of the Tracker, e.g. doTrackPageView() available to the rest of the project.
Added a new Twig function which uses the Service to trigger the doTrackPageView() method.
Added a call to this function to my base template
This works fine: Page views are correctly reported to Piwik and recorded without any problem. Of course I could easily add other methods to track events, actions, etc.
But: I am really surprised that I could not find any ready-to-use solution for this. Did I miss anything or is this the indented way to use Piwik with Symfony?

Meteor.js - Template Permissions

This has been asked in similar forms here and here but it seems pretty important, and the framework is under rapid development, so I'm going to raise it again:
Assuming your login page needs to face the public internet, how do you prevent Meteor from sending all of the authenticated user templates to a non-authenticated client?
Example use case: You have some really unique analytics / performance indicators that you want to keep secret. You've built templates to visualize each one. Simply by visiting the login page, Meteor will send any rando the templates which, even unpopulated, disclose a ton of proprietary information.
I've seen two suggestions:
Break admin into a separate app. This doesn't address the issue assuming admin login faces the public internet, unless I'm missing something.
Put the templates in the public folder or equivalent and load them dynamically. This doesn't help either, since the file names will be visible from other templates which will be sent to the client.
The only thing I can think of is to store the template strings in the server folder and have the client call a Meteor.method after login to retrieve and render them. And if you want them to behave like normal client templates, you'd have to muck around with the internal API (e.g., Meteor._def_template).
Is there any more elegant way to do this?
I asked a similar question here:
Segmented Meteor App(s) - loading only half the client or two apps sharing a database
Seems to be a common concern, and I certainly think it's something that should be addressed sometime.
Until then, I'm planning on making a smaller "public" app and sharing the DB with an admin app (possibly in Meteor, possibly in something else, depending on size/data for my admin)
These 2 packages try to address this issue:
https://atmospherejs.com/numtel/publicsources
https://atmospherejs.com/numtel/privatesources
It uses an iron-router plug-in to load your specific files on every route.
The main drawback I see here is that you must change your app structure, as the protected files need to be stored in /public or /private folder.
Also you are supposed to use iron-router.

Spring: Best way to select a customized view at runtime

I am looking to borrow someone's brains (& experience) to figure out an optimum way to implement the below.
I am currently working on an application which will be used by many of our clients and will require UI customizations according to each client's requirements. Although the underlying java code as well as business logic will be the same, at runtime I would like to figure out (via some property) which client the application is running for and thus, choose the corresponding view and display the web page.
Currently, all I could think of is to create a custom ViewResolver (extending InternalResourceViewResolver) that will read the client name property and use this to resolve a view (eg: WEB-INF/jsp//account/myAccount.jsp) or default to another view (eg: WEB-INF/jsp/default/account/myAccount.jsp) if client property not present.
The issue I see here is that I might not want to overwrite all my JSPs for a particular client where the default view should be rendered. This will require an I/O Call to check the existence of the JSP file in the client's folder before reverting to the default. To counter the I/O I can even create a Map of overwritten JSPs at application startup but this will require an application restart on addition of new customized JSPs which again can be handled by exposing some service to refresh this Map.
So what do you guys think? Also, how will it affect redirects? Plz let me know if you need any further information about our setup or requirement.
Regards,
Sumit
You can use tiles to control the rendering of your view it has a default view resolver bundled with spring and you can extend that.
Define custom layouts, extend them and change it in case you want another behavior.
http://tiles.apache.org/
After waiting for more comments / suggestions, as JB suggested, going with my own solution as described in the question itself.
Thanks all for your help.

Application_AuthenticateRequest hit for all requests, including images and js files

In my MVC3 application, I'm using Application_AuthenticateRequest to create my custom user context and create the session. However, I notice that this is getting fired for every file per page request, including images, js, css, etc.
Is this the right method to do what I'm trying to do, or should I be doing this somewhere else (i.e. action filter)? Or, is this the right place, I just need to put some checks and/or configuration to ensure this method (or my block of code) is just executed for page requests instead of requests for static files?
I searched for a while trying to find the answer, and found one specific to IIS7, but this is happening for me on my ASP.NET dev server (debugging) on WinXP. Other than that, I couldn't find much, which leads me to think I may be way off on something here, possibly overlooking something simple.
Thanks in advance.
Jerad,
You are correct that you would be better off creating an action filter to handle your user context. You can decorate those controllers where the user context is required.
This is a better solution than using code to investigate the request, just so you can ignore particular requests.
counsellorben

Does IIS throw away the URL fragment on custom error pages?

I'm using the old 404-rewrite method on a certain site that is tied to IIS6 *.
So if I enter
http://example.com/non-existent/path
it calls my error page like so
http://example.com/catch.aspx?404;http://example.com/non-existent/path
Great.
Except if I call the page with a fragment, like
http://example.com/non-existent/path#with-fragment
I get the same result as above. I can't find the fragment anywhere:
Request.Url
Request.Url.OriginalString
Request.UrlReferrer
Request.RawUrl
headers, server variables, etc
This has come up because I want to resolve paths created by AJAX to their server-side versions.
Is there any way for me to retrieve the original path from my handler?
Thanks.
(*) Please don't suggest I change platform. Obviously I would if I could.
No, there isn't.
The portion of URL after # is never passed to the server per HTTP spec. Has nothing to do with platform.
To work with info after # in javascript you should look at Javascript History plugins/functionality. jQuery has history plugin, asp.net ajax and mvc ajax (partial views et al) have that. Mind you, it's not a very easy thing to implement, you have to get into undo/redo mindset.
It probably won't work if you are trying to handle 404's on the server - server doesn't know that there was something after #. Not sure what you want to do though, 404 handling, or "resolve paths created by AJAX"? What exactly is the goal?

Resources