私はあなたがサービスを行うために必要があると思う:
# app/Services/Payment/Payment.php
<?php
namespace App\Services\Payment;
class Payment
{
public function billUser($cardnum, $mm, $yy, $cvc)
{
// Your logic here
}
}
その後、あなたはこのサービスを統合したい場合は、「Laravelの道」、あなたはサービスプロバイダを作成する必要があります:
# app/Services/Payment/PaymentServiceProvider.php
<?php
namespace App\Services\Payment;
use Illuminate\Support\ServiceProvider;
class PaymentServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton('Payment', function($app) {
return new Payment();
});
}
}
その後config/app.php
でこのサービスプロバイダを登録します。
return [
...
'providers' => [
...
App\Services\Payment\PaymentServiceProvider::class,
...
],
...
];
をすることができますし、ACお使いのコントローラで目的税を:
<?php
namespace App\Http\Controllers;
public function completeRegistration(Request $request)
{
$yy = $request->exp_year;
$mm = $request->exp_month;
$cardnum = $request->cardnum;
$cvc = $request->cvc;
if(app('Payment')->billUser($cardnum, $mm, $yy, $cvc))
{
echo "charge success";
}
else
{
echo "uh something was wrong";
}
}
あなたはその後、さらに行くと、あなたのサービスのためのファサードを作成することができます。
# app/Services/Payment/PaymentFacade.php
<?php
namespace App\Services\Payment;
use Illuminate\Support\Facades\Facade;
class PaymentFacade extends Facade
{
protected static function getFacadeAccessor()
{
return 'Payment';
}
}
それを使用するには、あなたもあなたのapp/config.php
ファイルにそれを登録する必要があります。
return [
...
'aliases' => [
....
'Payment' => App\Services\Payment\PaymentFacade::class,
],
];
そして、あなたのコード内の任意の場所にそれを使用する:
<?php
namespace App\Http\Controllers;
use Payment;
public function completeRegistration(Request $request)
{
$yy = $request->exp_year;
$mm = $request->exp_month;
$cardnum = $request->cardnum;
$cvc = $request->cvc;
if(Payment::billUser($cardnum, $mm, $yy, $cvc))
{
echo "charge success";
}
else
{
echo "uh something was wrong";
}
}
こちらがお役に立てば幸いです。
サービスプロバイダーがこれを行う方法です。多くの異なるサイトで使用されている場合は、https://laravel.com/docs/master/providersでカスタム作曲家パッケージを作成してください –