在控制器中与存储库一起使用模型

据我了解,使用存储库限制控制器访问数据库层,并且所有查询都通过存储库。但是控制器可以使用模型(laravel 可以在控制器中注入模型而不是 ID)将其传递到存储库或服务 - 例如在用户之间进行交易?或者更好地将 ID 发送到存储库,以查找用户并应用业务逻辑(用户是否有钱,或者他是否被禁止)。

更一般的问题是,您可以使用存储库之外的模型吗?因为如果您将某些表从 postgres 或 mysql 更改为其他表,您的模型也会更改。这意味着你的存储库应该有 get 方法来发回一些 DTO 对象?


慕村9548890
浏览 115回答 0
0回答

蝴蝶刀刀

注意:这是对此事的一般看法,适用于任何基于 MVC 的应用程序,而不仅仅是 Laravel。一个基于MVC模式的应用程序应该由三部分组成:交付机制:UI逻辑(用户请求处理和服务器响应创建),服务层:应用程序逻辑,领域模型:业务逻辑。以下是一些图形表示(我自己制作的):如上所示(并在下面的资源中详细描述),控制器和视图是交付机制的一部分。它们应该仅通过服务层对象(服务)与领域模型交互。因此,他们不应该了解域模型组件(实体 - 也称为域对象、数据映射器、存储库等)。更重要的是,控制器应该只有一项职责:将用户请求的值传递给服务层,以便它更新模型。所以,回答你的第一个问题:不,控制器不应该能够创建域模型元素的任何实例(因此你所谓的“模型”的实例 - 就 Laravel 的Active Record而言),甚至不能传递这些对象到其他组件(如存储库、服务等)。相反,控制器应该只将请求的值(例如,用户 id )传递给相应的服务。然后,这些服务将创建适当的域模型对象并使用适当的存储库、数据映射器等,以便保存到数据库或从数据库获取。至于第二个问题(如果我理解正确的话):存储库被视为实体的集合- 它们是域模型组件。因此,元素(例如实体实例)可以被获取、存储、更改或从其中删除。因此,根据定义,实体必须与存储库分开定义/使用。对于 Laravel,同样适用:“模型”应与存储库分开定义/使用。“通用”MVC 实现(为了更清楚):控制器:<?phpnamespace MyApp\UI\Web\Controller\Users;use MyApp\Domain\Service\Users;use Psr\Http\Message\ServerRequestInterface;/**&nbsp;* Add a user.&nbsp;*/class AddUser {&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* User service.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @var Users&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private $userService;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @param Users $userService User service.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function __construct(Users $userService) {&nbsp; &nbsp; &nbsp; &nbsp; $this->userService = $userService;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Invoke.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @param ServerRequestInterface $request Request.&nbsp; &nbsp; &nbsp;* @return void&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function __invoke(ServerRequestInterface $request) {&nbsp; &nbsp; &nbsp; &nbsp; // Read request values.&nbsp; &nbsp; &nbsp; &nbsp; $username = $request->getParsedBody()['username'];&nbsp; &nbsp; &nbsp; &nbsp; // Call the corresponding service.&nbsp; &nbsp; &nbsp; &nbsp; $this->userService->addUser($username);&nbsp; &nbsp; }}服务:<?phpnamespace MyApp\Domain\Service;use MyApp\Domain\Model\User\User;use MyApp\Domain\Model\User\UserCollection;use MyApp\Domain\Service\Exception\UserExists;/**&nbsp;* Service for handling the users.&nbsp;*/class Users {&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* User collection (a repository).&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @var UserCollection&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private $userCollection;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @param UserCollection $userCollection User collection.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function __construct(UserCollection $userCollection) {&nbsp; &nbsp; &nbsp; &nbsp; $this->userCollection = $userCollection;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Find a user by id.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @param int $id User id.&nbsp; &nbsp; &nbsp;* @return User|null User.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function findUserById(int $id) {&nbsp; &nbsp; &nbsp; &nbsp; return $this->userCollection->findUserById($id);&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Find all users.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @return User[] User list.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function findAllUsers() {&nbsp; &nbsp; &nbsp; &nbsp; return $this->userCollection->findAllUsers();&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Add a user.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @param string $username Username.&nbsp; &nbsp; &nbsp;* @return User User.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function addUser(string $username) {&nbsp; &nbsp; &nbsp; &nbsp; $user = $this->createUser($username);&nbsp; &nbsp; &nbsp; &nbsp; return $this->storeUser($user);&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Create a user.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @param string $username Username.&nbsp; &nbsp; &nbsp;* @return User User.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private function createUser(string $username) {&nbsp; &nbsp; &nbsp; &nbsp; $user = new User();&nbsp; &nbsp; &nbsp; &nbsp; $user->setUsername($username);&nbsp; &nbsp; &nbsp; &nbsp; return $user;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Store a user.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @param User $user User.&nbsp; &nbsp; &nbsp;* @return User User.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private function storeUser(User $user) {&nbsp; &nbsp; &nbsp; &nbsp; if ($this->userCollection->userExists($user)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new UserExists('Username "' . $user->getUsername() . '" already used');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $this->userCollection->storeUser($user);&nbsp; &nbsp; }}存储库:<?phpnamespace MyApp\Domain\Infrastructure\Repository\User;use MyApp\Domain\Model\User\User;use MyApp\Domain\Infrastructure\Mapper\User\UserMapper;use MyApp\Domain\Model\User\UserCollection as UserCollectionInterface;/**&nbsp;* User collection.&nbsp;*/class UserCollection implements UserCollectionInterface {&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* User mapper (a data mapper).&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @var UserMapper&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private $userMapper;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @param UserMapper $userMapper User mapper.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function __construct(UserMapper $userMapper) {&nbsp; &nbsp; &nbsp; &nbsp; $this->userMapper = $userMapper;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Find a user by id.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @param int $id User id.&nbsp; &nbsp; &nbsp;* @return User|null User.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function findUserById(int $id) {&nbsp; &nbsp; &nbsp; &nbsp; return $this->userMapper->fetchUserById($id);&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Find all users.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @return User[] User list.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function findAllUsers() {&nbsp; &nbsp; &nbsp; &nbsp; return $this->userMapper->fetchAllUsers();&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Store a user.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @param User $user User.&nbsp; &nbsp; &nbsp;* @return User User.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function storeUser(User $user) {&nbsp; &nbsp; &nbsp; &nbsp; return $this->userMapper->saveUser($user);&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Check if the given user exists.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @param User $user User.&nbsp; &nbsp; &nbsp;* @return bool True if user exists, false otherwise.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function userExists(User $user) {&nbsp; &nbsp; &nbsp; &nbsp; return $this->userMapper->userExists($user);&nbsp; &nbsp; }}实体:<?phpnamespace MyApp\Domain\Model\User;/**&nbsp;* User.&nbsp;*/class User {&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Id.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @var int&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private $id;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Username.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @var string&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private $username;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Get id.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @return int&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function getId() {&nbsp; &nbsp; &nbsp; &nbsp; return $this->id;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Set id.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @param int $id Id.&nbsp; &nbsp; &nbsp;* @return $this&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function setId(int $id) {&nbsp; &nbsp; &nbsp; &nbsp; $this->id = $id;&nbsp; &nbsp; &nbsp; &nbsp; return $this;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Get username.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @return string&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function getUsername() {&nbsp; &nbsp; &nbsp; &nbsp; return $this->username;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Set username.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @param string $username Username.&nbsp; &nbsp; &nbsp;* @return $this&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function setUsername(string $username) {&nbsp; &nbsp; &nbsp; &nbsp; $this->username = $username;&nbsp; &nbsp; &nbsp; &nbsp; return $this;&nbsp; &nbsp; }}数据映射器:<?phpnamespace MyApp\Domain\Infrastructure\Mapper\User;use PDO;use MyApp\Domain\Model\User\User;use MyApp\Domain\Infrastructure\Mapper\User\UserMapper;/**&nbsp;* PDO user mapper.&nbsp;*/class PdoUserMapper implements UserMapper {&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Database connection.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @var PDO&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private $connection;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @param PDO $connection Database connection.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function __construct(PDO $connection) {&nbsp; &nbsp; &nbsp; &nbsp; $this->connection = $connection;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Fetch a user by id.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* Note: PDOStatement::fetch returns FALSE if no record is found.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @param int $id User id.&nbsp; &nbsp; &nbsp;* @return User|null User.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function fetchUserById(int $id) {&nbsp; &nbsp; &nbsp; &nbsp; $sql = 'SELECT * FROM users WHERE id = :id LIMIT 1';&nbsp; &nbsp; &nbsp; &nbsp; $statement = $this->connection->prepare($sql);&nbsp; &nbsp; &nbsp; &nbsp; $statement->execute([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'id' => $id,&nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; &nbsp; &nbsp; $data = $statement->fetch(PDO::FETCH_ASSOC);&nbsp; &nbsp; &nbsp; &nbsp; return ($data === false) ? null : $this->convertDataToUser($data);&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Fetch all users.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @return User[] User list.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function fetchAllUsers() {&nbsp; &nbsp; &nbsp; &nbsp; $sql = 'SELECT * FROM users';&nbsp; &nbsp; &nbsp; &nbsp; $statement = $this->connection->prepare($sql);&nbsp; &nbsp; &nbsp; &nbsp; $statement->execute();&nbsp; &nbsp; &nbsp; &nbsp; $data = $statement->fetchAll(PDO::FETCH_ASSOC);&nbsp; &nbsp; &nbsp; &nbsp; return $this->convertDataToUserList($data);&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Check if a user exists.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* Note: PDOStatement::fetch returns FALSE if no record is found.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @param User $user User.&nbsp; &nbsp; &nbsp;* @return bool True if the user exists, false otherwise.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function userExists(User $user) {&nbsp; &nbsp; &nbsp; &nbsp; $sql = 'SELECT COUNT(*) as cnt FROM users WHERE username = :username';&nbsp; &nbsp; &nbsp; &nbsp; $statement = $this->connection->prepare($sql);&nbsp; &nbsp; &nbsp; &nbsp; $statement->execute([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ':username' => $user->getUsername(),&nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; &nbsp; &nbsp; $data = $statement->fetch(PDO::FETCH_ASSOC);&nbsp; &nbsp; &nbsp; &nbsp; return ($data['cnt'] > 0) ? true : false;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Save a user.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @param User $user User.&nbsp; &nbsp; &nbsp;* @return User User.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function saveUser(User $user) {&nbsp; &nbsp; &nbsp; &nbsp; return $this->insertUser($user);&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Insert a user.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @param User $user User.&nbsp; &nbsp; &nbsp;* @return User User.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private function insertUser(User $user) {&nbsp; &nbsp; &nbsp; &nbsp; $sql = 'INSERT INTO users (username) VALUES (:username)';&nbsp; &nbsp; &nbsp; &nbsp; $statement = $this->connection->prepare($sql);&nbsp; &nbsp; &nbsp; &nbsp; $statement->execute([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ':username' => $user->getUsername(),&nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; &nbsp; &nbsp; $user->setId($this->connection->lastInsertId());&nbsp; &nbsp; &nbsp; &nbsp; return $user;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Update a user.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @param User $user User.&nbsp; &nbsp; &nbsp;* @return User User.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private function updateUser(User $user) {&nbsp; &nbsp; &nbsp; &nbsp; $sql = 'UPDATE users SET username = :username WHERE id = :id';&nbsp; &nbsp; &nbsp; &nbsp; $statement = $this->connection->prepare($sql);&nbsp; &nbsp; &nbsp; &nbsp; $statement->execute([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ':username' => $user->getUsername(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ':id' => $user->getId(),&nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; &nbsp; &nbsp; return $user;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Convert the given data to a user.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @param array $data Data.&nbsp; &nbsp; &nbsp;* @return User User.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private function convertDataToUser(array $data) {&nbsp; &nbsp; &nbsp; &nbsp; $user = new User();&nbsp; &nbsp; &nbsp; &nbsp; $user&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->setId($data['id'])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->setUsername($data['username'])&nbsp; &nbsp; &nbsp; &nbsp; ;&nbsp; &nbsp; &nbsp; &nbsp; return $user;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Convert the given data to a list of users.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @param array $data Data.&nbsp; &nbsp; &nbsp;* @return User[] User list.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private function convertDataToUserList(array $data) {&nbsp; &nbsp; &nbsp; &nbsp; $userList = [];&nbsp; &nbsp; &nbsp; &nbsp; foreach ($data as $item) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $userList[] = $this->convertDataToUser($item);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $userList;&nbsp; &nbsp; }}看法:<?phpnamespace MyApp\UI\Web\View\Users;use MyApp\UI\Web\View\View;use MyApp\Domain\Service\Users;use MyLib\Template\TemplateInterface;use Psr\Http\Message\ResponseInterface;use Psr\Http\Message\ResponseFactoryInterface;/**&nbsp;* Add a user.&nbsp;*/class AddUser extends View {&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* User service.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @var Users&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private $userService;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @param ResponseFactoryInterface $responseFactory Response factory.&nbsp; &nbsp; &nbsp;* @param TemplateInterface $template Template.&nbsp; &nbsp; &nbsp;* @param Users $userService User service.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function __construct(ResponseFactoryInterface $responseFactory, TemplateInterface $template, Users $userService) {&nbsp; &nbsp; &nbsp; &nbsp; parent::__construct($responseFactory, $template);&nbsp; &nbsp; &nbsp; &nbsp; $this->userService = $userService;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Display a form for adding a user.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @return ResponseInterface Response.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function index() {&nbsp; &nbsp; &nbsp; &nbsp; $body = $this->template->render('@Template/Users/add-user.html.twig', [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'activeMainMenuItem' => 'addUser',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'action' => '',&nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $response = $this->responseFactory->createResponse();&nbsp; &nbsp; &nbsp; &nbsp; $response->getBody()->write($body);&nbsp; &nbsp; &nbsp; &nbsp; return $response;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Add a user.&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @return ResponseInterface Response.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function addUser() {&nbsp; &nbsp; &nbsp; &nbsp; $body = $this->template->render('@Template/Users/add-user.html.twig', [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'activeMainMenuItem' => 'addUser',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'message' => 'User successfully added.',&nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; &nbsp; &nbsp; $response = $this->responseFactory->createResponse();&nbsp; &nbsp; &nbsp; &nbsp; $response->getBody()->write($body);&nbsp; &nbsp; &nbsp; &nbsp; return $response;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP