向IOC 容器添加自己的類

來源:互聯網
上載者:User
從上一篇文章知道,serviceprovider是管理container的,所以在向container添加了自己的類的時候,也要註冊serviceprovider來管理

1.建立自訂的類

需要注意的是,laravel能夠對自身app目錄內的php 類進行自動載入,只要是符合psr4的命名規則就行

app/Billing/Stripe.php  //這裡在app下建立了一個Billing目錄,裡面放了一個Stripe的類< ?phpnamespace App\Billing;  //需要命名空間才能被php正確尋找到class Stripe{    public  function  charge(){        dd('charge');    }}

2.建立service provider

用這個命令建立

php artisan make:provider BillingServiceProvider
app/Providers/BillingServiceProvider.php  //建立成功後會在這個目錄產生php檔案app->bind('billing',function(){  //這裡用了bind方法來建立一個container,綁定了一個billing的關鍵字來關聯,這個container裡會返回一個Stripe執行個體,相當於執行個體化了。            return new Stripe();        });    }}

3.註冊service provider

在app.php裡註冊了才能被laravel識別這個service provider,才能在代碼裡面調用

config/app.php  'providers' => [  //在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\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,        Collective\Html\HtmlServiceProvider::class,        'Maatwebsite\Excel\ExcelServiceProvider',        /*         * Application Service Providers...         */        App\Providers\AppServiceProvider::class,        App\Providers\AuthServiceProvider::class,        App\Providers\EventServiceProvider::class,        App\Providers\RouteServiceProvider::class,        App\Providers\BillingServiceProvider::class, //按照格式添加即可    ],

4.管理路由(測試)

主要是為了測試目的,所以在路由裡面直接來測試service provider

app/Http/routes.php//這裡有兩種方式使用,第一種是跟上一篇文章提到的兩種container使用方式有關,第二種是通過自動解析機制來實現//Route::get('/', function () {//    $billing = app('billing');  //用直接傳入關鍵字,也可以用make()//    dd($billing->charge());//});Route::get('/', function (App\Billing\Stripe $billing) {  //傳入一個class名字和一個變數,laravel會自動去尋找和解析    dd($billing->charge());});
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.