Symfony2 Optional host in routing.yml - symfony

I'm developing a website for the organisation I work for. Our main site is www.mainsite.com, but we have a few sub sites that "inherit" (each site accesses the same data and controllers, just look different) the main website. For example sub1.mainsite.com, sub2.mainsite.com etc.
I've started writing the code for the main website, and using the new subdomain/hosts routing feature of Symfony 2.2 I hoped to be able to programatically change the look of the website depending on the subdomain.
If I add host: "{site}.mainsite.com" to one of my routes, I can successfully get {site} in my controller. However, if there is no subdomain, the route isn't matched. This could be solved by adding a new route with no host option, but that would mean I double my routes, and I really don't want to have to do that.
In essence, all I really need is to be able to change the look of the website and the menu it uses based on a subdomain (or no subdomain).
Does anyone know how I can do this?
An example route:
news:
pattern: /news/{page}
host: "{site}.mainsite.com"
defaults: { _controller: MainSiteNewsBundle:News:index, page: 1, site:main }
requirements:
page: \d+
site: sub1|sub2
Also, as an aside, if I could get this to work, could I apply this rule site-wide without having to modify every one of my routes?

Related

Access same website with multiple sub names

I have developed an asp.net application. I will host the application as a new website in IIS.
I want the application to work like below,
http://localhost:81/firm1/login
http://localhost:81/firm2/login
All the above 3 calls need to hit my website and I will do some process with the names 'firm1', 'firm2'.
I don't want to create multiple websites/virtual applications or sub domains.
You can use redirects from one domain to another by creating an on click event. But the best way to do it would be with Routing as mentioned in the previous answer. This is because hyperlinks generated using Routing does not require the changing of URL if the page changes its name or location.
Following this will help you to effectively use Routing:
1. Define custom URL patterns that are not dependent on physical file names.
2. Generate URLs based on route URL parameter values by using markup or code.
3. In a routed page, retrieve values passed in URL segments by using markup or code.
To add routes to a Web site, you can add them to the static Routes property of the RouteTable class by using the RouteCollection.MapPageRoute method.
Have a look at Routing in asp.net applications.
This will allow you to write modules that will receive the url of the requested page, break apart the url in components and delegate to your controllers from there.
If you already have routing set up, you will need to go through all the routes that need to sit below a 'Firm' and modify the URL they map to. For example if you have
routes.MapPageRoute("LoginRoute", "/login", "~/login.aspx")
You would change that to
routes.MapPageRoute("LoginRoute", "/{FIRM_CODE}/login", "~/login.aspx")
The routing classes will parse out the first part of the URL and make it available inside your controller as
RouteData.values("FIRM_CODE")
From there you will need to modify all your logic to handle the firm specific behaviours. You will probably want to store this value somwhere in your session state so that libraries have access to the current firm etc.

Symfony2 Routing for multiple clients

I'm very new to symfony, but I'm sure it will help me to develop faster.
So here are my basic problem. I want to develop a application, that can be used by multiple clients. They will all have its own url. Something like this:
http://example.com/customer1
http://example.com/customer2
I see, that this is very easily done by editing the routing.yml - thats very cool stuff
app:
resource: "#AppBundle/Controller/"
prefix: /{customer}/
type: annotation
In the AppBundle, I can build the whole app within the controllers and symfony offer me the framework to do. It will have some editing routes, admin routes and much more.
But what if the any user call http://example.com/unkownCustomer/someSite
If a someSite route is defined it will cause a problem, just because there is no valid customer. Sure I can handle it, on each Action, but that isn't very smart. I was thinking about extending the Controller class from symfony, to add some base funktionality for example extended the render method to add some basic stuff like customer settings for example the customer name to add it automaticaly in the parameters array for twig, that I don't have to do it explicit in every controller. I think some security features also have to be implemented more generally, that one authenticated user that have a role don't have this role on other customer sites or is not authenticated.
But how I can inject some code before I run the action functionality targeting the route? And the big question - what should be the right way to do? Do have to change my mind doing this thing in symfony?
PS: Sorry for my poor english - hope you will understand my problem.
I learned a lot in the last 2 days - and that video completly answer my question in a very good way doing it in a it think right way!
https://knpuniversity.com/screencast/question-answer-day/symfony2-dynamic-subdomains

