扩展 Sonata 用户包和自定义操作
Posted
技术标签:
【中文标题】扩展 Sonata 用户包和自定义操作【英文标题】:Extending Sonata User Bundle & custom actions 【发布时间】:2017-04-22 10:45:21 【问题描述】:我有一个带有 Sonata Admin、Sonata User 和 Fosuser 的应用程序。 我已经从 Sonata Admin 扩展了 Admin 文件以添加新的列和字段:
<?php
namespace AppBundle\Admin;
use Sonata\UserBundle\Admin\Model\UserAdmin as SonataUserAdmin;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
class UserAdmin extends SonataUserAdmin
/**
* @inheritdoc
*/
protected function configureFormFields(FormMapper $formMapper)
parent::configureFormFields($formMapper);
$formMapper
->with('Others')
->add('company')
->add('locations', 'sonata_type_collection', array(
'required' => false
), array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
))
->end()
;
// Fields to be shown on filter forms
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
parent::configureDatagridFilters($datagridMapper);
$datagridMapper
->remove('email')
->add('firstname')
->add('lastname')
->add('company.name')
;
// Fields to be shown on lists
protected function configureListFields(ListMapper $listMapper)
unset($this->listModes['mosaic']);
$listMapper
->add('company.name')
->add('firstname')
->add('lastname')
;
parent::configureListFields($listMapper);
$listMapper->remove('email');
$listMapper->add('_action', 'actions', array(
'actions' => array(
'Invoiced' => array(
'template' => 'AppBundle:User:list__action_invoiced.html.twig'
),
'Credited' => array(
'template' => 'AppBundle:user:list__action_credited.html.twig'
)
)
));
protected function configureRoutes(RouteCollection $collection)
parent::configureRoutes($collection);
$collection->add('invoiced', $this->getRouterIdParameter().'/invoiced');
$collection->add('credited', $this->getRouterIdParameter().'/credited');
现在,我对“开票”和“贷记”的新操作有疑问...
Controller "Sonata\AdminBundle\Controller\CRUDController::invoicedAction" for URI "/admin/app/user/1/invoiced" is not callable.
此配置适用于其他管理页面,但不适用于此 SonataUserAdmin 扩展。 在其他页面中我扩展了“Sonata\AdminBundle\Admin\Admin”,但在这里我需要扩展“Sonata\UserBundle\Admin\Model\UserAdmin”以使用 fos 的用户系统...
你有什么想法吗???
谢谢
【问题讨论】:
【参考方案1】:您已经配置了服务。现在您必须创建一个 UserController
来扩展 CRUDController
并实现您的 invoicedAction
和 creditedAction
方法。
<?php
namespace AppBundle\Controller;
use AppBundle\Entity\User;
use Sonata\AdminBundle\Controller\CRUDController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
class UserController extends CRUDController
/**
*
* @param string $id
* @param Request $request
*
* @return RedirectResponse
*/
public function invoicedAction($id = null, Request $request = null)
if ( $request == null )
$request = $this->getRequest();
$id = $request->get($this->admin->getIdParameter());
$user = $this->admin->getObject($id);
/* @var $user User */
if (!$user)
throw $this->createNotFoundException(sprintf('unable to find the user with id : %s', $id));
$this->admin->checkAccess('invoiced', $user);
$this->admin->setSubject($user);
/// your code here...
return new RedirectResponse($this->admin->generateUrl('show', array('id' => $user->getId())));
【讨论】:
【参考方案2】:我想你只需要重新配置the baseControllerName
argument的值
无论您的控制器是什么。您可以在 admin.yml
配置文件中执行此操作。
【讨论】:
【参考方案3】:我有这个
admin.user:
class: AppBundle\Admin\UserAdmin
arguments: [~, AppBundle\Entity\User, AppBundle:User]
tags:
- name: sonata.admin, manager_type: orm, label: User, group: Users
calls:
- [ setTranslationDomain, [AppBundle]]
【讨论】:
【参考方案4】:总是一样..
Controller "Sonata\AdminBundle\Controller\CRUDController::invoicedAction" for URI "/admin/app/user/1/invoiced" is not callable.
:-(
【讨论】:
【参考方案5】:您需要在 sonata_user 部分下设置您的控制器 见:
https://sonata-project.org/bundles/user/master/doc/reference/advanced_configuration.html
app/config/config.yml
sonata_user:
admin:
user:
controller: AppBundle:User
【讨论】:
【参考方案6】:(Symfony4.1 + Symfony Flex) 在你的 userAdmin 类的 services.yaml 中注册服务是不必要的。如果您这样做了 - 您的管理面板上将有两个实体。 要覆盖基本的 userAdmin 类,请确保您的 config/packages/sonata.yaml 看起来像这样(覆盖就足够了):
sonata_user:
security_acl: false
manager_type: orm
class:
user: App\Entity\Manager
group: App\Entity\Group
admin:
user:
class: App\Admin\UserAdmin
controller: App\Controller\InitController
translation: SonataUserBundle
【讨论】:
以上是关于扩展 Sonata 用户包和自定义操作的主要内容,如果未能解决你的问题,请参考以下文章