I'm trying to send email in a repository class in symfony2 but I get this error:
Error:
Undefined method 'get'. The method name must start with either findBy or findOneBy!
the code is :
public function sendMail()
{
$message = \Swift_Message::newInstance()
->setSubject('Hello Email')
->setFrom('send#example.com')
->setTo('info#test.com')
->setBody("Salam dadash kheili khosh omadi");
$this->get('mailer')->send($message);
}
what should I change or do?
tnx
It's not good idea to do it from repository class. It should be moved to some service and called as an event or in the simplest way - from Controller. The repository class really do only database things.
In your case the issue is in $this->get('mailer'), because you are in repository you cannot access the containter. Move this part into controller class and everything will be all right.
Related
I'm using Symfony 4 with VichUploaderBundle 1.9 and I'm having hard time injecting the DownloadHandler service in my controller in order to send file to the client.
I'm also using HashidsBundle in order to convert my entity ID to something like jFaJ in my URLs.
As stated in the VichUploaderBundle documentation, I'm injecting the service in my controller like this :
public function download(Wallpaper $wallpaper, DownloadHandler $downloadHandler)
{
return $downloadHandler->downloadObject($wallpaper->getMedia(), 'uploadedFile');
}
Here is the error I'm having:
Argument 2 passed to App\Controller\WallpapersController::download()
must be an instance of Vich\UploaderBundle\Handler\DownloadHandler,
integer given, called in
/mnt/c/Users/user/Documents/Dev/symfony/vendor/symfony/http-kernel/HttpKernel.php
on line 151
I also tried to manually call the service by adding the following line in my controller:
$this->get('vich_uploader.download_handler');
But it's still not working, I have this error now:
Service "vich_uploader.download_handler" not found: even though it exists in the app's container, the container inside "App\Controller\WallpapersController" is a smaller service locator that only knows about the "doctrine", "form.factory", "http_kernel", "parameter_bag", "request_stack", "router", "security.authorization_checker", "security.csrf.token_manager", "security.token_storage", "serializer", "session" and "twig" services. Try using dependency injection instead.
You can return the file using BinaryFileResponse.
public function download(Wallpaper $wallpaper): BinaryFileResponse
{
$file = new BinaryFileResponse($wallpaper->getMedia());
return $file;
}
For more info, check
https://github.com/aythanztdev/prbtcnccd/blob/master/src/Controller/MediaObject/ShowMediaObjectAction.php
The profiler in phpStorm is reporting:
The "request" service is deprecated and will be removed in 3.0. Add a typehint for Symfony\Component\HttpFoundation\Request to your controller parameters to retrieve the request instead.
I thought I was already doing this by following this suggestion by using the following code to get the request and session:
$this->request = $this->get( 'request_stack' )->getCurrentRequest();
$this->session = $this->request->getSession();
Is the warning correct or am I doing this correctly and the warning can be ignored?
Thanks.
This warning can be ignored unless you are going to upgrade to Symfony 3.0 in a future.
If you want to get rid of it I would suggest to follow this warning's message and inject Request object into your actions:
public function yourAwesomeAction(Request $request)
{
$session = $request->getSession();
}
Use like that;
$this->container->get('request_stack')->getCurrentRequest();
You have to use this one :
$request = $this->get('request_stack')->getCurrentRequest();
You may want to read the route name via the request variable, so you can do it as follows :
$routeName = $request->get('_route');
Thanks
I have a couple of days something lost with the declaration of services, especially when passing parameters to custom classes.
I have several extensions and listeners declared as services, to which I pass parameters to the constructor and operating properly but do the same for a custom class and does not work.
The error is that the class does not receive parameters in the constructor.
Error:
Catchable Fatal Error: Argument 1 passed to Consolidador\PanelBundle\Controller\Integration::__construct() must be an instance of Doctrine\Bundle\DoctrineBundle\Registry, none given, called in /var/www/vhosts/consolidadordeviajes.com/httpdocs/src/Consolidador/PanelBundle/Controller/HotelsController.php on line 149 and defined
This is the service that stated in app/config/services.yml:
integration:
class: Consolidador\PanelBundle\Controller\Integration
arguments: ['#doctrine', '#session']
This is the custom class:
<?php
namespace Consolidador\PanelBundle\Controller;
use Doctrine\Bundle\DoctrineBundle\Registry as Doctrine;
use Symfony\Component\HttpFoundation\Session\Session;
class Integration
{
private $em;
private $session;
public function __construct(Doctrine $doctrine, Session $session)
{
$this->em = $doctrine->getEntityManager();
$this->session = $session;
}
...
When I call the custom class is when the error occurs, I think I'm stating the service and collecting the parameters correctly, if not, I hope that other eyes may see the error that I get not find.
A greeting and thanks.
SOLUTION
The problem was simply that instead of calling the custom class from e lcontendor dependency, trying instalanciarla manually. Now I know that the services have to call them:
$foo= $this->get('service_name');
$foo-Method();
Since I migrated to Symfony 2.4, I'm getting the following error message :
Rendering a fragment can only be done when handling a Request.
It's happening because, on some pages, I'm rendering some templates with Twig inside some pages which are handled by another older framework, by doing $sf2->container->get('twig')->render("MyBundle::my-template.html.twig");.
So yes, that's right that Symfony 2 isn't handling those requests, but I still want to render those templates with Twig ! Why can't I do that (anymore) ?! And how to fix that ?
Here is the code I'm executing to "boot" SF2 in my older project :
$loader = require_once __DIR__.'/../../../app/bootstrap.php.cache';
Debug::enable();
require_once __DIR__.'/../../../app/AppKernel.php';
$kernel = new AppKernel('bootstrap', true);
$kernel->loadClassCache();
Request::enableHttpMethodParameterOverride();
$request = Request::createFromGlobals();
$kernel->boot();
$kernel->getContainer()->enterScope('request');
$kernel->getContainer()->set('request', $request, 'request');
$this->container = $kernel->getContainer();
EDIT : By the way, it might be related to that : Symfony 2.4 Rendering a controller in TWIG throws "Rendering a fragment can only be done when handling a Request." Exception .
Though, I don't want to downgrade to Symfony 2.3, and deleting the vendor directory didn't fix my problem.
EDIT² : I found out that the problem is because of the new RequestStack.
In HttpKernel, handleRaw(Request $request, $type = self::MASTER_REQUEST) normally push the request to the RequestStack ($this->requestStack->push($request);). So if I add a public method pushRequestStack($request) to the HttpKernel it works.. But how could I do it properly ? I don't find any public method able to get the $requestStack from HttpKernel (so I can push the request externally)..
And I can't use the "normal" method ($kernel->handle($request)) because it would throw some exceptions, for example for the route that doesn't exist, or also for the session that has already been started by PHP..
In conclusion, is there any way to "push" my/any request to the requestStack without completly handling the request ?
You have to push a new Request in the "request_stack" from your Sf2 command.
$this->getContainer()->get('request_stack')->push(Request::createFromGlobals());
I had the same problem and solved this by rebuilding the bootstrap with this command:
php vendor/bundles/Sensio/Bundle/DistributionBundle/Resources/bin/build_bootstrap.php app
I want to mock a service that is required in a class constructor. I have an exception of PHPUnit : MyService is required, Mock_MyService_0afc7fc1 given.
But with the Request, EntityManager or other Symfony 2 component, I haven't this issue.
Here is my Class's construct :
use Acme\Bundle\Service\MyService;
use Symfony\Component\HttpFoundation\Request;
...
public function __construct(MyService $service, Request $request)
{
and my mock :
...
$service = $this->getMock('MyService');
$class = new Class($service, $request);
It's impossible to mock our own service ? Only Symfony 2 component ?
PS : If I delete MyServicelike that : public function __construct($service, Request $request), this works. But I want to define my variable with it :(
The issue is that PHPUnit at the time of the test execution can't find (or autoload) your MyService class.
That means that you'll probably run into the same issues with other Mocking libraries as all of them require the original class to exist to scan it and create the mock.
It happens because you need to tell PHPUnit the Fully-Qualified Class Name.
Change your code to $this->getMock("\Acme\Bundle\Service\MyService"); and it should work out.
(Still, give mockery a try. It's a nice library)