流明共享控制器 [英] Lumen shared controller

查看:84
本文介绍了流明共享控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个"Helper"控制器,我想在其他控制器中使用它,但是我做错了什么.我需要流明服务吗?然后我只是不知道如何设置它.

I wrote a "Helper" controller that I want to use in other controllers, but I am doing something wrong. Is Lumen Service what i need? Then I just don't get how to set it up.

我的主班:

namespace App\Http\Controllers;

use App\Http\Controllers\HelperController as Helper;

class InitController extends Controller
{
    public function work($hash, $type)
    {
        return response()->json([
            'answer' => Helper::makeCodeUrl()
        ]);
    }
}

帮助程序控制器:

namespace App\Http\Controllers;

class HelperController extends Controller
{
    public function makeCodeUrl($arr, $type){
       return str_random(32);
    }
}

推荐答案

请记住,控制器负责处理路由逻辑.考虑到这一点,实际上没有帮助程序控制器"之类的东西,因为没有路由被映射到它,因此从传统意义上讲它并不是真正的控制器".

Remember, a Controller is responsible for handling Routing Logic. With this in mind, there's really no such thing as a "Helper Controller", as no routes are being mapped to it and therefore isn't really a "Controller" in the traditional sense.

您所描述的是所谓的服务类别".任何具有适当复杂性的应用程序都将使用服务类作为一种手段,将所有业务逻辑从Controller中抽象为可重用的组件.

What you're describing is what's called a "Service Class". Any application of reasonable complexity will use service classes as a means to abstract away all of the business logic from Controllers into reusable components.

对于您而言,服务类正是您所需要的.为了构建自己的数据库,您需要做一些事情:

In your case, a Service Class is exactly what you need. In order to build your own, you need to do a few things:

  1. 注册 ServiceProvider
  2. 添加实际的Service provider类,并将您的Helper Service绑定到应用程序的IOC容器
  3. 创建您的帮助服务
  4. 在控制器的构造函数中键入帮助服务
  5. 运行composer dump-autoload
  1. Register a ServiceProvider
  2. Add the actual Service provider class, and bind your Helper Service to the application's IOC container
  3. Create your Helper Service
  4. Typehint the Helper Service in your Controller's Constructor
  5. Run composer dump-autoload

作为概念的快速证明,这将是最终的样子:

As a quick proof of concept, this is what it will end up looking like:

bootstrap/app.php

/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| Here we will register all of the application's service providers which
| are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line.
|
*/

$app->register(App\Providers\HelperServiceProvider::class);

app/Providers/HelperServiceProvider.php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Services\Helpers\HelperService;

class HelperServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind(HelperService::class, function(){
            return new HelperService;
        });
    }
}

app/Services \ Helpers \ HelperService.php

namespace App\Services\Helpers;

class HelperService
{
    public function makeCodeUrl(){
        return str_random(32);
    }
}

InitController.php

namespace App\Http\Controllers;

use App\Services\Helpers\HelperService;

class InitController extends Controller
{
    protected $helperService;

    public function __construct(HelperService $helperService)
    {
        $this->helperService = $helperService;
    }
    public function work($hash, $type)
    {
        return response()->json([
            'answer' => $this->helperService->makeCodeUrl()
        ]);
    }
}

虽然我可以体会到这种特殊的代码模式在学习上有一定的曲折,但我强烈建议您尽可能多地阅读有关服务提供商的信息.事实证明,它将来会变得非常有价值,并使您能够将整个第三方库引导到Laravel或Lumen安装中.

While I can appreciate that there's a bit of a learning curve with this particular pattern of code, I would highly recommend reading as much as possible about Service Providers. It will prove to become extremely valuable in the future, and will enable you to bootstrap entire 3rd party libraries into Laravel or Lumen installations.

这篇关于流明共享控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