Shopware 6: Get PHP data in own custom admin module - symfony

I have build my own Plugin in Shopware 6. I allready have a custom module with custom route. Now I want to add data from my custom database table to my custom routes html.twig.
My Route: http://localhost:8888/admin#/ankauf/module/overview
My Database Table: product_reservation
I have build my own controller but I can't get this controller to listen to my route. Maybe because my route is build from the module? The path in my module is: ankauf.module.overview
Is a controller the right way? And if yes, how can it listen to my path and don't overwrite it with his own route?
Is there a better way to push PHP Code to my custom Backend path?

If you have created an own entity with definition etc (like here or here described) there is an easy to get your data to an admin module. Shopware will automatically register routes for CRUD operations on your entity. So the entity is automatically available via the admin API. On the admin side there are also helper services to read out your API. In your vue.js component you need to inject the
inject: [
'repositoryFactory'
],
With this factory you are able to create a repository which requests your custom entity API route.
{
...
created() {
this.repository = this.repositoryFactory.create('product_reservation');
}
}
The repository has several methods like search, get, create, delete, etc.
With these methods you are able to read out your data in the vue.js component of your plugin and bring it to your module
Read more about an own module here

Related

Prestashop : Decorate a module's service from another module

I want to use the module ps_facebook to send a catalog to Facebook but I need to change the name of the product sent.
I already sought and I found that's the module ps_eventbus that manages to load and decorate datas.
Unfortunately there is no hook. My only possibility is to rewrite a part of this service.
This service is loaded by the module ps_eventbus in the file decorate.yml and I want to change the call of this service by our specific service in our module.
So I wonder if it's possible to override properly the service PrestaShop\Module\PsEventbus\Decorator\ProductDecorator and how to do that ?
I suggest you check the Symfony documentation, here
https://symfony.com/doc/3.4/service_container/service_decoration.html
As it is not directly related to the PrestaShop itself. If this service is public, you can probably decorate it, as shown in the docs.

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.

Programmatically register routes at runtime

I want to be able to annotate some of my controllers with #SomethingProvider so that they automatically serve POST /{original-resource-url}/something URL and tie this URL to a generic handler.
Using a controller with wildcard mapping would not work because not all the resources will have that "something" collection available. Ex:
POST /user/something
POST /product/something
etc.
but not
POST /anything/something
My idea is to scan my controllers at runtime and look for that #SomethingProvider annotation so I can programmatically register the additional "something" endpoints.
I know it has to do with RequestMappingHandlerMapping but I can't figure out how to tap into it to add mappings.
Thanks geniuses

symfony2 configure service with data from another service

Is it possible in Symfony2 to configure a service by injecting data from another service? For example, by calling a getter on another service?
In my specific case I am creating a (reusable) service that can handle translatable entity fields. For this I need a list of available locales in the application. I have looked at some other bundles that also work with locales, but they always use a static array from the configuration. For example:
a2lix_translation_form:
locales: [en, fr, nl]
This configuration usually ends up mapping to the service in the form of a constructor parameter or setter via the bundle configuration. For example:
class SomeService {
function __construct(array $locales) { ... }
// or
function setLocales(array $locales) { ... }
}
But in my case the list of available locales is not always static and often comes from the database. I have created a Locale service in my application with a method getLocales that returns an array. But how do I get that array into my service that needs it?
The service I am creating that needs a list of locales is split off into a separate reusable bundle. I don't want to inject the Locale service directly because that service is specific to the application, and not the bundle I am creating. I want users of my bundle to be able to provide a static list of locales, or point towards a service that has all the locales.
I would solve this problem using semantic configuration and config defintions. It works pretty similar to how FOSUserBundle asks for a driver and uses different settings depending on your choice (orm, mongodb, propel).
You could add something like this to your config.yml:
a2lix_locale:
provider: default # database
# ... additional settings which are optional,
# but required by provider, e.g. database settings
Your bundle's Configuration.php would verify that a valid provider was selected and that additional settings are set according to what each provider requires. Again, FOSUserBundle provides a great example for how to do this.
Additionally in your bundle's MyBundleExtension.php in /DependencyInjection you can access the service container and pass for instance the parameter locale to your default service in order for it to use the application's default locale provided in parameters.yml.

Managing routing while extending a controller

I am trying to override a controller that I use in Frontend:
AcmeDemoBundle:DemoController //route is /demo
For the backend, I extended this controller in AdminDemoBundle:
AdminDemoBundle:DemoController //route is /admin/demo
I haven't overriden any methods in the new controller yet, but all the routes from AcmeDemoController go to /admin/demo.
Does anyone know why?
Is there a solution to override frontend controllers for backend purposes?
Basically, by extending the controller, all the routes are redefined with the same name and a different prefix, which means that the routes are being replaced.
There is actually no point at all in extending a controller for the backend, as controllers are meant to be tiny. The logic of the app must be kept in services instead.
Moving the logic to a service allows a better organisation of the code. There is no need to worry about backend/frontend when talking about services. Just, create the logic in a service, and use the methods in either a frontend controller or a backend controller.

Resources