简体   繁体   English

如何从视图访问存储库逻辑?

[英]How do I access repository logic from the view?

Basically, I'm writing a simple blogging application where users can vote up or down on posts (I've named this process scoring inside my application). 基本上,我正在编写一个简单的博客应用程序,用户可以在其中投票赞成或反对帖子(我在应用程序内部将该过程评分)。 My problem is I am not sure what the best approach is for accessing a method to determine if the user has voted or not on the post, as I don't want to pass a repository into my view nor do I want the model to have methods mimicking the repository... Here are those two ideas - are these the only approaches? 我的问题是我不确定访问该方法以确定用户是否对该帖子投票的最佳方法是什么,因为我既不希望将存储库传递到视图中,也不希望模型具有模仿存储库的方法...这是两个想法-这些是唯一的方法吗?

The first approach requires that I pass a PostRepository to my views as well as the Post model which is already passed... 第一种方法要求我将PostRepository传递给我的视图以及已经传递的Post模型...

<!-- Repository-in-view approach -->
<p>You voted {{ $postRepository->hasUserScored($post->id, $user->id) ? 'up' : 'down' }}.</p>

-----------

// Inside `PostRepository`
public function hasUserScored($postId, $userId, $vote = true)
{
    // DB logic to determine ...
}

Or should I maybe do something like this? 还是我应该做这样的事情?

<!-- Repository-in-view approach -->
<p>You voted {{ $post->hasUserScored($user->id) ? 'up' : 'down' }}.</p>

-----------

// Inside `Post`
public function hasUserScored($userId)
{
    return (new PostRepository)->hasUserScored($this->id, $userId);
}

// Inside `PostRepository`
public function hasUserScored($postId, $userId, $vote = true)
{
    // DB logic to determine ...
}

What is the best way to overcome this? 克服此问题的最佳方法是什么? Any help greatly appreciated, thanks! 任何帮助,不胜感激,谢谢!

Best practice is to avoid calling functions from within views. 最佳实践是避免从视图内部调用函数。

Either call the function from the controller, and pass that data to the view. 从控制器中调用该函数,然后将该数据传递给视图。 Or, alternatively, use a view composer. 或者,也可以使用视图编辑器。

Why do you do this in such a complicated way? 你为什么要这么复杂地做呢? Can't your Post model just have many-to-many relationship with users + pivot table? 您的Post模型不能与用户+数据透视表建立多对多关系吗? Then, checking a User vote is as simple as: 然后,检查User投票很简单:

function users()
{
    return $this->belongsToMany('User')->withPivot('vote');       
}

function getUserScore($userId)
{
     return $this->users->find($userId)->pivot->vote;       
}

Also, if you want to check whether a user has voted or not, simply do: 另外,如果要检查用户是否已投票,只需执行以下操作:

function hasUserVoted($userId)
{
     return $this->users->contains($userId);
}

you have to pass the Repository to the view.. although what you want to do is not the smartest approach but according with what you need.. the way is 您必须将存储库传递给视图。.尽管您想要做的不是最聪明的方法,但是要根据您的需要..方式是

on your controller __construct you add something like this 在控制器__construct上,您可以添加如下内容

function __construct(PostRepositoryInterface $post){
    $this->post = $post;
}

if you dont use interface then just pass the repository 如果您不使用接口,则只需通过存储库

then you return to the view 然后您返回视图

view(your.view)->with('post', $this->post);

then you are done.. 那你就完成了..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel 5.4:除了视图,模型和存储库之外,我在哪里可以进行逻辑处理? - Laravel 5.4 : apart from the view, the model and the repository, where can I do the logic? Yii-如何从应用程序SiteController中的模块访问视图? - Yii - How do I access a view from a module in the applications SiteController? 如何从Drupal中的视图访问节点信息 - How do I access node information from a view in Drupal 如何从cakePHP控制器函数访问外部html元素(在视图中未定义)? - How do I access external html elements (not defined in the view) from a cakePHP controller function? 如何使用CMIS从Alfresco存储库下载图像? - How do I download an image from an Alfresco repository using CMIS? 如何使用codeIgniter在PHP中使用控制器访问三个视图页面? - How do I access three view pages with controller in PHP with codeIgniter? 如何在视图中使用键值访问数组值 - How do I access an Array Value using it's Key in a view 如何在我的视图中使用 Javascript 访问我的 PHP 响应? - How Do I Access My PHP Response in Javascript in My View? Symfony2:如何从存储库访问服务 - Symfony2: How to access to service from repository 如何从离子视图中的另一个视图获取数据? - How do I get the data from a view to another view in ionic?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM