What am I missing?
services.yml
AppBundle\Services\ItemAddService:
arguments: ['#mailer']
ItemAddService class
namespace AppBundle\Services;
class ItemAddService{
private $mymailer;
public function __construct(\Swift_Mailer $mailer){
$this->mymailer = $mailer;
}
public function itemCreatedMailer(){
$message = (new \Swift_Message('Hello Email'))
->setFrom('xxx#gmail.com')
->setTo('xxx#gmail.com')
->setBody("Successfully got SwiftMailer to mail from Symfony3");
$this->mymailer->send($message);
return "Check mail";
}
}
When I call itemCreateMailer() in the controller I get this
"Type error: Argument 1 passed to AppBundle\Services\ItemAddService::__construct() must be an instance of SwiftMailer, none given, called in /home/admin-daniel/symfony-test-sites/july132017/src/AppBundle/Controller/DefaultController.php"
Missing something....
You don't need a use because you're using "\" before the class name that's mean the full class name . Have a Look to my services file https://github.com/ismail1432/eniams-website/blob/master/app/config/services.yml . After that in your Controller call the service via the id which is app.send_mail but you name it as you want . So in the Controller do that $this->get('app_send_mail')->itemCreatedMailer();
Try this in services.yml
AppBundle\Services\ItemAddService:
arguments:
$mailer: '#mailer'
Related
I have a custom authentication service and in ZF2 I accessed this as follows:
Application/view/layout/layout.phtml
$authenticationService = $this->getHelperPluginManager()
->getServiceLocator()
->get('AuthenticationService');
$currentIdentity = $authenticationService->getIdentity();
Now the Zend\ServiceManager#getServiceLocator() is deprecated.
How to get a service available in a view script (or concrete in this case in the layout) in ZF3?
For this purpose there is already Identity View Helper
As documentation says
// Use it any .phtml file
// return user array you set in AuthenticationService or null
$user = $this->identity();
The solution is to assign a global view variable in the onBootstrap(...):
namespace Application;
use ...
class Module
{
public function onBootstrap(MvcEvent $e)
{
...
$serviceManager = $e->getApplication()->getServiceManager();
$viewModel = $e->getApplication()->getMvcEvent()->getViewModel();
$viewModel->authenticationService = $serviceManager->get('AuthenticationService');
}
...
}
Another (perhaps an even better/cleaner) solution is to use a ViewHelper. See also here.
Trying to create a service that logs information to a database. The service has to call the Doctrine\ORM\EntityManager in the constuctor, but I keep getting this error:
Catchable Fatal Error: Argument 1 passed to AppBundle\Service\EmailLoggerManager::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called in /Users/augustwhitlock/Desktop/symfony/SymfonyRepositories/forms/src/AppBundle/Controller/DefaultController.php on line 45 and defined
Here is what I have in my service file
namespace AppBundle\Service;
use Doctrine\ORM\EntityManager;
use AppBundle\Entity\Logger;
class EmailLoggerManager
{
private $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public function logMessageToDatabase($type, $message, $date)
{
$logger = new Logger();
$logger->setMessageType = $type;
$logger->setMessageText = $message;
$logger->setMessageDate = $date;
$this->em->persist($logger);
$this->em->flush();
}
This I how I'm handling the injection of the EntityManager.
app.email_logger_manager:
class: AppBundle\Services\EmailLoggerManager
arguments: ['#doctrine.orm.entity_manager']
At this point I'm just learning about service and trying different things out. But this doesn't want to work.
Here is the edit of the DefaultController. I'm adding lines 45 and 46. There is nothing about it except the class definition.
$emailLoggerManager = new EmailLoggerManager();
$emailLoggerManager->logMessageToDatabase('Info', 'Hiya', new \DateTime());
return new Response('Message Logged');
The whole concept behind the class is to just use doctrine in the service to log things to the database, clearing my controllers from having to be clogged of all that code.
You should call the service from the controller as follows:
$this->get('app.email_logger_manager')
->logMessageToDatabase('Info', 'Hiya', new \DateTime());
instead of instantiating the class directly in the controller.
Furthermore it is advisable to pass the "#doctrine" service instead of #doctrine.orm.entity_manager due to the possibility of the EntityManager being closed.
The constructor would than have to receive Doctrine\Bundle\DoctrineBundle\Registry instead of Doctrine\ORM\EntityManager
I am not sure why I am getting a "404 Not Found" on the following GET call to my api (using PostMan)
http://localhost:53840/api/v1/MessageLog/SomeStuff/3
The method in the Controller is as follows
[System.Web.Http.HttpGet]
public string SomeStuff(int s)
{
return "Received input !";
}
The Register method in the WebApiConfig class has the only route as follows :
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/v1/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
But when I change the code to
[System.Web.Http.HttpGet]
public string SomeStuff()
{
return "Received input !";
}
The call http://localhost:53840/api/v1/LogMessage/SomeStuff works and POSTMAN displays the "Recieved input !" string in the response body.
Is there a specific calling convention for passing in int/string etc. (I tried using a [FromUri] without much success) ? I have another POST method in the controlled which takes a JObject and that seems to be working perfectly fine.
It should be something like:
[System.Web.Http.HttpGet]
public string SomeStuff(int id)
{
return "Received input !";
}
Web API matches the parameter by name. In your route template, it is defined as {id} so the action parameter name must match that.
The reason the second one works is because the id is optional and the action matches the template.
I want to send a sql string for index action in Yii controller? something for example:
index.php?r=staff/index&id=1
I tried it and changed actionIndex() to actionIndex($id) but Yii gave me
error 400 : Your request is invalid.
Is it possible or I have to define another action ?
no you don't need to do that, receive the id as the normal request parameter inside your action method
public function actionIndex(){
$id = $_GET['id'];
}
your can get parameter as follows.
$key = Yii::app()->getRequest()->getParam('your_parameter_name');
and also you can get all the parameters from
$allParam = Yii::app()->getRequest()->restParams
OR
actionIndex($id = 0)
in your staff controller
I used the PowerShell New-WebServiceProxy commandlet to get a connection with a WebService(WCF/SOAP). It´s possible to connect to the WebService but when I want to execute a methode I´m getting a access denied. The access denied is because the WebService needs a custom message header. But this is not possible with New-WebServiceProxy.
Question: What is the easiest way to connect/use the WebService and add the message header? Is there a PowerShell example script?
(My prefered language is PowerShell in that case)
BTW: Yes I know that there is a Question very similar to my: Add custom SOAP header in PowerShell using New-WebServiceProxy
Thank you in advance!
This is more of a workaround, but maybe it works for you. Instead of using the cmdlet, create a C# oder VB.NET Project, add the WCF service reference as it is intended to be used. Then create a class that has a method for every service method you want to call and exposes the arguments you need to use in PowerShell.
class MyProxy
{
public string Url { get; set; }
public string SomeParameterForTheHeader { get; set; }
public string CallSomeMethod(string input1, string input2)
{
// create WCF context using this.Url
// create MessageHeader using this.SomeParameterForTheHeader and add it to the context
// call SomeMethod on the context using input1 and input2
}
}
Compile it and use the assembly and class in your PowerShell script.
[System.Reflection.Assembly]::LoadWithPartialName("MyAssembly") > $null
$ref = New-Object MyNamespace.MyProxy()
$ref.Url = "http://..."
$ref.SomeParameterForTheHeader = "your value here"
$ref.CallSomeMethod("input1", "input2")