FOSUserbundle and multiple database connections in Symfony2

I am working on a Symfony2 Application that uses multiple databases. I followed this guide: https://stackoverflow.com/a/24585284/5244717 so now all my routes have a prefix with the database name from where the application should get its data.
But now I need to be able to login, I tried using the FOSUserbundle, but I cannot get FOSUserbundle to work with the company prefix. I added this to the config/routing.yml
fos_user_security:
resource: "#FOSUserBundle/Resources/config/routing/security.xml"
prefix: /{_site}
defaults:
_site: default
Now when I go to http://localhost/company/login it shows me the login form, but when I login it gives me this error:
You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.
I have no idea how to fix this and if the FOSUserbundle will even work with what I am trying to do here. Having used Symfony2 only for week is not really helping. I've been searching the internet for a good solution to using multiple databases in 1 symfony bundle but so far have had little success. Each company should get a separate database with the same structure, but this seems to be an impossible thing to code in Symfony2(or any other framework).
Any help is very much appreciated!
Your options but not limited to
1). have multiple firewalls
2). use of subdomains instead of slugs in url
i could be wrong but i believe symfony does not allow parameters to be set in a route for a login page

asp mvc 3 home index page only loads ONCE

I created an asp mvc 3 project using the default layout that comes with the non-empty application. Everything has worked great etc. Until I put it in IIS. I have a very strange issue. I can only go to page.com/ which is the HOME controller and Index view ONLY ONCE. When i first load the page i see it. But then when i click the home button it takes me to the account log on, and the url is messed up it looks like page.com/account/logon=?and a bunch of other information.
But on some other controller it does look good like page.com/Information/Shop
Anybody know what might be the home index issue?
Web.config looks like this
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Are you using Named Pipes? .Net 4.0?
it could be that your home link is pointing to something different (whether thats www. or lack there of, or localhost/home rather than localhost) and this is causing the confusion in routing.
It is likely that the IIS site you deployed to has the webroot folder for your site set to require authentication. Thus, when you attempt to access the pages it requests you log in. Once logged in the users information should be maintained in the SessionState so you would not be required to login again during that session. Of course, once the session is ended (due to timeout or certain navigation events) the users informaiton is lost and you would need to log in again. The settings can typically changed in your web.config file. Here is a link to MSDN describing the authentication section.
http://msdn.microsoft.com/en-us/library/532aee0e.aspx

ASP.NET WebForms and MVC Hybrid - Strange characters in URL (random stuff)

I am in the process of creating a hybrid webforms / mvc app. It all works find in VS with its debug web server. However, after overcoming numerous other issues and I am now stuck with a od problem indeed.
When viewing pages served by controllers I am getting random characters show up in the URL - but the site still works, I think.
For example, navigating to /Route/Index takes me to (S(1t2ba055d2unxyqllwuntf55))/Route/Index
Why are these odd characters being generated?
My routes look like this in Global.asax
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.IgnoreRoute("{resource}.asmx/{*pathInfo}");
routes.IgnoreRoute("{resource}.svc/{*pathInfo}");
routes.MapRoute("Route", "{controller}/{action}/{id}",
new { action = "Index", id = "" });
I was informed I had to add a default route without a controller otherwise people could not navigate to / (the root). MVC would try and map this. Wonder if this controllerless rule is part of the issue.
Thanks
(1t2ba055d2unxyqllwuntf55) looks like cookie-less sessions. Check your web.config and make sure it reads something like:
<sessionState cookieless="false" />

Resources