Symfony4 资源控制器
Symfony4 resource controller
我正在 symfony4 下开发一个 API,我希望我可以创建一个父控制器,我可以用它来调用将在另一个控制器中重复的函数。这是我想从父控制器扩展的控制器:
我的 DeliveryController:
<?php
namespace App\Controller;
use App\Entity\DeliveryMan;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class AuthController
* @package App\Controller
* @Route("/api")
*/
class DeliveryController extends AbstractController
{
/**
* @Route(
* name="api_delivery_man_post",
* path="/delivery_man",
* methods={"POST"},
* defaults={
* "_api_resource_class"=DeliveryMan::class,
* "_api_collection_operation_name"="post"
* }
* )
*/
public function postAction(DeliveryMan $data, UserPasswordEncoderInterface $encoder): DeliveryMan
{
return $this->encodePassword($data, $encoder);
}
protected function encodePassword(DeliveryMan $data, UserPasswordEncoderInterface $encoder): DeliveryMan
{
$encoded = $encoder->encodePassword($data, $data->getPassword());
$data->setPassword($encoded);
return $data;
}
}
我的授权控制器:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Entity\User;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
/**
* Class AuthController
* @package App\Controller
* @Route("/api")
*/
class AuthController extends AbstractController
{
/**
* @Route(
* name="api_users_post",
* path="/users",
* methods={"POST"},
* defaults={
* "_api_resource_class"=User::class,
* "_api_collection_operation_name"="post"
* }
* )
*/
public function postAction(User $data, UserPasswordEncoderInterface $encoder): User
{
return $this->encodePassword($data, $encoder);
}
protected function encodePassword(User $data, UserPasswordEncoderInterface $encoder): User
{
$encoded = $encoder->encodePassword($data, $data->getPassword());
$data->setPassword($encoded);
return $data;
}
}
可以看出,我在 2 个不同的控制器中调用了 2 个相同的动作,唯一的区别是实体和道路的路径。
所以我正在考虑创建一个从 AbstractController 扩展的 ResourceController 父控制器,并且子控制器将从 ResourceController 扩展,但是我不知道如何在我的父控制器中创建我的方法并检索它们在子控制器中。
如果有人已经这样做了,我是接受者:)谢谢你的帮助。
编辑结果资源控制器:
<?php
namespace App\Controller;
use App\Entity\DeliveryMan;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
class ResourcesController extends AbstractController
{
private $encoder;
public function __construct(UserPasswordEncoderInterface $encoder)
{
$this->encoder = $encoder;
}
public function encodePassword(User $data): User
{
$encoded = $this->encoder->encodePassword($data, $data->getPassword());
$data->setPassword($encoded);
return $data;
}
public function encodePasswordDelivery(DeliveryMan $data): DeliveryMan
{
$encoded = $this->encoder->encodePassword($data, $data->getPassword());
$data->setPassword($encoded);
return $data;
}
}
只需创建一个扩展 Symfony AbstractController 的 ResourceController。
在这里写下你的 2 个共享方法,然后在任何扩展 ResourceController 的控制器中,你可以像通常调用 class 方法一样调用它们:使用 $this
class ResourceController extends AbstractController
{
private $encoder;
public function __construct(UserPasswordEncoderInterface $encoder)
{
$this->encoder = $encoder;
}
public function encodePassword(Object $data): Object
{
$encoded = $this->encoder->encodePassword($data, $data->getPassword());
$data->setPassword($encoded);
return $data;
}
}
class AuthController extends ResourceController
{
public function someAction(User $data)
{
return $this->encodePassword($data);
}
}
我还建议您编写一个接口,其中包含 User 和 DeliveryMan 将实现的 getPassword 方法。您不仅可以确保实现该方法,而且还可以输入提示,例如 AuthenticatedEntityInterface,而不是 Object
我正在 symfony4 下开发一个 API,我希望我可以创建一个父控制器,我可以用它来调用将在另一个控制器中重复的函数。这是我想从父控制器扩展的控制器:
我的 DeliveryController:
<?php
namespace App\Controller;
use App\Entity\DeliveryMan;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class AuthController
* @package App\Controller
* @Route("/api")
*/
class DeliveryController extends AbstractController
{
/**
* @Route(
* name="api_delivery_man_post",
* path="/delivery_man",
* methods={"POST"},
* defaults={
* "_api_resource_class"=DeliveryMan::class,
* "_api_collection_operation_name"="post"
* }
* )
*/
public function postAction(DeliveryMan $data, UserPasswordEncoderInterface $encoder): DeliveryMan
{
return $this->encodePassword($data, $encoder);
}
protected function encodePassword(DeliveryMan $data, UserPasswordEncoderInterface $encoder): DeliveryMan
{
$encoded = $encoder->encodePassword($data, $data->getPassword());
$data->setPassword($encoded);
return $data;
}
}
我的授权控制器:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Entity\User;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
/**
* Class AuthController
* @package App\Controller
* @Route("/api")
*/
class AuthController extends AbstractController
{
/**
* @Route(
* name="api_users_post",
* path="/users",
* methods={"POST"},
* defaults={
* "_api_resource_class"=User::class,
* "_api_collection_operation_name"="post"
* }
* )
*/
public function postAction(User $data, UserPasswordEncoderInterface $encoder): User
{
return $this->encodePassword($data, $encoder);
}
protected function encodePassword(User $data, UserPasswordEncoderInterface $encoder): User
{
$encoded = $encoder->encodePassword($data, $data->getPassword());
$data->setPassword($encoded);
return $data;
}
}
可以看出,我在 2 个不同的控制器中调用了 2 个相同的动作,唯一的区别是实体和道路的路径。
所以我正在考虑创建一个从 AbstractController 扩展的 ResourceController 父控制器,并且子控制器将从 ResourceController 扩展,但是我不知道如何在我的父控制器中创建我的方法并检索它们在子控制器中。
如果有人已经这样做了,我是接受者:)谢谢你的帮助。
编辑结果资源控制器:
<?php
namespace App\Controller;
use App\Entity\DeliveryMan;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
class ResourcesController extends AbstractController
{
private $encoder;
public function __construct(UserPasswordEncoderInterface $encoder)
{
$this->encoder = $encoder;
}
public function encodePassword(User $data): User
{
$encoded = $this->encoder->encodePassword($data, $data->getPassword());
$data->setPassword($encoded);
return $data;
}
public function encodePasswordDelivery(DeliveryMan $data): DeliveryMan
{
$encoded = $this->encoder->encodePassword($data, $data->getPassword());
$data->setPassword($encoded);
return $data;
}
}
只需创建一个扩展 Symfony AbstractController 的 ResourceController。
在这里写下你的 2 个共享方法,然后在任何扩展 ResourceController 的控制器中,你可以像通常调用 class 方法一样调用它们:使用 $this
class ResourceController extends AbstractController
{
private $encoder;
public function __construct(UserPasswordEncoderInterface $encoder)
{
$this->encoder = $encoder;
}
public function encodePassword(Object $data): Object
{
$encoded = $this->encoder->encodePassword($data, $data->getPassword());
$data->setPassword($encoded);
return $data;
}
}
class AuthController extends ResourceController
{
public function someAction(User $data)
{
return $this->encodePassword($data);
}
}
我还建议您编写一个接口,其中包含 User 和 DeliveryMan 将实现的 getPassword 方法。您不仅可以确保实现该方法,而且还可以输入提示,例如 AuthenticatedEntityInterface,而不是 Object