在Laravel中,如何将服务依赖注入控制器方法

时间:2017-07-24 08:56:53

标签: php laravel dependency-injection service-provider

很抱歉新手问题,但我找不到任何关于如何实际使用服务提供商的示例(Laravel文档仅显示如何创建它们)。

由于Laravel的分页器不适用于分组,我想创建自己的分页器服务,但我不知道如何将所述服务注入控制器。论坛控制器抛出异常:Class App\Http\Controllers\CustomPaginator does not exist,但我认为如果我将服务类绑定到服务容器,可以使用自定义名称解析它吗?

以下是文件:

应用/ HTTP /控制器/对于

<?php

namespace App\Http\Controllers;

use App\ForumModel;
use Illuminate\Http\Request;

class Forum extends Controller
{
    public function index($page = 1, CustomPaginator $paginator)
    {
        $topics = ForumModel::orderBy("sticky", "DESC")->orderBy("lastpost", "DESC");

        return view("forum", ["topics" => $topics]);
    }
}

应用/提供者/ CustomPaginatorServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class CustomPaginatorServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind("CustomPaginator", CustomPaginatorServiceProvider::class);
    }

    public function a()
    {
        echo "It works!";
    }
}

应用配置中的提供商表

'providers' => [

    /*
     * Laravel Framework Service Providers...
     */
    Illuminate\Auth\AuthServiceProvider::class,
    Illuminate\Broadcasting\BroadcastServiceProvider::class,
    Illuminate\Bus\BusServiceProvider::class,
    Illuminate\Cache\CacheServiceProvider::class,
    Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
    Illuminate\Cookie\CookieServiceProvider::class,
    Illuminate\Database\DatabaseServiceProvider::class,
    Illuminate\Encryption\EncryptionServiceProvider::class,
    Illuminate\Filesystem\FilesystemServiceProvider::class,
    Illuminate\Foundation\Providers\FoundationServiceProvider::class,
    Illuminate\Hashing\HashServiceProvider::class,
    Illuminate\Mail\MailServiceProvider::class,
    Illuminate\Notifications\NotificationServiceProvider::class,
    Illuminate\Pagination\PaginationServiceProvider::class,
    Illuminate\Pipeline\PipelineServiceProvider::class,
    Illuminate\Queue\QueueServiceProvider::class,
    Illuminate\Redis\RedisServiceProvider::class,
    Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
    Illuminate\Session\SessionServiceProvider::class,
    Illuminate\Translation\TranslationServiceProvider::class,
    Illuminate\Validation\ValidationServiceProvider::class,
    Illuminate\View\ViewServiceProvider::class,

    /*
     * Package Service Providers...
     */
    Laravel\Tinker\TinkerServiceProvider::class,

    /*
     * Application Service Providers...
     */
    App\Providers\AppServiceProvider::class,
    App\Providers\AuthServiceProvider::class,
    // App\Providers\BroadcastServiceProvider::class,
    App\Providers\EventServiceProvider::class,
    App\Providers\RouteServiceProvider::class,
    App\Providers\CustomPaginatorServiceProvider::class

],

1 个答案:

答案 0 :(得分:3)

不要在 CustomPaginatorServiceProvider

中绑定 CustomPaginatorServiceProvider

并绑定一个实例。

$this->app->bind('CustomPaginator', function () {
    return new Services\CustomPaginator();
});
  1. 创建服务提供商 CustomPaginatorServiceProvider
  2. 在app config中注册服务提供商 CustomPaginatorServiceProvider
  3. 创建服务 CustomPaginator
  4. CustomPaginatorServiceProvider
  5. 绑定服务 CustomPaginator

    btw您可以在 AppServiceProvider 中绑定服务 CustomPaginator ,而不是创建新的服务提供商

    别名数组中的

    config / app.php:

    ...
    'CustomPaginator' => \App\Services\CustomPaginator::class,
    ...
    

    控制器内部动作:

    public function someAction(\CustomPaginator $paginator)
    {
        dd($paginator);
    }
    

    如果你需要将一些数据传递给CustomPaginator方法__construct - 只需在ServiceProvider中写一下:

    $this->app->bind(\App\Services\CustomPaginator::class, function () {
        return new \App\Services\CustomPaginator($firstParam, $secondParam, ...);
    });