Symfony2 Routing for multiple clients - symfony

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

Related

Angular2 and Symfony combined in the same application

this is my first question on stack overflow so please be kind!
I have designed a Symfony server based application with an almost complete backend interface. Using symfony router, i can login in the backend using a url (ex. www.domain.com/admin) and see backend pages.
Based on this backend, i want to create an Angular2 application for dealing with the front end. It is quite straight forward to make it working for the front page url (www.domain.com). However, if i use any subroutes which would otherwise work in an Angular2 app (ex. www.domain.com/inner-page), Symfony router takes over and throws a 404 Not Found - NotFoundHttpException.
The question is, how can i use both routers in "parallel"? Any URL under www.domain.com/admin will be handled by Symfony router, and any other url by Angular2 router?
EDIT
Thank you for your replies!
I have something in mind. It might not be elegant, since it will require manual exclusion of angular routes from symfony, and vice versa, but it will look cleaner. I am still working on it though, so its still a theoretical concept:
For each url containing "/admin/", Symfony router will serve backend twig-php pages as it is designed to do. For any other url however, a simple controller will just serve the angular app.
On the other hand, in the front end, Angular router will work as it would normally do. However, if a url contains "/admin/", it will be handled by an Angular component that will simply change window.location.href within its OnInit function to redirect to Symfony backend.
The angular app should be built separately, Symfony is a back end framework and isn't a good choice to use for building angular apps.
I created an example of how to use the 2 together, with FOS User Bundle and FOS OAuth Server Bundle to deal with authentication.
It's a couple of years old now, but should give you a good idea of how to go about using them together.
Github is down right now, but I'll update this with a link when it comes back.
edit:
https://github.com/mbates/Symfony2-AngularJs
There is AngularUI routing framework available in AngularJS for routing purposes in your AngularJS application. You can use Angular UI-Routing framework to govern your application's frontend while Symfony2 take care of the backend of your application. Angular JS UI-Routing URL's will looks like this,
www.domain.com/#page1
instead of this,
www.domain.com/page1
After the hash (#) the url will not be a part of Symfony2. The place where you map Symfony2 urls with Angular UI-Router urls will be within states. Take a look at this tutorial. I hope AngularUI will solve your issue :)
Cheers!

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

New symfony bundle, when

On my symfony app, the time has come to add mailer functionality. Im always aware that that some new functionality justifiably goes into its own bundle. right now i want to add some mailer functionality so a user can check off some options, then send the items to a friend.
thinking ahead, i might also use that functionality in another bundle in this same app, which is a different part of the website.
so im thinking, i might want to put an email controller in its own bundle, but i know the swiftmailer bundle is already doing this, which i will be using.
so in the end im thinking its probably only a few lines of code i will need, and that may be best placed on the controllers of the specific parts of the website i need the email functionality on.
now comes the main reason i thought of making it its own bundle, twig templates for the email body's. do i want these templates dangling around in my other bundles? i guess it would make sense.
any suggestions?
It looks a bit overkill to create a bundle just for a few lines of code.
For your twig template you can put the shared template part inside app/Resources/views, which is shared for all your application. And put domain specific templates in domain specific bundles.
http://symfony.com/doc/current/book/templating.html#template-naming-locations
Whatever your email logic code should be inside a service wrapping swift mailer, like that if you need to switch mailing strategy, for example sending mails using an HTTP API, you just need to change this service, not all your controllers.
If you have some code to share between your bundles, may should you have an {App|Main|Core|...}Bundle containing all your "Single" services, that can be moved later in their own bundle if needed.
Anyway their is many approach for your global question :
You could use a single bundle containing all your business logic and externalize / decouple your technical / generic stuff inside bundles that can be shared between your apps
You could have an opposite approach with one bundle for technical stuff and many bundles for your business logic, may could it be harder to keep it low coupled
Or a mix of both
In my point of view the first approach works nicely for simple applications while second and third can be more domain oriented for bigger apps. The most important is probably to be consistant.

Adding new Attributes to ACL in Symfony 2

I'm new to Symfony, for my project I'm trying to use ACL and their Permission Attributes. In the cookbook it says that the permission map is by no means static and could be completely replaced at will. I need to create two other attributes, one Proposer and the other Acceptance. Something like where a user can propose new pages or comments and another one can decide if it can be published or not. Just for the moment I can't find how to implement this with ACL. Anyone has a clue or reference I could start from? I already read about ACL in the cookbook and the advanced use of it but it did not help me.
What you looking for is more related to a rules engine than ACL.
You can have a look on the following link for a quick implementation :
http://knpbundles.com/rezzza/RulerBundle

Detect browser when site is accessed

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.

Resources