Mount/Use controller in console - symfony

In a Silex console command I want to perform a sub-request on a controller route. Unfortunately routes don't seem to be imported when executing a command by default.
I always get the following error:
Unable to generate a URL for the named route "xyz_subject_method" as
such route does not exist
Therefore I tried to mount the defining controller from within my Console class by using $this->mount('/prefix', Controller::mount($this)) inside the Console constructor. This has no effect.
Is there any way to access controllers directly from a console command?

After mounting a controller inside the Console constructor you need to flush the controller collection by using $this->flush();
So mounting the controller should look like this:
$this->mount('/prefix', Controller::mount($this));
$this-flush();

Related

Symfony redirect to dynamic route name

I'm using the Symfony CMF Routing Bundle to create dynamic routes (I'm using one example here):
$route = new Route('/dynamic-url');
$route->setMethods("GET");
$route->setDefault('_controller', 'AppBundle:MyRoute:getResponse');
$routeCollection->add('my-dynamic-route', $route);
The response is loaded from the getResponseAction() function inside the MyRouteController:
/**
* No annotations here, because I want the url to be dynamic from the database
*/
public function getResponseAction(Request $request) {
return $this->render('dynamic-page-template.html.twig');
}
When I go to '/dynamic-url', it works.
When in another controller, I want to redirect to this dynamic route, like this:
return $this->redirectToRoute('my-dynamic-route');
But I get this error: "None of the chained routers were able to generate route: Route 'my-dynamic-route' not found"
Also interesting: when I go to '/dynamic-url', the dev bar actually says that the Route name is 'my-dynamic-route'.
Edit
When I load all the routes, I don't see my dynamic route names:
$this->get('router')->getRouteCollection();
I think they should be in this list.
Since it's a dynamic route, which wasn't saved anywhere (like routing.yml ) it will be only availabe for Request where it has been defined. So at the end of Request your app will immediately "forget" about new Route generated at runtime.
When I load all the routes, I don't see my dynamic route names:
$this->get('router')->getRouteCollection();
I think they should be in this list.
Actualy No. It depends on where you call $this->get('router')->getRouteCollection();
Just try to call
dump($this->get('router')->getRouteCollection();)
right before the return statement in your Action where you're adding the my-dynamic-route route. I'm sure you'll see your my-dynamic-route in the list of routes, but if you call it anywhere else - you won't see it.
It's less about symfony rather about stateless nature of web (see Why say that HTTP is a stateless protocol?)
I started to think about this and pointed your question to an routing issue on symfony-cmf. You tagged with #symfony-cmf and i think this would be important feature for us.
I also think, when you persist your route with /my-route you should also ask the router for that name (or in case of the CMF with an content object with that a route.)
If you use the CmfRoutingBundle dynamic router, you should persist your routes to doctrine. The idea of dynamic here is that they can be created at runtime, e.g. in an admin interface or elsewhere by code. The DynamicRouter loads routes from the database.
If you use PHPCR-ODM, the route name is the repository id, typically something like /cms/routes/my-route and you can generate it with that name. If you have access to the route object loaded from the database, you can also generate from that, rather than the path. If you have to hardcode a path in your application, its an indication that probably a normally configured route would be better.
If you just add a route to the route collection on the fly, you would have to make sure that happens in each request, its only available when you add it. With the CMF ChainRouter you could register your own router that does that, but i would not know of a good use case for that. Either you know the route, then you can configure it in the routing.xml|yml|php config file. Or routes are loaded dynamically, in which point you should use DynamicRouter, potentially with a custom route loader.

Web Service Error path property must set before calling the send method

path property must set before calling the send method
What is the cause of this error?
Thanks
The usually means that the WSDL that you are using doesn't have a Service section defined to say where the web service is located. So the error is basically telling you that it doesn't know where to send the method call because there is no destination defined.
When you create an instance of the serivce try setting the Url property to the location of the service and then try calling the method again.

Access to Doctrine during Bundle Initialization

I have a Symfony2 bundle which I want to use database table which stores key value configuration parameters. I want to be able to load a query and cache it for a long time and be able to inject the configuration parameters into symfony2 service container.
Right now I am injecting a service which loads the configuration from doctrine, and calling a get($key) method to retrieve the value for the key I want.
I basically want these configuration options to be available from the symfony2 service container parameter bag.
Is there maybe an event I could tie into or some sort of compiler pass I can use with my bundle to achieve this?
I'll do something like that in your service listener
public function onLateKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
$mydata= $this->manager->getRepository('YourBundle:YourTable')->getAll();
$parameters['mydata'] = $mydata;
$request->attributes->add($parameters);
}
In your Controller, you can get your parameters :
$this->container->get('request')->attributes->get('mydata');

Is it possible to get the controller name in the output of Symfony2's router:debug command?

The Symfony2 console command router:debug produces a list of all the currently configured routes, including the name, HTTP method and pattern for each route.
Is it possible to also get the controller for each route in this list, without resorting to using router:debug [name] for each route and parsing out the controller from the "Defaults _controller:" line?
Examining the RouterDebugCommand class showed that this wasn't possible, so I made a change to the outputRoutes method: https://gist.github.com/3380250

OVerride a url mapping defined in a controller present in a jar

I am using spring-security-oauth2 jar file. During the oauth verification process it makes a call to "/oauth/token" and hence the method. I want to modify the method..
Is it possible if i can override the url mapping already present? or exlcude the class from jar..
Thanks
Harshit

Resources