Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在我的实体类中使用URL ZF2_Url_Hyperlink_Frameworks_Zend Framework2 - Fatal编程技术网

如何在我的实体类中使用URL ZF2

如何在我的实体类中使用URL ZF2,url,hyperlink,frameworks,zend-framework2,Url,Hyperlink,Frameworks,Zend Framework2,我必须在发送给我的用户的电子邮件中插入链接。所以我有一个实体类来发送这封邮件。但我不知道如何使用ZF2视图/控制器系统的“url”方法创建此链接 我的班级是: class UserEntity { public function sendMail($user) { $link = $unknow->url("route",array("param" => "param")); //how can create this ? $text = "clic

我必须在发送给我的用户的电子邮件中插入链接。所以我有一个实体类来发送这封邮件。但我不知道如何使用ZF2视图/控制器系统的“url”方法创建此链接

我的班级是:

class UserEntity
{
  public function sendMail($user)
  {
      $link = $unknow->url("route",array("param" => "param")); //how can create this ? 
      $text = "click here $link";
      $this->sendMail($to,$text);
  }
}

你能帮我吗?感谢

在设计方面,让您的域模型负责创建URL(或任何其他不以最简单的术语描述实体的内容)

我将创建一个
UserService
,它将封装a
SendMail
函数,其中
UserEntity
可以作为参数传递,它是用于发送电子邮件的
email
属性

class UserService {

   protected $mailService;

   public function __construct(MailService $mailService) {
      $this->mailService = $mailService;
   }

   public function sendUserEmail(UserEntity $user, $message) {

      $this->mailService->send($user->getEmail(), $message);
   }  
}
邮件服务可以是封装
Zend\mail\Transport
实例的另一个服务

您的控制器将使用
UserService
将邮件发送给正确的用户

$message
需要包含使用

类UserController扩展了AbstractActionController{ 受保护的$userService; 公共函数构造(UserService$UserService){ $this->userService=$userService; } 公共函数sendEmailAction(){ //从路由参数或表单post数据加载$user $user=$this->userService->findUserByTheirId($this->params('id')); //生成url $url=$this->url()->fromRoute('user/foo',array('bar'=>'param1')); $message=sprintf('这是电子邮件文本!',$url); $this->userService->sendUserEmail($user,$message); } }
这些都是人为的例子,但我的观点是,您应该只将信息存储在实体中,这样您就可以用它来“做事情”,而不是在实体中。“这是一个非常非常错误的地方来做这样的事情。”。实体是数据对象,而且(几乎)仅此而已。最好将其外包到服务中,并通过ViewHelperManager注入URL ViewHelper。感谢您的answare。例如,如果我想发送邮件来验证用户的邮件地址,我应该在我的控制器中写下这个:public function registrationAction(){if($this->userService->addUser($data)){$this->sendMail($data->user_email);}}}public function sendMail($email){$url=$this->url()->fromRoute(“verifyLink”);$text=“要验证您的地址,请单击此处$url”;$this->userService->sendMail($email,$text);}看起来不错;请记住,我的示例不包括正常验证,因此当您从用户处获取电子邮件地址时,您应该使用正确验证的表单数据
$form->isValid()==true
然后创建用户实体并发送电子邮件或任何您需要的内容。
class UserController extends AbstractActionController {
   protected $userService;

   public function __construct(UserService $userService) {
     $this->userService = $userService;
   }  

   public function sendEmailAction() {
     // load $user from route params or form post data
     $user = $this->userService->findUserByTheirId($this->params('id'));         

     // Generate the url
     $url = $this->url()->fromRoute('user/foo', array('bar' => 'param1'));
     $message = sprintf('This is the email text <a href="%s">link</a>!', $url); 

     $this->userService->sendUserEmail($user, $message);
   }
}