独自のクラスを IOC コンテナに追加する

WBOY
リリース: 2016-06-20 12:27:20
オリジナル
887 人が閲覧しました

前の記事からわかるように、サービス プロバイダーがコンテナを管理するため、独自のクラスをコンテナに追加するときは、それを管理するサービス プロバイダーも登録する必要があります

1. カスタム定義クラスを作成します

psr4 の命名規則に準拠している限り、laravel は PHP クラスを独自のアプリ ディレクトリに自動的にロードできることに注意してください

app/Billing/Stripe.php  //这里在app下建立了一个Billing目录,里面放了一个Stripe的类< ?phpnamespace App\Billing;  //需要命名空间才能被php正确查找到class Stripe{    public  function  charge(){        dd('charge');    }}
ログイン後にコピー

2. サービスプロバイダーを作成します

このコマンドを使用して

php artisan make:provider BillingServiceProvider
ログイン後にコピー
app/Providers/BillingServiceProvider.php  //创建成功后会在这个目录生成php文件<?phpnamespace App\Providers;use App\Billing\Stripe;use Illuminate\Support\ServiceProvider;class BillingServiceProvider extends ServiceProvider{    /**     * Bootstrap the application services.     *     * @return void     */    public function boot()    {        //    }    /**     * Register the application services.     *     * @return void     */    public function register()    {        $this->app->bind('billing',function(){  //这里用了bind方法来创建一个container,绑定了一个billing的关键字来关联,这个container里会返回一个Stripe实例,相当于实例化了。            return new Stripe();        });    }}
ログイン後にコピー

を作成します。

の場合のみapp.php に登録されていますが、laravel で認識できますか? このサービスプロバイダーは、コード

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. 管理ルーティング (テスト)

は主にテスト目的なので、テストします。ルーティング内のサービスプロバイダー

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());});
ログイン後にコピー
関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!