I use this library https://github.com/smsapi/smsapi-php-client to send sms from site. But I had a problem when I try to handle base class in service. So my question is there is the way to call a static method with with argument? I try to do it this by factory but this doesn't work for me. This is my code:
smsapi.client:
class: SMSApi\Client
factory: ['SMSApi\Client', createFromToken]
properties:
attributes: '%smsapi_token%'
But I got error below:
Warning: Missing argument 1 for SMSApi\Client::createFromToken(), called in ../cache/dev/appDevDebugProjectContainer.php on line 5073 and defined
As described in the doc: Passing Arguments to the Factory Method:
you can use the arguments options inside the service container.
As Example:
smsapi.client:
class: SMSApi\Client
factory: ['SMSApi\Client', createFromToken]
arguments:
attributes: '%smsapi_token%'
Hope this help
PS: please share the repo of the library you are using or the main part of the source code in order to handle the situation.
Related
I renamed a service from Notifications to Notification (Because I have already a class called Notifications), but when I tried to call it I have the following error:
ParameterNotFoundException in ParameterBag.php line 106:
The service "notification" has a dependency on a non-existent parameter "app.bundle.notification.class". Did you mean one of these: "app.bundle.utils.class", "app.bundle.notifications.class"?
In my Bundle/Services I have 2 files:
- Notification.php
- Utils.php
In Notification.php the class is called class Notification {...}
In my config file services.yml
notification:
class: %app.bundle.notification.class%
arguments: [#templating]
I don't know where it can found the suggest value app.bundle.notifications.class
I tried to clear the cache but I have the same error.
You have to declare your service by this way:
parameters:
your.service.class: ACME\YourBundle\Services\ServiceName
services:
your.service:
class: "%your.service.class%"
arguments: [#templating]
Hope it will help you :)
Symfony errors on container mistakes are - IMO - one of the best thing of this stack. Look closer:
Did you mean one of these: "app.bundle.utils.class", "app.bundle.notifications.class"?
Compiler tells you probable parameters you can use (and which exist). Check the ones of your bundle and rename this param to the new convention.
The simpliest thing to do: search whole project if it looks a bit hidden.
PS. Don't forget to delete cache.
I have a service that needs to access the current application base URL (what's returned by app.request.getBaseURL() in Twig views). Currently, my config is like this:
services:
WidgetModel:
class: AppBundle\Model\WidgetModel
scope: prototype
arguments: ['%widgets%']
So as a second argument, I would like to inject the base URL. Is it possible? If not, what would be proper solution?
As far as I know there is no builtin base url service. Which is actually a bit of a red flag that maybe having your component depending on it might not be such a good idea. But I can't think of a good reason why.
So normally, one would just inject the request object. But that has it's own problems as documented here: http://symfony.com/blog/new-in-symfony-2-4-the-request-stack
Instead, inject the #request_stack service and pull the url from it:
class WidgetModel
{
public __construct($widgets,$requestStack)
{
$this->baseUrl = $requestStack->getCurrentRequest()->getBaseUrl();
If you do find yourself needing the baseUrl in multiple services then you could define your own factory type service to generate it. But again, that might mean your design needs rethinking.
You can use the expression language in your service definition.
This example should do what you want:
services:
WidgetModel:
class: AppBundle\Model\WidgetModel
scope: prototype
arguments: [%widgets%, "#=service('request').getBaseUrl()"]
It fetches the request service and then executes the getBaseUrl method.
You will need to add a second parameter in your WigetModel for the base URL.
To complete the answer, in Symfony 3 you can use request_stack to get the base url using expressions languages (updated link) such as:
services:
WidgetModel:
class: AppBundle\Model\WidgetModel
scope: prototype
arguments: [%widgets%,"#=service('request_stack').getCurrentRequest().getBaseUrl()"
I'm trying to use the method described on this other question and I wasn't able to make it work:
Symfony2: How to inject ALL parameters in a service?
In summary, I would like to define the arguments of my service in a unique file using a variable. I would then only pass this variable as an argument to my service. If one day, I decide to change the arguments, I don't have to change all the calls on my service. I would just change the content of this variable. However, I'm having the following error:
Symfony\Component\DependencyInjection\Exception\InvalidArgumentException You cannot dump a container with parameters that contain references to other services (reference to service "doctrine.orm.entity_manager" found in "/company.usermanagerservice/em").
Was anybody able to make it work? Or do you have other ideas?
Edit:
services.yml:
company.user_manager:
class: Company\CoreBundle\Model\UserManager
arguments: [%company.userManagerService%]
Then on config.yml:
parameters:
company.userManagerService:
em: 'doctrine.orm.entity_manager'
user_helper: 'company.helper.user'
fos_manipulator: 'fos_user.util.user_manipulator'
Hi I'm trying to use Doctrine inside the default ExeptionController but I get the following error:
Fatal error: Call to undefined method Symfony\Bundle\TwigBundle\Controller\ExceptionController::getDoctrine()
when I try to call:
$manager = $this->getDoctrine()->getManager();
What I'm trying to do is to have a custom 404 page where I can present some items from the database.
Could you please help me? Thank you!
You may also inject the Doctrine service as a dependency in your Controller (in that case you don't need to entend class Controller)
You will have to create your own ExceptionController extending the default one. You'll have to declare it as described here: http://symfony.com/doc/current/cookbook/controller/error_pages.html#custom-exception-controller. Your custom controller must have a constructor with at least an argument of type Registry (Doctrine). You have to declare that controller as a service in your service.yml (or xml depending on your config) Have a look at the symfony doc for further explanation on how to do that. For the moment I can't help you much more as I'm outside with my Android and it's rather difficult to make long answers
I am trying to do something like the following question:
Trying to swap a controller using an event listener with Symfony2
However, when I use the code (as recommended in the answer):
$event->setController('MyMainBundle:Manage:show');
I just get an error:
LogicException: The controller must be a callable (MyMainBundle:Manage:show given).
Is there a way to use that Bundle:Controller:Method syntax in setController? Or maybe some other method I can call to resolve that to a "callable"?
What you should give to $event->setController is a callable.
What you give a string representing the logical path to a callable.
You can resolve this string using symfony's ControllerResolver.
You have to inject the controller_resolver service in your listener, and then use it like this:
$request = new Symfony\Component\HttpFoundation\Request();
$request->attributes->set('_controller', 'MyMainBundle:Manage:show'));
$event->setController($this->resolver->getController($request));
But you are clearly doing the framework's job here.