毎度忘れては調べて作っている上に、なかなか正解に辿り着けないので覚書です。
手順
準備
適当にクラス作ります
app/Http/Controllers/hoge.php
class hoge
{
public function byTen($num)
{
return $num * 10;
}
}
ファサードの派生クラス作成
app/Facades/hoge.php
<?php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class hoge extends Facade
{
protected static function getFacadeAccessor(){
return 'hoge';
}
}
サービスプロバイダ作成
app/Providers/hogeServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class hogeServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
$this->app->bind(
'hoge',
'App\Http\Controllers\hoge'
);
}
}
Configに登録
config/app.php
(略)
'providers' => [
App\Providers\hogeServiceProvider::class,
]
(略)
'aliases' => [
'hoge' => App\Facades\hoge::class,
]
キャッシュクリア
console
php artisan optimize:clear