简体   繁体   English

流明/丁戈/拉尔韦尔到达正确的控制器

[英]Lumen/Dingo/Laravel Getting to the correct controller

I know this is kind of a sin, but I don't have any code to show, it is just that I completely hit a brick wall. 我知道这是一种罪过,但是我没有任何代码可显示,只是我完全碰到了砖墙。 My problem is the following. 我的问题如下。

I have a client that connects to a Lumen/Dingo Api. 我有一个连接到Lumen / Dingo Api的客户端。 For all the requests it works great. 对于所有请求,它都很棒。 For example: 例如:

My route: 我的路线:

$api->get('contact', 'ContactController@get');

Coupled with this I have a ContactController and inside I have a get function that returns a response. 与此结合的是,我有一个ContactController ,在里面有一个返回响应的get函数。 No problem here. 没问题

Now the following problem : 现在出现以下问题

I have a route with tasks: 我有一条执行任务的路线:

$api->get('task/{id}', 'TaskController@get');

The problem is that in the system not all the Tasks are the same. 问题在于,系统中并非所有Tasks都是相同的。 Depending on the Type of task I want to perform special actions. 根据任务Type ,我要执行特殊操作。 I was thinking of creating a new Controller for each Task . 我正在考虑为每个Task创建一个新的Controller Like MySpecialTask1Controller , MySpecialTask2Controller and from my main TaskController I redirect to another controller. MySpecialTask1ControllerMySpecialTask2Controller一样,我从主TaskController重定向到另一个控制器。

Problem is 1) I do not know how to do this, and 2) I do not know if this would be the best way to do this. 问题是1)我不知道如何执行此操作,以及2)我不知道这是否是执行此操作的最佳方法。

Maybe this problem can be solved with services or other tools Dingo/Lumen provide. 也许可以使用Dingo / Lumen提供的服务或其他工具解决此问题。 Maybe someone can help me out on this. 也许有人可以帮我解决这个问题。

I wouldn't bother with controller for each task, I would rather go for a service class that handles that and is called in the TaskController. 我不会为每个任务而烦恼控制器,而宁愿选择一个处理该问题并在TaskController中调用的服务类。

The service class would have a public handle() method (or find a better name for it) which will take 1 argument (based on what you described). 服务类将具有一个公共handle()方法(或为其找到一个更好的名称),该方法将接受1个参数(根据您的描述)。 The parameter will be the type of task you need to do. 该参数将是您需要执行的任务的类型。

I would have another protected method which will have a lookup table based on the type you provide, it will call the corresponding class and execute the method within that class. 我将有另一个受保护的方法,该方法将根据您提供的类型提供一个查找表,它将调用相应的类并在该类中执行该方法。

This is also called polymorphism. 这也称为多态性。

Code example: 代码示例:

class TaskController extends Controller 
{
    protected $taskService;

    public __construct(TaskService $taskService)
    {
        $this->taskService = $taskService; 
    }

    public function someMethod()
    {
        // determine the type of task and then
        $this->taskService->handle($type)
    }
}

class TaskService 
{
    public function handle($type)
    {
        $this->doTask($type);
    }

    protected function doTask($type) 
    {
        // the lookup table
        $tasks = [
           'crazy' => new CrazyTask(),
           'insane' => new InsaneTask(),
           'hard' => new HardTask(), 
           'easy' => new EasyTasK()
       ];

       foreach($tasks as $taskType => $task)
       {
           if($taskType === $type) {
              return $task->do();
           }

       }
    }
}

This code should give you an idea how you can solve your problem using this design pattern (I can't think of the name right now). 这段代码应该使您了解如何使用此设计模式解决问题(我现在无法想到这个名称)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM