Symfony differentiate application by dynamic route prefix - symfony

I need to differentiate what data to load by route prefix.
Let's say that I have these routes:
http://test.com/app1/news
http://test.com/app1/gallery
http://test.com/app2/news
http://test.com/app2/gallery
http://test.com/app3/news
http://test.com/app3/gallery
URL of apps (app1, app2, app3) are saved in database. For each app, there are different data for news, gallery etc.
Is there any way how to "cache" app entity and do not load it every time from database and check prefix in every action in controllers?
Is it good idea to save app object to session (and change it only if URL is changed)?
Thanks.

Yes, if you use Doctrine ORM you can configure second level cache.
More Information in link Doctrine second-level-cache

Related

Attaching an entity to a Request in Symfony

I have a symfony 2 application where I set up a kernel.request listener to perform dynamic url checking by loading a corresponding entity from db.
The entity that is loaded then gets attached to a service, so that throughout the application who ever needs that data can ask the service for it.
Reviewing the code I was tempted to simply attach the entity to the Request object. It kind of makes sense, since this entity is used everywhere in the application, and it's determined by a url component.
Is that a proper way to use the Request, or is it better to leave it incapsulated into a service?
Thanks in advance
sergio

Symfony 2 - How can I share data between controllers

I need to be able to make some request data from one controller available in another controller. I can make a service to set the data in one controller, but when the other controller fires and I get the service, a new instance of the service is created. Is there any way I can make this data static and share it between two controllers?
The same basic things you would do whenever you need information to be available in PHP from a new request:
Store it in the session. Symfony2 has a great session component for this. Ideal for fleeting data that needs to be saved only while the user is navigating
Store it in the database. Symfony2 supports Doctrine which makes this very easy. Ideal for permanent storage
Optionally:
Store it on the filesystem. Not recommended unless it's actually a file, but possible as well.
In the end, rather than using the session to store data, I created two separate routes to the same controller action. I added an optional argument in the controller action, with a default value only specified in one of the routes. I can then test for that argument's value when the controller runs. In the Twig template that calls this controller action, the path can be generated using either one of these routes, depending on a variable already available.
Bit of a work around, but problem solved!

Manage security/routing with Symfony2 by requesting a database

Using Symfony2, I'm looking for a way to easily access the main route prefix for a given request, outside any controller:
I am trying to figure out the best way to deal with access control related to some database elements in Symfony2.
I would like to restrict the access to some 'applications' located in the path /application/APP_ID according to the Subscription stored in the database.
Every restricted resource will be in this path.
The subscriptions are referencing a User entity and an Application entity. It includes an ending date. After this ending date, the application should not be accessible anymore.
The restriction should be : for every resource starting with /application/APP_ID, I need to check if the controller should be accessed.
This prefix is static and is actually defined in the app/config/routing.yml as a 'prefix' and the corresponding route name is stored in one of the Application entity attributes. The other involved routes are defined in an independent routing.yml file located for example in MyAppBundle/Resources/config/routing.yml and imported in the main routing file.
Can I easily know if the current route requested (ex. /application/APP_ID/action/1) is included in the route defining the prefix (ex. /application/APP_ID) in order to know what Application is concerned?
Or, is there a way to do that without having to give a list of routes or paths which necessarily require modifications for every application added, route added or modified?
Did you try to develop your own Voter ? As explained here : http://symfony.com/doc/current/cookbook/security/voters.html.
Instead of testing $request->getClientIp, you could test $request->getRequestUri. And you could have an access to doctrine, using $this->container->get('doctrine'). I think you could have all informations you need to secure your paths.
I did it few days ago to store my own RoleHierachy in database, it works well.

Symfony2 Create an entity from two databases in one repository

I have a legacy database from which I have to extract some data and provide it as xml. For that I chose Symfony2 but now I am stuck. I would like to create one entity object, but the problem is, the data for it is distributed in two databases´(on the same server). I don't want to rewrite what I already made, so the easiest way would be to load the other database connections EntityManager in the existing repository. This is where I'am stuck. How can I load an EntityManager in a repository that uses the other connection? And what is the easiest way to "fill-in" the rest of the data of the entity? (By the way, I've used native queries in the repositories, because the legacy database is really complex and does not obey to any rules of DB design). I would be appreciate any help.
You could manage a second database connection called 'legacy', linking to the same database
than you need to map the entities to your managed connections than you could access your legacy table => entity and do whatever you want to with it ;)
http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html

Having a hard time with Nhibernate Sessions in the context of a Controller and Repository

I trying to build an ASP.NET MVC application using Nhibernate and Structuremap along with the Repository pattern.
Through my initial approach to this project and the subsequent reading, I was wanting to keep the Nhibernate Session confined to the Repositories, and the Repositories to the Controller. But at the same time, in regards to transactions, I wanted to keep this separated from the repository methods (i.e. I just wanted to call session.Save()).
But It almost seems required that the Controller know about the session so that I can perform any transaction setup on the session before any Controller action, and any persistence to the database after the action using something like (i.e. the ActionFilterAttribute methods BeforeExecution or AfterExecuted, or BeginRequest/EndRequest events in the MvcApplication.
So in my mind I am having to give the current NHibernate Session to the Controller as well as the Repository, just so I can act on the Session when certain actions/results occur on the controller.
Is this the only way I can go about this?
I have read Ayende's blogs, and an assortment of different ways to go about this, but in the case of using the repository pattern, this almost seems like a must. I was hoping to be able to make the Nhibernate / StructureMap setup to the most pluggable way possible so with the exception of defining FluentNhibernate configuration for a specific project, there would be very minimal wiring for adding it to another MVC project.
That's ok. You'll need to open your session on your request begin and close it on your request end.

Resources