I want to get User (FOSuserBundle) in Listener With Symfony (2.7).
My Listener service :
utilisateur_cabinet_listener:
class: UtilisateurBundle\Listener\Cabinet
arguments: [#router, #utilisateur_cabinet_repository]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
In Stackoverflow, you recommend to import container service, but the subjects date. Do you have another solution ?
Related
I'm using Guzzle as a http client in a Symfony app. With the following code I'm able to log all requests and responses. Is there a way to log also response time?
I tried on_stats but it should be added to each client call and it is not an option.
services:
app.client.default:
class: 'GuzzleHttp\Client'
lazy: true
arguments:
- handler: '#app.handler_stack.default'
app.handler_stack.default:
class: 'GuzzleHttp\HandlerStack'
factory: [ GuzzleHttp\HandlerStack, create ]
calls:
- [ push, [ '#app.middleware.log_default', 'log_default' ] ]
app.middleware.log_default:
class: callback
factory: [ GuzzleHttp\Middleware, log ]
arguments:
- '#logger'
- '#app.log_formatter.default'
app.log_formatter.default:
class: 'GuzzleHttp\MessageFormatter'
arguments:
- ">>>>>>>>\n{request}\n<<<<<<<<\n{response}\n--------\n{error}"
app.service:
class: 'App\Service'
arguments:
- '#app.client.default'
use GuzzleHttp\Client;
class Service {
private $httpClient;
public function __construct(Client $httpClient) {
$this->httpClient = $httpClient;
}
public function apiRequest() {
$response = $this->httpClient->get('https://www.google.com');
// ...
}
}
I'm writing a Symfony 4 application. Its core is a process with some nested sub-processes. In general, the idea, how to handle it looks like this:
There are some processes (hierarchy of the main processes and its sub-processes). Every process has a set of states. Every process has a set of allowed events.
The facade of this mechanism is the SystemEventHander. It registers the handled processes, analyses the incoming requests, and find and execute the appropriate handler for the current event.
services.yaml
# events
App\Process\SystemEventHandlerInterface:
class: 'App\Process\SystemEventHandler'
tags:
- { name: kernel.event_listener, event: general:user_message_received, method: handle }
- { name: kernel.event_listener, event: process_a:foo, method: handle }
- { name: kernel.event_listener, event: process_a.process_b:foo, method: handle }
- { name: kernel.event_listener, event: process_a.process_c:bar, method: handle }
- { name: kernel.event_listener, event: process_a.process_d:baz, method: handle }
- { name: kernel.event_listener, event: process_a.process_d.process_e:buz, method: handle }
So my all custom events are initially handled by only one handler: the SystemEventHandler().
The proble is, that for every single event I have to write nearly the same line:
- { name: kernel.event_listener, event: process_name:event_name, method: handle }
Only process_name:event_name varies, the rest is static.
Image I have a big number of events -- 30? 50? 100? How can I avoid explicitly writing a new line for every single one?
This must be a simple oversight - why isn't my entity_menus parameter being injected into the StoreController?
This is the error I am seeing:
Catchable Fatal Error: Argument 1 passed to AppBundle\Controller\Api\Admin\Common\StoreController::__construct() must be of the type array, none given
The relevant parts of services.yml
parameters:
entity_menus: [ 'manufacturers', 'vendors' ]
services:
app.admin.store_controller:
class: AppBundle\Controller\Api\Admin\Common\StoreController
arguments:
- '%entity_menus%'
The controller:
class StoreController extends FOSRestController
{
private $entityMenus;
public function __construct( Array $entityMenus )
{
$this->entityMenus = $entityMenus;
}
I updated services.yml like so to set the service container:
app.admin.store_controller:
class: AppBundle\Controller\Api\Admin\Common\StoreController
arguments: ['%entity_menus%']
calls:
- [setContainer, ["#service_container"]]
Thanks to: https://stackoverflow.com/a/19283476/2182349
I updated routing_rest.yml to use the service name and the class:
app_admin_common_store_menu:
type: rest
class: AppBundle\Controller\Api\Admin\Common\StoreController
resource: app.admin.store_controller
name_prefix: app_admin_api_store_
prefix: /api/store
Thanks to: https://github.com/FriendsOfSymfony/FOSRestBundle/issues/990
I made a command that automatically writes in routing.yml.
My problem is that when I try to browse one of the routes
api:
resource: "."
type: "api"
prefix: /api
I get this exception
Cannot load resource "."
I tried to add a cache:clear to my command but I get the same exception.
I added a cache warmup that runs after the command termination that way Symfony dumps routes into generated code .
class TerminateListener {
public function onConsoleTerminate(ConsoleTerminateEvent $event) {
if ($event->getCommand()->getName() == ('my:command')) {
$app = new Application();
$cache_clear_command = $event->getCommand()->getApplication()->find('cache:warmup');
$cache_clear_command->setApplication($app);
$event->getOutput()->setVerbosity('VERBOSITY_QUIET');
$cache_clear_command->run($event->getInput(), $event->getOutput());
}
}
}
services:
warmup.listener:
class:TerminateListener
tags:
- { name: kernel.event_listener, event: console.terminate , method: onConsoleTerminate }
I am working on a Symfony 2 web app and I would like to inject a Monolog logger using a specific channel to a service:
The Config:
monolog:
handlers:
main:
type: stream
path: %kernel.root_dir%/%kernel.environment%.log
level: error
#channels: [!alert]
alert:
type: stream
path: %kernel.root_dir%/%kernel.environment%.alert.log
level: info
channels: [alert]
Service Config:
services:
some_service:
class: Some\Service
arguments: [#logger]
tags:
- { name: monolog.logger, channel: alert }
The Service:
class SomeService {
protected $logger;
public function __construct($logger) {
$this->logger = $logger;
$this->logger->info('Log this!');
}
The prod.log file:
[2016-03-28 11:25:47] alert.INFO: Log this!
The Problem: Although I specifically inject the logger using the alert channel, the message is handled by the main handler. Thus the messages are logged into the prod.log file instead of the prod.alert.log file.
When I leave the line channels: [!alert] as comment, the message is logged to prod.log. When I activate this line by removing the comment, the message is not logged at all (main handler ignores the channel correctly).
What have I to to, in order to use a specific handler to target a specific log file, mailer, etc? Messages to alert channel should be handled by the alert handler while all other handlers are ignored.
Use special service created for Monolog handler:
services:
some_service:
class: Namespace\Some\Service
arguments: ["#monolog.logger.alert"]