Symfony2 - resize image after upload - symfony

I want to generate thumbnails after I upload the file. For image manipulations, I use Avalanche123's ImagineBundle.
I tried using the code in Entity:
$avalancheService = $this->get('imagine.cache.path.resolver');
$avalancheService->getBrowserPath($this->getUploadRootDir().'/'.$path.'/'.$extn[0].'.jpg', 'avatar');
But it doesn't help. What can I do?

On this line you try to get a service: $this->get('imagine.cache.path.resolver').
But a code example you're following is supposed to be executed in a controller. There is no get() method in an Entity, controller inherits it from Controller class that all controllers should extend. So calling $this->get() in an Entity is pointless.
This is by design. Entities in Symfony are supposed to be dumb and only represent data they have.
The right thing to do is to either do resize in your controller, or create a service, inject imagine.cache.path.resolver into it and call it from your controllers.

Check the variables using dump(var) in twig. Perhaps $extn[0] doesn't have the correct file or isn't set. Try poutputting the full getBrowserPath string to make sure it's accurate.

Related

How to Generate Controller in Symfony 3.0?

Hope you are all fine, I am a beginner trying to learn Symfony 3.0, so I want to generate my controller in a folder called Bundles, but it is not created, here is the command I taped :
php bin/console generate:controller
And this is fine, when I tape the controller name, that was what I wrote :
?[32mController name?[39m: Bundles/FrontBundle
But their answer is :
?[37;41m The controller name must contain a : ("Bundles/FrontBundle"
given, expecting something like AcmeBlogBundle:Post) ?[39;49m
I don't know if I have to do something before this to noy have such error.
Any Help would be much appreciated.
This is because controller name should be a combination of BundleName:ControllerName and you have provided a relative path to your bundle. If that doesn't work make sure you have your autloading properly setup and Symfony knows about your bundle
According to his doc you should use the shortcut notation when you are declaring a controller, entity, etc.
also you need to read the output of the command.
Welcome to the Symfony controller generator
Every page, and even sections of a page, are rendered by a controller.
This command helps you generate them easily.
First, you need to give the controller name you want to generate. You
must use the shortcut notation like AcmeBlogBundle:Post
Controller name: BundleNameEndingBundle:ControllerName

Doctrine2 , generate on a fly the value of a Entity's attribute that depends on symfony2 container

I have an API, with an API call GET /users which returns me a list of users that all have a avatar_url field
in database this field is just the image name, and in the controller i'm then putting the base URL of my static domain serving images. So that it's only one URL to change in my conf , so the code works in staging/production etc.
but things start to get tricky with GET /comments etc. that all have sub-resource users that needs to have the url, so it means that currently every single point using users needs to have this logic, which is not very DRY
I would like to have something like that
// in my entity
use JMS\Serializer\Annotation as Serializer;
/**
* #Serializer\VirtualProperty
* #Serializer\SerializedName("url")
*/
public function getUrl()
{
return $this->container->getParameter('IMG_URL').$this->imgName;
}
so that regardless on how deeply nested my entity is, I will be able to seralize it with the property.
It seems to me it is possible to achieve something like as there's a bundle
https://github.com/KnpLabs/DoctrineBehaviors
which seems to achieve something similar
Check this out. http://jmsyst.com/libs/serializer/master/handlers
From what I understand you could create your own handler for the url serializer. By having the handler as a service written by you, then you can inject anything you want in it.
More info can be found at Creating a JMS Serializer handler in symfony2

Symfony2: Access Request object in Entity

I'd like to know how to access the Request object in an entity (Symfony2) to modify the user locale.
If someone has found a solution for my problem, please let me know.
It's not possible. This is by design: the entity is just a simple object that should know nothing about the request - it's the responsibility of the controller to interpret the request, and manipulate the entity based on that.
Something like:
//inside your controller:
public function fooBarAction(Request $request)
{
$entity = // get entity
$entity->setLocale($request->getSession()->getLocale());
}
The above is just example code, it won't work if you just copy and paste it. It's just to demonstrate the general idea. The entity should just be a very simple object, who's only responsibility is to hold some data. It shouldn't know where the data is coming from - that keeps it flexible (if you want to set the locale based on something else, you only have to change your controller, not all your entities).
It is possible, but...
What you can but never should do is inject the Request object into the entity (Practically turning your entity into service, see here). Also, even worse idea (but which people still do), you could inject the whole container and get Request from there. The reason why you shouldn't do it is you never should have any code that deals with business rules or any system code in your entities.
You can switch your locale directly in your routes by using _locale custom variable (accessible also from the Request). Or you can create a kernel listener, which will do the required functionality for you. This way you keep your code testable and decoupled.

using another bundle in Symfony 2, can't access $this->get

I have a REST API built with Symfony2 and the FOSRestBundle. This all works fine, however in one service I combine data form another service in a different bundle - this seemed like it would be simple but it is not.
I am creating a new request object and adding in my parameters, from there I fire off the request to the other service, the service receives the request fine, however, when I try to use $this->get it gives me the good old Call to a member function get() on a non-object in ...
I know that I am missing the service container (I don't entirely understand why it's available when I call a hit the first bundle but not the second), that's all well and fine but how do I inject it or a component of it so I can use $this->get to hit my custom services defined in services.yml? (easy to pass them the service container using arguments:
container: "#service_container")
Setting this bundle up as a service won't work as FOSRestBundle does not call it as a service.
In Short: I want to be able to get data from bundle2 when inside bundle1 by doing
namespace MyVendor\Bundle1\Controller
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use MyVendor\Bundle2\Controller\Bundle2ClassName;
class Bundle1 {
//if i wanted to do this here it would work fine:
// $this->get('my.service.defined.in.service.yml');
$bundle2 = new Bundle2ClassName();
$returned_data = $bundle2->myFunction();
}
Then once inside myFunction in bundle2 if I try to call the exact same service function i get the dreaded get error. If I call bundle2 directly through the FOSRest route i obviously don't have that problem.
namespace MyVendor\Bundle2\Controller
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class Bundle2 {
//this does not work
$this->get('my.service.defined.in.service.yml');
//do some stuff then return response
return($response);
}
I've read all the service container docs over and over again, so if you're going to link to them i'd appreciate it if you could point out the exact section where it explains how this stuff is handled. This has been the one problem i've never been able to fully understand since I started working with Symfony a few months back.
P.S. could someone with enough points add the FOSRestBundle as a tag?
Thanks!
First of all you should use $this->forward to forward request to another controller.
Second, the reason you don't have access to service container in second controller is probably because you're trying to manually initialize it - never do that unless you absolutely know what you're doing (specifically, you forgot to pass service container as controller dependency).
Third, just as an example on how things work - your original controller dependency on service container is handled by the same container and extends ContainerAware, on that controller initialization a setContainer() is called, which you most likely forgot to do when manually initializing second controller. So to get it working (which again I strongly recommend not doing), you should do this:
class Bundle1 {
//if i wanted to do this here it would work fine:
// $this->get('my.service.defined.in.service.yml');
$bundle2 = new Bundle2ClassName();
$bundle2->setContainer($this->container);
$returned_data = $bundle2->myFunction();
}
The reason you're getting the $this->get() on a non-object... error is because $this->get() in your controller is actually a shortcut to $this->container->get() (defined in Symfony\Bundle\FrameworkBundle\Controller\Controller)

Asp.net MVC RouteBase and IoC

I am creating a custom route by subclassing RouteBase. I have a dependency in there that I'd like to wire up with IoC. The method GetRouteData just takes HttpContext, but I want to add in my unit of work as well....somehow.
I am using StructureMap, but info on how you would do this with any IoC framework would be helpful.
Well, here is our solution. Many little details may be omitted but overall idea is here. This answer may be a kind of offtop to original question but it describes the general solution to the problem.
I'll try to explain the part that is responsible for plain custom HTML-pages that are created by users at runtime and therefore can't have their own Controller/Action. So the routes should be either somehow built at runtime or be "catch-all" with custom IRouteConstraint.
First of all, lets state some facts and requirements.
We have some data and some metadata about our pages stored in DB;
We don't want to generate a (hypothetically) whole million of routes for all of existing pages beforehand (i.e. on Application startup) because something can change during application and we don't want to tackle with pushing the changes to global RouteCollection;
So we do it this way:
1. PageController
Yes, special controller that is responsible for all our content pages. And there is the only action that is Display(int id) (actually we have a special ViewModel as param but I used an int id for simplicity.
The page with all its data is resolved by ID inside that Display() method. The method itself returns either ViewResult (strongly typed after PageViewModel) or NotFoundResult in case when page is not found.
2. Custom IRouteConstraint
We have to somewhere define if the URL user actually requested refers to one of our custom pages. For this we have a special IsPageConstraint that implements IRouteConstraint interface. In the Match() method of our constraint we just call our PageRepository to check whether there is a page that match our requested URL. We have our PageRepository injected by StructureMap. If we find the page then we add that "id" parameter (with the value) to the RouteData dictionary and it is automatically bound to PageController.Display(int id) by DefaultModelBinder.
But we need a RouteData parameter to check. Where we get that? Here comes...
3. Route mapping with "catch-all" parameter
Important note: this route is defined in the very end of route mappings list because it is very general, not specific. We check all our explicitly defined routes first and then check for a Page (that is easily changeable if needed).
We simply map our route like this:
routes.MapRoute("ContentPages",
"{*pagePath}",
new { controller = "Page", action = "Display" }
new { pagePath = new DependencyRouteConstraint<IsPageConstraint>() });
Stop! What is that DependencyRouteConstraint thing appeared in mapping? Well, thats what does the trick.
4. DependencyRouteConstraint<TConstraint> class
This is just another generic implementation of IRouteConstraint which takes the "real" IRouteConstraint (IsPageConstraint) and resolves it (the given TConstraint) only when Match() method called. It uses dependency injection so our IsPageConstraint instance has all actual dependencies injected!
Our DependencyRouteConstraint then just calls the dependentConstraint.Match() providing all the parameters thus just delegating actual "matching" to the "real" IRouteConstraint.
Note: this class actually has the dependency on ServiceLocator.
Summary
That way we have:
Our Route clear and clean;
The only class that has a dependency on Service Locator is DependencyRouteConstraint;
Any custom IRouteConstraint uses dependency injection whenever needed;
???
PROFIT!
Hope this helps.
So, the problem is:
Route must be defined beforehand, during Application startup
Route's responsibility is to map the incoming URL pattern to the right Controller/Action to perform some task on request. And visa versa - to generate links using that mapping data. Period. Everything else is "Single Responsibility Principle" violation which actually led to your problem.
But UoW dependencies (like NHibernate ISession, or EF ObjectContext) must be resolved at runtime.
And that is why I don't see the children of RouteBase class as a good place for some DB work dependency. It makes everything closely coupled and non-scalable. It is actually impossible to perform Dependency Injection.
From now (I guess there is some kind of already working system) you actually have just one more or less viable option that is:
To use Service Locator pattern: resolve your UoW instance right inside the GetRouteData method (use CommonServiceLocator backed by StructureMap IContainer). That is simple but not really nice thing because this way you get the dependency on static Service Locator itself in your Route.
With CSL you have to just call inside GetRouteData:
var uow = ServiceLocator.Current.GetService<IUnitOfWork>();
or with just StructureMap (without CSL facade):
var uow = ObjectFactory.GetInstance<IUnitOfWork>();
and you're done. Quick and dirty. And the keyword is "dirty" actually :)
Sure, there is much more flexible solution but it needs a few architectural changes. If you provide more details on exactly what data you get in your routes I can try to explain how we solved our Pages routing problem (using DI and custom IRouteConstraint).

Resources