I've installed SonataAdminBundle and created custom CRUD controller for one entity. I want to perform some action after entity updated. How can I achieve this using CRUD controller's? I've noticed, that Admin* classes have preUpdate and postUpdate methods for such purpose, I could use them, but I guess, that logic should be placed in controllers.
The easiest way I see it's rewriting editAction (override this method in my controller, copy/paste code from base CRUDController and add calling own postUpdate), but copy/paste it's bad:) Maybe I missed some way?
If you review code written in sonata admin's editAction() in CRUDController you can see its calling admin's update() method.
$object = $this->admin->update($object);
you can review update() method in sonata base admin class before calling model manager to persist object it has a preUpdate() call and same case for postUpdate() after calling model manager.
That means the one you are trying to implement pre or post actions for your entity you have to write your own logic but the question is why you want to redo or rewrite any available action ? you can use already provided pre or post hooks.
Related
I am building a plugin for Shopware 6 and cannot seem to find any documentation as to how to extend an existing controller action. I found this How to add an Action to Account Controller in Shopware but it seems to refer to Shopware 5 and I am not sure I can use it that way in Shopware 6.
The controller action I want to extend is \Shopware\Storefront\Controller\AddressController::saveAddress - in my case I want to add custom address validation that would use a service in my plugin where a request to 3rd party API would be made, if the address is correct then allow the address, if not then return an error. Perhaps it is better to instead extend \Shopware\Core\Checkout\Customer\SalesChannel\AddressService::save but I have no clue for now (I am new to Shopware in general). Extending the service would mean I do not have to override the whole action logic so that it contains my check in the middle. Or perhaps there is an event I can use for address saving (same thing, can't find a good source/list of events for Shopware6).
There seem to be guides here:
https://docs.shopware.com/en/shopware-platform-dev-en/developer-guide/controller
and here:
https://docs.shopware.com/en/shopware-platform-dev-en/how-to/custom-storefront-controller
but these only describe how to make a new controller and it is not very useful to me since I do not want to add any new routes but use the existing one /account/address/create.
I would be very grateful for a code example of how to register the override in the plugin (config, xml) and how would the extending class look like. If it is not too much to ask the ideal answer would contain an example of:
How to extend an action for existing controller.
How to extend an existing service.
Where to find which event is firing in a controller/service, subscribe to it and make it override default behaviour (like throw Shopware\Core\Framework\Validation\Exception\ConstraintViolationException).
It does not make sense to extend/override action, as action should be as thin as it is possible, and all business logic should be in service.
To extend existing service you can decorate it or subscribe to some events to extend functionality. See https://docs.shopware.com/en/shopware-platform-dev-en/how-to/decorating-a-service
In your case, you can subscribe on framework.validation.address.create or/and framework.validation.address.update events to extend list of constraints. In general all validation events have prefix framework.validation. second part is defined in \Shopware\Core\Framework\Validation\DataValidationFactoryInterface implementation in your case it is \Shopware\Core\Checkout\Customer\Validation\AddressValidationFactory
As the heading to this question states how does one go about adding/integration a View class to a plugin in Cakephp 3?
There is the AppView class for the application but when baking a plugin no View class is included.
There is no View class baked into plugin, because by default plugins are using View class from main application.
If you need to do some specific view-rendering logic in your plugin, you can create eg. YourPluginDir/src/View/CustomView.php and do your things there. You need also to tell your controller to use this View class:
$this->viewBuilder()->setClassName('YourPlugin.Custom');
More info about custom View classes can be found in docs:
Creating your own view classes in CakePHP
i am using OctoberCMS for a project and in the backend, i want to know how to override the create, update, delete actions in the controller.
Actually, i developed a custom form widget which is a full page calender and i want to use the deffered binding as the model i am trying to save this calender is not exists at the point of creation.
So, any help regarding "Deffered Binding" or "Controller Actions Override" would be appreciated.
I refered the documenation but its not much helpful.
Thanks
You can use your own logic for the create, update or preview action method in the controller, then optionally call the Form behavior parent method
check this link:
https://octobercms.com/docs/backend/forms#overriding-action
I want to have a controller to do some initial settings and fetch some basic data in all of my application pages (written with PHP and CodeIgniter). As I'm new in CI, please help me to do it.
Some of my goals to have such controller:
Setup submenus (regardless of the current page)
Check user login status.
Notice:
I don't have any problem on creating and working with general Controllers in CI. What I need is something like a hidden controller that does not any need to URI segments to run, and it is not the CI default controller too.
What you can do is create a base controller and and extend that base controller in every other controllers instead of CI_Controller. Your base controller will extend CI_Controller. You can add your required checks in the constructor of the base controller and do stuffs according to that. So every time you call a controller your checks will be done. But you have autoload the base controller. I hope this helps you.
Let us say I have 2 controllers, the Users controller and the Admin controller. In Users, I am validating a form to check if the user credentials is correct so I created the callback_confirmUser function in form_validation.php and put the confirmUser function in the Users Controller. How can I use this function inside the Admin controller for form validation too?
The only way to do that is to override from the Admin controller. But I don't think that that's the way to go.
Why don't you create a library for that? It's also possible to extend the CI form validation library in case you miss functionality.
See also: http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html and http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html
Good luck!
Edit: it's also possible to use a core controller. You create in the 'core' folder the controller 'MY_Controller.php' that extends the CI_Controller. Instead of extending your controllers from CI_Controller, you extend from MY_Controller. It's than possible to put your validate function in MY_Controller and use it in both admin and user controller.